Structure file input

This is a discussion on Structure file input within the C Programming forums, part of the General Programming Boards category; I am having problems reading in lines from a .bat file or text file into an array of structures and ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Structure file input

    I am having problems reading in lines from a .bat file or text file into an array of structures and outputing the array of structures to a different file. It is reading garbage for some fields of the array of structures. How do I properly read in lines of data from a file into an array of structures?

    This is some of my code:
    Code:
    #include <stdio.h>
    
    #define MAXPTS 20
    #define MAXNAME 10
    
    struct coord
    {
       int x;
       int y;
    };
    
    struct navtag
    {
       int id;
       char name[MAXNAME];
       struct coord pts;
       float depth;
    };
    
    void getData(FILE *ifp, struct navtag navrec[]);
    void printData(FILE *ofp, struct navtag navrec[]);
    
    int main()
    {
       FILE *ifp, *ofp;
       int n;
    
       struct navtag navrec[MAXPTS];
       getData(ifp,navrec);
       printData(ofp,navrec);
    
    
    
    	return 0;
    }
    
    void getData(FILE *ifp, struct navtag navrec[])
    {
       int x;
    	ifp=fopen("c:\\lab4.dat", "r");
    	for(x=0; x<MAXPTS; x++)
       {
    	fscanf(ifp,"%d", &navrec[x].id);
    	fscanf(ifp,"%s", &navrec[x].name);
    	fscanf(ifp,"%d", &navrec[x].pts.x);
    	fscanf(ifp,"%d", &navrec[x].pts.y);
    	fscanf(ifp,"%f\n", &navrec[x].depth);
       }
    
       fclose(ifp);
    
       return;
    }
    
    
    void printData(FILE *ofp, struct navtag navrec[])
    {
    int x;
    ofp=fopen("c:\\lab4.out", "w");
    for(x=0; x<MAXPTS; x++)
    {
    	fprintf(ofp,"point = %d\n", navrec[x].id);
    	fprintf(ofp,"      name is %s\n", navrec[x].name);
    		fprintf(ofp,"      location is x = %d, y = %d\n ", navrec[x].pts.x,navrec[x].pts.y);
    	fprintf(ofp,"      depth = %f\n", navrec[x].depth);
    }
    fclose(ofp);
    return;
    }
    
    //here is the lab4.dat file
    100 Pt. Jones 10 -70 6.27
    105 Pt. Smith 5 28 3.23
    110 Pt. Brown -3 -17 10.48
    115 Pt. Ruiz 42 -5 8.64
    120 Pt. Alman -12 35 14.76
    125 Pt. Aloha 23 8 20.19
    130 Pt. Club 18 -53 18.67
    135 Pt. Marks 13 45 17.55
    140 Pt. Hart 33 -21 1.47
    145 Pt. Jacob -14 17 5.74
    150 Pt. Miser 25 -36 7.89
    155 Pt. Mixup 88 56 6.54
    160 Pt. Fling -7 64 2.38
    165 Pt. Blank -2 -32 9.99
    170 Pt. Hope 9 -2 14.34
    175 Pt. Bass 34 25 19.88
    180 Pt. Slim 40 -12 8.77
    185 Pt. Slick -6 16 13.41
    190 Pt. Lock -1 38 5.65
    195 Pt. Easy 0 10 3.00
    //Here is the output I get to the file lab4.out
    point = 100
          name is Pt.
          location is x = -1184335880, y = -34821
           depth = -199679.984375
    point = -815630342
          name is Jones
          location is x = 10, y = -70
           depth = 6.270000
    point = 105
          name is Pt.
          location is x = -784203776, y = -2055995159
           depth = 0.000000
    point = -1005845735
          name is Smith
          location is x = 5, y = 28
           depth = 3.230000
    point = 110
          name is Pt.
          location is x = 1386151950, y = 2
           depth = 0.000000
    point = 1990197248
          name is Brown
          location is x = -3, y = -17
           depth = 10.480000
    point = 115
          name is Pt.
          location is x = -264306625, y = 889485303
           depth = 0.000000
    point = -26738688
          name is Ruiz
          location is x = 42, y = -5
           depth = 8.640000
    point = 120
          name is Pt.
          location is x = 786434, y = 1835008
           depth = 0.000000
    point = 258211840
          name is Alman
          location is x = -12, y = 35
           depth = 14.760000
    point = 125
          name is Pt.
          location is x = 721122, y = 0
           depth = -8704265901225330058000000000.000000
    point = 27
          name is Aloha
          location is x = 23, y = 8
           depth = 20.190001
    point = 130
          name is Pt.
          location is x = 1027110901, y = 1703956
           depth = -167482727468899400000000000000000000000.0
    point = 109838354
          name is Club
          location is x = 18, y = -53
           depth = 18.670000
    point = 135
          name is Pt.
          location is x = 434862054, y = 30694
           depth = -223310303291865866600000000000000000000.0
    point = -1623719918
          name is Marks
          location is x = 13, y = 45
           depth = 17.549999
    point = 140
          name is Pt.
          location is x = 131072, y = -4718592
           depth = 859471996918968111600000.000000
    point = 131136
          name is Hart
          location is x = 33, y = -21
           depth = 1.470000
    point = 145
          name is Pt.
          location is x = -8388544, y = 1097072658
           depth = 0.000000
    point = 0
          name is Jacob
          location is x = -14, y = 17
           depth = 5.740000
    &#91;code]&#91;/code]tagged by Salem

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A few remarks:

    The filepointers in main() are unnecessary, move them to the functions which use the filepointers, main() does not need them. Especially since you're closing the files in the functions which use the pointers.

    In the functions which use the filepointers, you should add a check which checks if the file is opened. If the file could not be opened, then the filepointer has value NULL after fopen.

    Using fscanf is not very safe. I'd recommend to use fgets() to read a line from the into a buffer and then use this buffer to fill in the structure.

    The same counts for fprintf. Fill a buffer and write this buffer to the file using fputs().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  5. open input file twice?
    By keithmolo in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2003, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21