簡體   English   中英

通過命令行傳遞的C ++ txt文件(std :: ifstream inFile具有初始化程序,但類型不完整)

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

我正在嘗試讀取包含多個項目的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”具有初始值設定項,但類型不完整

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM