How to read text file line by line?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream in("file.txt");

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  char str[255];

  while(in) {
    in.getline(str, 255);  // delim defaults to '\n'
    if(in) cout << str << endl;

  }

  in.close();

  return 0;
}


This is my code but it is reading whole text file but i want to read first line and input it to function after that again read second line then input it to function and again read third line and so on
Last edited on
That's kind of what you're doing, isn't it? Am I misunderstanding something, here? Also, it's better to loop on getline, and prefer std::string to C-style strings.

Look:
1
2
3
4
5
6
7
std::string str;
while (std::getline(in, str)) {
    // output the line
    std::cout << str << std::endl;

    // now we loop back and get the next line in 'str'
}
The code which i posted is reading file.txt till EOF(End of file) but i want to read first line in first attempt and pass that line to some function1 after that again read second line and pass second line to same function1 and again read 3rd line and so on. So how this can be implemented in loop reading line by line and passing one line once that line encounter end of line .
Last edited on
char str[255];
while(in.getline(str,255))
{
cout << str <<endl;
}
So... like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function1(const std::string& str) {
    // ... do stuff ...
}

int main() {
    // ...

    std::string str;
    while (std::getline(in, str)) {
        function1(str);
    }

    // ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 ifstream myfile ("file.txt");
   
		   if (myfile.is_open())
		   {
			
				while (! myfile.eof())	
			{
				for(i=0;i<=100;i++)
				{
				
		
					getline (myfile,line);
					cout << line << endl;
					break;
								
				}
				std::thread th1 (&classA::function1,&x,line);
			  	std::thread th2 (&classA::function2,&x);
				th1.join();
			  	th2.join();
				
			 }
			myfile.close();
		   }
				else 
				cout << "Can't open file"<<endl; 


I have made to work it, in my way.......Thank you all for advice
That doesn't make overly sense. The for loop (line 8) has no effect. Plus you read one line beyond the end (because you did not check the error state/eof when getline (on line 12) is done).
Last edited on
Still i am working on it..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

		   if (myfile.is_open())
		   {
			
				while (! myfile.eof())	
			{
				for(i=0;i<=20;i++)
				{
				
					myfile.ignore(3,' ');
					getline (myfile,line);
					//cout << line << endl;
				if(i%2!=0)
				{
				
				std::thread th2 (&classA::function2,&x,line);
				th2.join();
				}
				else
				{	
				std::thread th1 (&classA::function1,&x,line);
			  	th1.join();
			  				
				}
				
				}
				break;
			
		 	 }
			myfile.close();
			
		   }
				else 
				cout << "Can't open file"<<endl; 

Just want to pass even lines to function1 and odd lines to function2. Work is still in progress. As soon as come up with final code i wil post it. Please if i am going wrong means let me know i will correct it.
Just a word of advice - you'll help yourself a lot if you adopt a sensible, consistent indentation style. You'll be able to see the flow of control much more clearly, and see errors more easily.

Also, even when a conditional block has a single line - e.g. your line 34 - it's a good idea to use braces around it. It makes it less likely that you'll introduce errors if you decide to add more lines.
Last edited on
The code is overly complex and some of it seems to add complexity while providing no actual functionality.

1. the while loop at line 5 terminates after the first pass when reaching the break statement at line 27. Thus it isn't actually behaving as a loop at all.

2. the use of while (! myfile.eof()) is generally to be avoided, it can lead to errors. The code presented by NT3 above is better.

3. there is no check of file status after the getline() at line 11. This can (and almost certainly will) lead to errors.

4. It isn't clear what is the purpose of myfile.ignore(3,' ');

5. It isn't clear why a for loop is used. Are there exactly 20 lines in the file, or is it required to process only the first 20 lines?

In order to distinguish between odd/even lines, an integer count does make sense, but it need not use a for loop to do so.

Try using a class container to store multiple objects in. loop through the file and store each line in an object then put it in a vector.
Example:

1
2
3
4
5
while(file >> [whatever you are reading)
{
   Object * pObj = new Object(whatever you were reading);
   vector.push_back (([Containable Class] *) pObj);
}
Topic archived. No new replies allowed.