Reading a file into a structure array

This is a discussion on Reading a file into a structure array within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that reads a file into an array of structs. I decided to see ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Question Reading a file into a structure array

    I am trying to write a program that reads a file into an array of structs. I decided to see if this aspect worked before expanding on the program. Here's the code:

    Code:
    #include<stdio.h>
    
    struct analg {
    	char word[6];
    	char signature[6];
    };
    
    
    int main() {
    	analg h[106];
    	FILE *fp;
    	int i;
    	
    	fp = fopen("list.dat", "r");
    
    	if(fp == NULL) {
    		printf("CANNOT OPEN FILE.  PROGRAM EXITING");
    		return 1;
    	}
    	else {
    	
    		for(i=0; i<106; i++) {
    			while(fgets(h[i].word, 6, fp){
    				printf("%s", h[i].word);
    			}
    		}
    	}
    
    	fclose(fp);
    
    
    	return 0;
    }
    list.dat is just a file that has a list of words, like

    throw
    timer
    tones
    tower
    trace
    trade
    tread

    Entered exactly like that. When I try to compile the program, I get several errors, and I was wondering what I was doing wrong.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You're missing a right parenthesis on your while loop. In addition, since this is C and not C++ do:
    Code:
    struct analg {
      char word[6];
      char signature[6];
    };
    
    int main() {
      struct analg h[106];
    }
    Or...
    Code:
    typedef struct {
      char word[6];
      char signature[6];
    } analg;
    
    int main() {
      analg h[106];
    }

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    525
    Code:
    for(i=0; i<106; i++) {
       while(fgets(h[i].word, 6, fp)) /* was missing */
       {
          printf("%s", h[i].word);
       }
    }
    All this will do is give you the last line in ever member of h[]. Because you read in and overwrite h[i] through the entire file...i dont know what the for loop is for?

    Something like this... maybe:

    Code:
    for (i=0;fgets(h[i].word,6,fp) && i<106;i++)
       printf("%s",h[i].word);
    Last edited by nonpuz; 05-02-2004 at 12:40 AM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    The for loop was simply to populate the array with the words in list.dat. The list.dat I have has about 106 words that I have to store in every location of the array. I had the printf statement in the loop to check what went into the array location. Once I knew the array had what I wanted in it, I was going to get rid of the printf statement entirely.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 10:25 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Reading an array in a while not end of file loop
    By npg316 in forum C Programming
    Replies: 5
    Last Post: 10-18-2004, 01:25 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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