返回首页

您好,

我想逐行读取文件中的一行,并从每一行是由space.I分离得到的字符串,我做这样的事情:

ifstream fin;

string dir, filepath;

DIR *dp;

 cout << "dir to get files of: ";

  getline( cin, dir );  // gets everything the user ENTERs



  dp = opendir( dir.c_str() );

  if (dp == NULL)

    {

    cout << "Error(" << errno << ") opening " << dir << endl;

    return errno;

    }

 

  while ((dirp = readdir( dp )))

    {

	 filepath = dir + "/" + dirp->d_name;

    string str="h";

    fin.open( filepath.c_str() );

 

   pos=filepath.find(".");

    //Way to turn a string into const char* using c_str()

   const char* str1=(filepath.substr(pos+1)).c_str();

   //compare the strings

   if((filepath.substr(pos+1).compare(str))==0)

   {

	   char character;

	   cout<<"FoundC"<<endl;

	   cout<<filepath<<": "<<num<<endl;

	   std::string line;

	   std::string word;

	   std::getline(fin,line);

	   cout<<"getline"<<": "<<"line"<<endl;

	   if(fin.get(character).good())

	   {

		   while(character!='\n')

			   word = GetStringsFromLine(line);

 

	   }

	   cout << line << endl;

	   cout << word << endl;

	   cout << buffer <<endl;

   }

 

std::string& GetStringsFromLine(std::string& line)

{

 std::string str2;

 

 int position=0;

 std::stringstream ss;

 cout << "line=" << line << endl;

 

  		 while( (line.at(position) != (isspace(line.at(position)))) )

  		 {

  			 ss<<line.at(position);

  			 ss>>str2;

  			 position++;

  			 cout << "position=" << position << endl;

  		 }

  		 	 cout << "str2=" << str2 << endl;

 return str2;

}

但这并不是working.Str2不打印和代码崩溃时GetStringsFromLine()函数是called.Please帮助我正确it.Also是我的方式,检查线正确(寻找的'\ n')
年底谢谢。

回答

评论会员:游客 时间:2012/02/07
。::​​strtok是你想要的分成一个字符串由空格分隔的单词"第一次调用::strtok,提供你想要的字符串解析,以及令牌寻找。只要函数没有返回null返回值将指向下一个字。当函数返回NULL,你已经达到了该行的末尾。::strtok修改你所传递的字符串,所以一定要确保你不需要字符串afterwords。它将取代NULL字符的空间。codeprelang="c++"spanclass="code-comment"///spanspanclass="code-comment"youassignedthestringtexttoavaluecalled"str1"earlierinthecode./spancout<<spanclass="code-string""/spanspanclass="code-string"CompleteLineText:"/span<<str1<<endl;spanclass="code-keyword"char/span*pWord=::strtok(str1,spanclass="code-string""/spanspanclass="code-string""/span);spanclass="code-keyword"while/span(pWord){cout<<spanclass="code-string""/spanspanclass="code-string"Word:"/span<<pWord<<endl;} spanclass="code-comment"///spanspanclass="code-comment"Examplewithprintout:/spanspanclass="code-comment"///spanspanclass="code-comment"/spanspanclass="code-comment"///spanspanclass="code-comment""Gettingstringsfromaline"/spanspanclass="code-comment"///spanspanclass="code-comment"/spanspanclass="code-comment"///spanspanclass="code-comment"Output:/spanspanclass="code-comment"///spanspanclass="code-comment"/spanspanclass="code-comment"///spanspanclass="code-comment">CompleteLineText:Gettingstringsfromaline/spanspanclass="code-comment"///spanspanclass="code-comment">Word:Getting/spanspanclass="code-comment"///spanspanclass="code-comment">Word:strings/spanspanclass="code-comment"///spanspanclass="code-comment">Word:from/spanspanclass="code-comment"///spanspanclass="code-comment">Word:a/spanspanclass="code-comment"///spanspanclass="code-comment">Word:line/span/pre/code
SAKryukov
评论会员:游客 时间:2012/02/07
,寻找的'\n'是不是真的正确,特别是,因为这是不可移植的。为不同的平台有不同的行字符结束。例如,在微软的系统,这是'\R\N'。这就是为什么特殊的API文本文件都可以在一个平台独立的方式做。你说的是什么?这是一个文本文件。看看本手册,阅读节"文本文件",第二个代码示例使用字符串:imgsrc=]。mdash;SA