0

I'm trying to read character by character from a text file until EOF, put them into a character array, so that I can manipulate it after. Compiled with g++ without errors, and when run, I'm prompted for the input file but then it just hangs.

int main (int argc, char *argv[]) { string filename; ifstream infile; char *cp, c[1024]; memset (c, 0, sizeof(c)); cp = c; cout << "Enter file name: " << endl; cin >> filename; //open file infile.open( filename.c_str() ); //if file can't open if(!infile) { cerr << "Error: file could not be opened" << endl; exit(1); } while (!infile.eof()); { infile.get(c, sizeof(infile)); // get character from file and store in array c[] } }//end main 
3
  • 1
    See stackoverflow.com/q/7241871/485561 Commented Nov 25, 2012 at 3:24
  • 1
    You shouldn't read char by char. This is not the 1980s anymore... Commented Nov 25, 2012 at 3:39
  • I should've mentioned that it's for my assignment, but that's good to know. Commented Nov 25, 2012 at 5:57

3 Answers 3

1

You should try the istream::read() method rather than get(). This will help resolve any buffer overruns:

unsigned int chars_read = 0; //... // Read in the file. if (!infile.read(c, sizeof(c)) { // Handle the read error here. // Also check for EOF here too. } // Obtain the number of characters actually read. chars_read = infile.gcount(); 
Sign up to request clarification or add additional context in comments.

Comments

0

First off, you don't want to test for eof()! Somehow I start to feel like Don Quixote having found my windmills. However, I do know that you need to check that the input was successful after trying to read it because before attempting to read the stream can't know whether it will be successful.

You program actually doesn't hang! It just waits for you to enter sizeof(infile) characters or end the input (e.g., using Ctrl-D on UNIXes and Ctrl-Z on Windows). Of course, this may look remarkable like a hanging program. You can verify that this is, indeed, the problem by using a smaller size, e.g., 4. Of course, sizeof(infile) is nearly as good as a small random number: It is the size of an object of type std::ifstream and who can tell what that is? You probably meant to use sizeof(c) to make sure that the call to get(c, n) won't write more character than can fit into c.

3 Comments

The OP also needs to check the amount of characters read vs. the capacity of the array. Otherwise a buffer overflow will occur.
Thanks everyone, changing sizeof(c) and doing an infile.gcount() has helped. I did notice that when my file is being read, it only reads up until the next return line (hence only one line is read)?
I had used infile.get() and infile.read(), but infile.read() ignores new lines.
0

Try this:

int cont = 0; while(infile.good()) { c[cont++] = infile.get(); } 

2 Comments

Possible buffer overflow with your solution. If the file size is greater than 1024, this starts writing beyond the array.
Yes, but don't blame me, he's the one who put the limit :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.