Jan 27, 2012 at 11:27pm UTC
how do i show output on command prompt from a file? The type i want to show is a string.
Jan 27, 2012 at 11:42pm UTC
i need it to start reading at a certain point, then read the rest of the file until it reaches xxx(do/while +fscanf)?
Last edited on Jan 27, 2012 at 11:45pm UTC
Jan 27, 2012 at 11:47pm UTC
Moschops was on about the getline() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt" );
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file" ;
return 0;
}
EDIT:
I noticed you edited your post. Do you know about getline()?
If so, have a look at the following find() function, for strings:
http://www.cplusplus.com/reference/string/string/find/
Last edited on Jan 27, 2012 at 11:54pm UTC
Jan 28, 2012 at 12:06am UTC
the find isn't exactly what i'm looking for. i have a reservation system and i need it to display all the bookings that are currently in the system. i would use the getline function as a seekg (i think that is the right way to go about setting a get pointer)
Jan 28, 2012 at 12:32am UTC
Read in from file. If that's a booking, display it. Repeat until reach end of file. It's that simple.
Using seekg to skip back and forth through a text file as if it were a binary file is making things much harder for yourself.