Solved

Open and read a file

Posted on 1998-03-13
5
168 Views
Last Modified: 2010-04-02
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
Comment
Question by:boisvert
  • 2
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1257704
To be consistent I'd suggest either reading with fscanf(entree,"%d",&n);
or writing with fwrite(&n,sizeof(int), 1, entree);
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 1257705
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
 

Author Comment

by:boisvert
ID: 1257706
Do you mean the rest of the code is correct ?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1257707
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
 

Author Comment

by:boisvert
ID: 1257708
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question