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