• C

Open and read a file

I try to read numbers in a file but the result I got when reading each element are not the same numbers.
Ex: in file is  1324
    print is    18723

Why ?  Here the code.
#include <stdio.h>
//#include <stdlib.h>

//#define in(fp, pbuf) fread(pbuf, sizeof(int), 1, fp)
//#define out(fp, pbuf) fwrite(pbuf, sizeof(int), 1, fp)

//FILE *foriginal


void main(void)
{      char nomfichier[21];
      int n;
   int longueur=0;
      FILE * entree;
      printf("Entrez le nom du fichier a lire: ");
      scanf("%20s", nomfichier);
   printf("\n");
      entree=fopen (nomfichier, "r");
      do
      {      fread(&n, sizeof(int), 1, entree);
            if(!feof(entree))
                  printf("%d\n",n);
            longueur++;
      }
      while(!feof(entree));
   printf("La longueur du fichier est: %d", longueur);
      fclose(entree);
}
boisvertAsked:
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.

ozoCommented:
To be consistent I'd suggest either reading with fscanf(entree,"%d",&n);
or writing with fwrite(&n,sizeof(int), 1, entree);
0
imladrisCommented:
The fread is going to read the bytes directly from the file.
That is, it will treat the byte sequence as binary.
So for instance if the file contains 1324, the bytes that rep-
resent that are: 31 33 32 34 (all in hexadecimal). The variable n
will get the first two or four of those (depending on your
machine).

However, you
are intending that the bytes in the file be interpreted as ASCII
characters. This requires a conversion, which scanf can supply.
For instance, fscanf(entree,"%d",&n); This will cause the number
to be converted so that n will correctly contain 1324 (or in
hexadecimal: 52c


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
boisvertAuthor Commented:
Do you mean the rest of the code is correct ?
0
imladrisCommented:
Oooooh, correct is such an absolute concept :)
It's also, of course, dependent on your intentions. I'll give
a commentary. Keep in mind that my french isn't what it used to
be.

The program opens with some includes and defines, most of which
are commented out.

void main(void)
   { char nomfichier[21]; //name of file?
   int n;
      int longueur=0;     // length (of file?)
   FILE * entree;         // file pointer
   printf("Entrez le nom du fichier a lire: "); //enter name of file
   scanf("%20s", nomfichier);                   //to be read?
      printf("\n");
   entree=fopen (nomfichier, "r");    //open file in read mode
   do
   { fread(&n, sizeof(int), 1, entree); //I'll assume you changed this
   if(!feof(entree))     // while not end of file print
   printf("%d\n",n);     // integer quantity read
   longueur++;           // increment number of integers read
   }
   while(!feof(entree));
      printf("La longueur du fichier est: %d", longueur);
   fclose(entree);       // print length of file
   }

Main thing I wonder about, from reading this is the apparent
discrepancy at the end there. The printing says (I think) "length
of file", but what is being printed is the number of integers.
File length is normally quoted in bytes, integers take up 2 to
4 bytes.

0
boisvertAuthor Commented:
It is the number of elements in a file, not really the length.  It's used to break the file in several other files.  It is a part of the polyphase merge sort I try to do.  My biggfest problem is to merge all files in a singles sorted file by using the polyphase merge sort.

Thank you for the comments.
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.