Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

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


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.
Posted
Updated 29-Nov-11 17:17pm
v2

::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.

C++
// 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
 
Share this answer
 
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
 
Share this answer
 
v2
Comments
Abhinay Kumar 30-Nov-11 0:43am    
i know how to get a line.What i want is when i fetch a line i should be able to traverse the entire line and get individual strings in the line that are seperated by space.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900