0

Hi I'm reading data from a file put it in the buffer. Now I read the data from the file and got it in the buffer, but somehow the buffer is filled with some junk. In fact, I get the code from http://www.cplusplus.com/reference/clibrary/cstdio/fread/.I always get the result Reading Error and when I check the size of the lSize and result , they two are not the same.I'm new to C or C++.Can somebody help me? I tag both C and C++ since I don't know which one is the correct one. Sorry.

FILE *data_fp, *keyFile;
long lSize;
int i,j;
char hvalue[21];
char dt[300];
uint64_t insert_key;

data_fp = fopen("./aaa", "r+");
if (data_fp == NULL) {
        printf("fopen aaa error\n");
        exit(-1);
}
// obtain file size:
fseek (data_fp , 0 , SEEK_END);
lSize = ftell (data_fp);
rewind (data_fp);

// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

size_t result = fread (buffer,1,lSize,data_fp);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
puts(buffer);
2
  • Have you printed lSize? Have you printed result? Did you consider removing the unused variables from this snippet? Did you consider defining the used variables (result, buffer) that are not defined? Did you try making the snippet into an executable program? Commented Apr 13, 2011 at 3:04
  • Thx Jonathan, It's ok now after putting rb instead of r+. Commented Apr 13, 2011 at 3:27

2 Answers 2

2
data_fp = fopen("./aaa", "rb");
Sign up to request clarification or add additional context in comments.

2 Comments

Yes It's ok now !! But when I try with .txt file, I must still put rb ??? When I tried with r with .txt file, the result and lSize are still not the same.
@kevin: That's because ftell() does not tell you the size of the file in text mode. Reading from a binary stream and then writing to a text mode stream (like stdout) is not usually the right thing to do.
2

Your file is open in text mode. Using ftell() to determine the length of a file open in text mode is not portable, and doesn't necessarily work.

fread() may return a short item count if either end-of-file is reached, or an error occurs. You must use feof() / ferror() to determine which is the case:

size_t result = fread(buffer, 1, lSize, data_fp);
if (result != lSize) {
    if (ferror(data_fp)) {
        perror("Reading error");
        exit (3);
    }
    /* End-of-file reached, so adjust file size downward */
    lSize = result;
}

(Using perror() will mean that if an error is occuring, you will see what it is).

Note also that the data read from the file is not necessarily nul-terminated, so you cannot just pass it to puts(). You must add a nul-terminator:

buffer[lSize] = 0;

This will also require allocating a single additional byte:

buffer = (char*) malloc (lSize + 1);

(sizeof(char) is defined to be 1, so it is not necessary here).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.