View Full Version : is there a limit to char buffer sizes?
cliffski
08-27-2004, 02:17 PM
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?
IIRC gets() should return an int with the number of bytes which were actually read.
ggambett
08-27-2004, 02:26 PM
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.
GameStudioD
08-27-2004, 03:28 PM
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.
vBulletin v3.6.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.