Open and read a file

Posted on 1998-03-13
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);
      entree=fopen (nomfichier, "r");
      {      fread(&n, sizeof(int), 1, entree);
   printf("La longueur du fichier est: %d", longueur);
Question by:boisvert
  • 2
  • 2
LVL 84

Expert Comment

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

Accepted Solution

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

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


Author Comment

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

Expert Comment

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

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?
   entree=fopen (nomfichier, "r");    //open file in read mode
   { 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
      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.


Author Comment

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.

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now