• C

problem with fread

Hello group,

I'm using fread() function to read a file as:

            bytes_read=fread(csv_buffer, 1, 1024, fp);

As far as I know since fp has value and it is no NULL so this function should be able to read the data. However, bytes_read is always 0.

Please find the code in snippet. Any help is appreciated.


Thanks,
ak


fp = fopen(outfile, "rb");
    if (!fp)
    {
      fprintf(stderr, "Failed to open %s\n", outfile, strerror(errno));
      exit(EXIT_FAILURE);
    }
 
    bytes_read=fread(csv_buffer, 1, 1024, fp);
    printf("\nbyte_read = %d", bytes_read);     >>>> shows always 0!

Open in new window

akohanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Does the file have data?
clear errno before fread and check errno after call
Call ferror and see if it reports an error.
0
Infinity08Commented:
Note that fread doesn't return the number of bytes read. It returns the number of >elements< read. In this case, the size of your elements is 1 byte, so it comes down to the same, but it's an important distinction to keep in mind.

Now, regarding your problem.

Check the size of the file that you opened. Does it have size 0 ?
0
stev75Commented:
when read binary, you can specify in fread(), how you interprete the binary data, eg. a struct.
arg1 // pointer to buffer
arg2 // size of each element of the array; in bytes, e.g. sizeof(char) reading text stored as bin
arg3 //simply how many elements you want to read
size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file);
 You simply need to tell your compiler how to interprete your data

 
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

akohanAuthor Commented:

Yes file has a size but can you let me know how I use errno in this case?
0
sunnycoderCommented:
0
Infinity08Commented:
>> Yes file has a size but can you let me know how I use errno in this case?

Are you sure ? Verify that the file you want to open is the same file as the file you are actually opening. What's the value of 'outfile' ?
0
akohanAuthor Commented:

 Simply I put:

    errno=0;
    bytes_read=fread(csv_buffer, 1, 1024, fp);
    printf("error no = %d", errno);                              

and it is 0.

0
Infinity08Commented:
See my previous post ...
0
akohanAuthor Commented:

This is so strange! it works up in my code but it doesn't work in few blocks down!!!

0
Infinity08Commented:
Can you show all of your code ?

>> it works up in my code but it doesn't work in few blocks down!!!

What do you mean by that ?
0
akohanAuthor Commented:

Sure. I have attached the code. The fread with issue is marked with comment as "problem starts here".

Thanks!
0
akohanAuthor Commented:

Sure. I have attached the code. The fread with issue is marked with comment as "problem starts here".

Thanks!
0
akohanAuthor Commented:

I cannot see it attached! retrying ...
abc.txt
0
Infinity08Commented:
>> I have attached the code.

Can't see it ;)
0
Infinity08Commented:
Don't you want to pclose the fp file pointer before opening it again ?
0
akohanAuthor Commented:

Do you mean after 2nd while() ?
0
Infinity08Commented:
I mean right before you experience the problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Thanks! ok, now I found I was missing popen() in there and now I can read data however, the problem is that it still shows previous data from previous while loops.
I even cleaned csv_buffer[] but didn't help.

Doesn't it store read data into this buffer?

Regards,
0
Infinity08Commented:
>> the problem is that it still shows previous data from previous while loops.

Can you give a bit more detail ?
0
akohanAuthor Commented:

Ok, as you know there are two series of while loops as:

 ... some code

 while(fread()....){
     if(csv_parse()....)
  }

 while(fread()....){
     if(csv_parse()....)
  }


  //starting a new set

 while(fread()....){
     if(csv_parse()....)
  }

 while(fread()....){
     if(csv_parse()....)
  }

In the second while loop set I had to call a function (library related) as init_csv() to clear previous data stored in memory. The problem was that I was getting previous processed data. Now, by calling this function library I'm getting what I was expecting.

Thank you so much for being so patient with me to learn!

Regards.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.