RS> Hi, I got a problem I been working on. I'm a beginner at C++
RS> programming. What I need help with is how to read a text file line by
RS> line with a program. I can get my program to open a file stream and
RS> it reads the first line then exits. Could someone please point me into
RS> the right direction :)
I'm assuming your using the iostream.h package.
I'm assuming your using the ifstream class.
****
const BUFF_SIZE = 256;
char buffer[BUFF_SIZE];
ifstream infile("file.txt"); // opens the file "file.txt"
while (!infile.eof()) { // loop while not the end of the file
infile.getline(buffer, BUFF_SIZE); // allow upto 256 chars per line
}
infile.close();
// in this example .eof is an inherited method from "ios" and
// getline is inherited from "istream"
// getline has an optional parameter which specifies the text
// end of line delimiter (\n by default)
// gcount() can tell you how many characters where read
--- GEcho 1.00
---------------
* Origin: Digital OnLine Magazine! - (409)838-8237 (1:3811/350)
|