ive got a weird bug where im using fgets (or fstream) to read a line of text from a file. im setting my buffer at 1024 chars, but if the input is over 512 chars, then it seems to 'overcopy' the amount, and basically miss the next new line. its very weird... #define MAXSTRING 1024 FILE* fileptr = fopen("text.txt","rt"); while(!feof(fileptr)) { char buffer[MAXSTRING]; strcpy(buffer,""); fgets(buffer,MAXSTRING,fileptr); } what am i missing?
Hmmm. My only doubt is reading MAXSTRING characters instead of MAXSTRING-1, because of the \0 added by fgets(). You could have problems if you happen to read a MAXSTRING characters line (writting MAXSTRING + 1 bytes to buffer) Other than that, it should work fine. Maybe there's an implementation-specific limitation? I find nothing in the man page.
The code looks ok, except for what ggambett pointed out. I have done strings bigger than 1024 before ... it should work. It sounds like fgets is missing an end of string char for some reason. Take a look at the input coming in, maybe a space or newline char is missing or the input is just messed up. Also, check ferror to see if an error occured. I think this might be an "I forgot to cross the t" error when you do figure it out.