从一行中获取字符串 [英] Getting strings from a line

查看:119
本文介绍了从一行中获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试逐行读取文件并从每行中以空格分隔的字符串中获取字符串.我在做这样的事情:

Hi,

I am trying to read a file line by line and get strings from the each line that are seperated by space.I am doing something like this:

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;
}



但这是行不通的.调用GetStringsFromLine()函数时,Str2不被打印并且代码崩溃.请帮助我更正.我也是检查行尾是否正确的方法(寻找''\ n'')吗?
谢谢.



But this isnt working.Str2 is not printed and code crashes when GetStringsFromLine() function is called.Please help me correct it.Also is my way for checking end of line correct(looking for ''\n'')?
Thank you.

推荐答案

:: strtok 是您想要将字符串拆分成用空格"分隔的单词的方法. >
首先调用:: strtok,提供要解析的字符串以及要查找的令牌.
只要该函数不返回NULL,则返回值将指向下一个单词.
当函数返回NULL时,您已经到达了行的末尾.

:: strtok会修改您传入的字符串,因此请确保不需要字符串后缀.它将用NULL字符替换空格.

::strtok is what you want to split a string into words separated by a space " ".

first call to ::strtok, provide the string you want to parse, as well as the token to look for.
as long as the function does not return NULL the return value will point to the next word.
When the function returns NULL, you have reached the end of the line.

::strtok modifies the string you pass in, so make sure you do not need the string afterwords. It will replace the spaces with NULL characters.

// you assigned the string text to a value called "str1" earlier in the code.

cout << "Complete Line Text: " << str1 << endl;
char* pWord = ::strtok(str1, " ");
while (pWord)
{
  cout << "Word: " << pWord << endl;
}

// Example with print out:
//
//     "Getting strings from a line"
//
// Output:
//
// > Complete Line Text: Getting strings from a line
// > Word: Getting
// > Word: strings
// > Word: from
// > Word: a
// > Word: line


不,寻找"\ n"并不是真正正确的,尤其是因为它不可移植.对于不同的平台,行尾字符是不同的.例如,在Microsoft系统中,这是"\ r \ n".这就是为什么可以以与平台无关的方式使用文本文件特殊API的原因.
你在说什么?这是一个文本文件.查看本手册,阅读文本文件"部分,这是使用string的第二个代码示例: http://www.cplusplus.com/doc/tutorial/files/ [ ^ ].

—SA
No, looking for ''\n'' is not really correct, in particular, because this is not portable. End of line characters are different for different platforms. In Microsoft systems, for example, this is ''\r\n''. That''s why the special APIs for text files are available to do in a platform-independent way.

What are you talking about? This is a text file. Look at this manual, read the section "Text file", the second code sample where string is used: http://www.cplusplus.com/doc/tutorial/files/[^].

—SA


这篇关于从一行中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆