Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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);
}
0
boisvert
Asked:
boisvert
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now