Jump to content
  • Advertisement
Sign in to follow this  

Strange fread problem.

This topic is 4551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lang: C What I'm tring to do is to display all the contents in the file, but the problem is that when I use fread to read in all the information in one sweap and store it in the data "buffer", the fread seems to be reading other characters that are past the EOF. Is there any way to solve this? The file content is: testing file stream The output is: testing file streamogramH
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[], char *envp[]) {
	FILE *usr_file = NULL;
	FILE *ysh_file = NULL;
	char *data = NULL;
	long usr_file_size = 0;
	int result = 0;

	usr_file = fopen("./usr_file", "rb");

	if(NULL == usr_file) {
		printf("File Error: Couldn't open %s file!\n", "usr_file");
	result = fseek(usr_file, 0, SEEK_END);

	if(result != 0)

	usr_file_size = ftell(usr_file);

	data = (char *)malloc(usr_file_size * sizeof(char));

	fread(data, sizeof(char), usr_file_size, usr_file);

	printf("%s\n", data);	



Share this post

Link to post
Share on other sites

fread is reading data past the end of the file, or the variable "data" is displaying data, past what should be the end of the file?

It's likely you need to put a terminating character at the end of the data.
For example:

data = (char *)malloc( ( usr_file_size + 1 )* sizeof(char));

// read in your data
fread(data, sizeof(char), usr_file_size, usr_file );

// Cap the end of the char* so printf knows where the end of the string is
data[usr_file_size] = 0;

Cheers and good luck!

Share this post

Link to post
Share on other sites
Wow! It makes me wonder sometimes about my programming skills when I forget about the little things like '\0'. I defently don't want to sound big headed at all, but people at my college view me as a very good coder, but am I a good coder if I forget about '\0'?

Thanks jwalsh, kspansel

Share this post

Link to post
Share on other sites

Exchanging the call to Rewind() with the call kspansel suggested will also likely help. According to the MSDN documentation:

However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. Also, unlike fseek, rewind does not return a value to indicate whether the pointer was successfully moved.

The removal of the "end-of-file" indicator could be the reason your char* isnt capped with a null character.

Edit: I dont recall whether fread() adds a null character when it encounters eof or not.

Share this post

Link to post
Share on other sites
Thats a good point, I was questioning rewind but I wasn't sure that fseek would be better off instead of rewind. Thanks for the tips.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!