Hi, can anyone help me with this function. It opens my file but when it reaches the fread command, it crashes with an access violation 0xC0000005. It's driving me nuts. The file is a simple text file that has been created with Pocket Word and renamed to a .txt file.
- Code:
char *LoadStr (char fn[])
{
FILE *f;
char *s = NULL;
char buffer[1024];
int i;
f = fopen(fn, "rb");
if (f != NULL)
{
s = _strdup("");
while (!feof(f))
{
i = fread(buffer, sizeof(char), 1024, f);
if (ferror(f))
break;
else
strcat(s, buffer);
}
fclose(f);
}
return s;
}
Regards,
kornalius
Okay, what you're trying to do is allocate memory for your data, pointed to by s. I assume that's what the strdup is for, to allocate memory. strdup will allocate enough space to make a copy of the string you pass to it. You are passing a string of length 0, so it will allocate 1 byte (for the null terminator). Then you're trying to read all kinds of data into that single byte.
So in place of strcpy you need to do something like this:
s=malloc(i);
memcpy(s, buffer, i);
However your loop is still not right, because you aren't doing anything with each s you allocate. So you've got a memory leak, and the end result is that s points to the last chunk of data you loaded. That is in the event that the file is larger than your buffer, which is 1024 bytes. If the file is less than 1024 bytes then it will "work".
Dan East
Hi Dan,
In fact I am trying to load buffer with a maximum of 1024 char's and then append it to the end of s until then eof. Then s is my variable I want to return. Is there something wrong with the FREAD ? It crashes there, the s variable is not used at this point. Buffer should hold 1025 char's no? Fread should then return the number of bytes read.
What does s point to? The memory allocated by strdup. How much memory did strdup allocate? 1 byte. strcat, strcpy, etc, do not allocate memory, they just write to the buffer you provide. So strcat is writing past the end of your buffer. If you change the strdup call to s=malloc(1024) (and then set s[0]=0) then it will work.
Dan East
kornalius wrote:Hi Dan,
In fact I am trying to load buffer with a maximum of 1024 char's and then append it to the end of s until then eof. Then s is my variable I want to return. Is there something wrong with the FREAD ? It crashes there, the s variable is not used at this point. Buffer should hold 1025 char's no? Fread should then return the number of bytes read.
I don't see anything wrong with the fread call. Buffer can hold 1024 chars, which is how many 1 byte elements you are telling fread to load at most.
Dan East
You should always use the "sized" versions of the string functions, with proper sizes, to avoid buffer overflow and underflow.
For example, use strncpy instead of strcpy.
mlepage wrote:You should always use the "sized" versions of the string functions, with proper sizes, to avoid buffer overflow and underflow.
For example, use strncpy instead of strcpy.
...which completely throws the concept of a "string" out the window. If you want to deal with raw arrays of bytes then just use memcpy. There's nothing wrong with using null terminated strings as long as you use them correctly.
Dan East
Okay, let's start over here. There is no need to call fread in a loop. It will read all of the bytes you tell it to, up to EOF.
- Code:
char *LoadStr (char fn[])
{
FILE *f;
char *s;
int i;
f = fopen(fn, "rb");
if (!f)
return NULL;
//determine size of file
fseek(f, 0, SEEK_END);
i=ftell(f);
fseek(f, 0, SEEK_SET);
//allocate mem for string and read data into it
s=malloc(i);
fread(s, 1, i, f);
fclose(f);
return s;
}
That assumes your string as stored in the file has a null terminator. If not then you need to allocate an extra byte and set the last element to zero.
Dan East
Hi Guys,
Thanx for the tip about the strcat. I now make sure I have room before I do a strcat.
The LoadStr works like a charm now. I understand a little more how string works in C.
Regards,
Kornalius