Solved

Open and read a file

Posted on 1998-03-13
5
166 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

746 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

12 Experts available now in Live!

Get 1:1 Help Now