Thread: read a structure from a file

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    read a structure from a file

    i have the following structure:

    Code:
    typedef struct fagure
    {
            int celule[50];
            char tip;
            int regine[300];
    } fagure;
    and i have a file with the structure

    [aray of int] [char (C or R character)] [aray of int]
    ... many lines

    what would be a smart way to read it ?

    i've done something like this:
    Code:
            fagure temp;
            char a;
            int n=0,m=0;
            temp.tip=0;
            while((fscanf(f,"%c",&a))!=EOF)
            {
                    if((a!=' ')&&(a!='\n'))
                    {
                            if((a=='C')||(a=='R')) temp.tip=a;
                            if(temp.tip==0) temp.celule[n++]=a-'0';
                            if((temp.tip!=0)&&(a!='C')&&(a!='R')) temp.regine[m++]=a-'0';
                    }
                    if(a=='\n')
                    {
                            generare(temp,n,m);
                            temp.tip=0;
                            n=0;
                            m=0;
                    }
            }
            generare(temp,n,m);
    where generare(temp,n,m) is the print function.
    The only problem with this sort of read is that if i have a two digit number it separes it in do numers of one digit.

    Pls help!

    Thank you!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    used fread() fucntion to read a one whole record from the file. to do this u need to open the file in binary mode and work on it. when ever u read a data from a file u are reading in terms of one whole record rather than char or a sinle word or a line. fwrite fucntion to write back to the binary file.

    ssharish2005

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fwrite() and fread() don't generally work between computers that have different byte alignment, edianness, or ordering of structure members.

    You would be better off use fprintf()/fscanf() or putc()/getc() or something if you want the files to be cross-platform compatible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM