C++ txt 文件通过命令行传递(std::ifstream inFile 具有初始值设定项但类型不完整)

C++ txt file passing thru command line ( std::ifstream inFile has initializer but incomplete type)

本文关键字:类型 命令行 文件 txt inFile ifstream std C++      更新时间:2023-10-16

我正在尝试读取包含多个项目的list.txt文件(如下所示(

100/1 
111/1
115/2 
116/3 
120/1

通过命令提示符说program.exe list.txt,所以argv[1]将是txt文件list.txt(保存在CWD中(然后我需要将100/1分成 partname = "100"rev_id = "1" 并运行 do_something() ;类似地,111/1 partname = "111"rev_id = "1"(循环中(。

以下是我的代码片段。 它在ifstream std::ifstream处给出编译错误:"inFile具有初始值设定项但类型不完整。

有人可以告诉我我在这里哪里出错了。

多谢!

#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main (int argc, char *argv[])
{
    ifstream inFile (argv[1]);
    string partName;
    while (inFile)
   {
      getline(inFile, partName);
      cout <<endl<< partName << endl;
    std::string str = partName;
    char *cstr = new char[str.length() + 1];
    strcpy(cstr, str.c_str());
 char * partname=(strtok (cstr,"/"));
    char * rev_id = (strtok (NULL,"/"));
    //do something();
     printf(" n Partname is %s n",partname);
  printf(" n Rev_id is %s n",rev_id);
   }
inFile.close();
  return 0;
}

包含标头<fstream>

你可以在这里找到类似的帖子。很少谷歌搜索就足够了。

文件不完整类型错误

C++:变量"std::ifstream ifs"具有初始值设定项,但类型不完整