array of structures

This is a discussion on array of structures within the C Programming forums, part of the General Programming Boards category; I do not know how to make a normal structure variable to an array structure that would work. example. Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    array of structures

    I do not know how to make a normal structure variable to an array structure that would work.
    example.
    Code:
    struct info { int sample; char name[10]; int number;};
    
    main()
    {
           info data[7]; //this means i got an array of 7 structures ok
           FILE *myfile ;      
    
           data[2].sample=1;
           data[2].name="henry"; //by the way, this doesnt work for some reason. cant initialize a                             string.
    
           //and now i want to write
          myfile=fopen("c:\\prog.c", "ab");
     
           fwrite(&data[2], sizeof(data), 7, myfile);
           fclose(myfile);
    }
    and the prog messes up big time.
    I dont know properly how to use structure of arrays in terms of writing and reading from a file. Can someone just write a sample prog?

    Say i want to initialize all the int sample; of each structure to 1 using a for loop. How would I write it?

    and wht is the crorect way to pass an array of structure to a function?

    thank you.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    use strcpy function to copy a string

    Code:
    data[2].name="henry";
    
    to
    
    strcpy(data[2].name,"henry");
    open the file in the write mode or append mode

    Code:
     myfile=fopen("c:\\prog.c", "a+");
    Say i want to initialize all the int sample; of each structure to 1 using a for loop. How would I write it?
    Code:
    for(i=0;i<7;i++)
        data[i].sample = 1;
    and wht is the crorect way to pass an array of structure to a function?
    Code:
    function foo(struct info *); // function prototype
    
    struct info temp[10];
    
    foo(temp);  // this is how u send
    ssharish2005
    Last edited by ssharish2005; 03-25-2007 at 11:11 AM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    open the file in the write mode or append mode

    Code:
     myfile=fopen("c:\\prog.c", "a+");
    shouldnt it be "a+b" if i am using fwrite and fread or it doesnt matter?


    Code:
    for(i=0;i<7;i++)
    {  data[i].sample = 1;
         fwrite(&data[i], sizeof(data), 7, myfile) 
    }
    after this code, within the for loop would I write the fwrite function and is the fwrite syntax ok?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    a+b
    This means that u are trying to open the binary file in read and append mode. If you are writting the data in bianry mode on to the file.


    Code:
    fwrite(&data[i], sizeof(data), 7, myfile)
    If you are trying to write to file each record at a time.

    Then it should be
    Code:
    fwrite(&data[i], sizeof(data[i]), 1, myfile)
    Or if your are trying to write the whole record at a time, then it should be

    Code:
    fwrite(data, sizeof(data[0]), 7, myfile)
    this shuld be outside the loop

    ssharish2005
    Last edited by ssharish2005; 03-25-2007 at 12:34 PM.

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    1. First of all, if you don't typedef struct info as info, you need to specify struct when declaring a variable of type "struct info":

    Code:
    typedef struct
    {
        int sample;
        char name[10];
        int number;
    } info;
    
    and then you can use:
    info data[7];
    OR

    Code:
    struct info
    {
        int sample,
        char name[10],
        int number;
    };
    
    and then you can use:
    struct info data[7];
    2. In your code you specify "7" as third argument for fwrite, or this is the number of items you write to the file and it should be 1 if you write them one by one. The 2 following solutions work:
    Code:
    int i;
    for (i = 0; i < 7; i++)
        fwrite(&(data[i]), sizeof(struct info), 1, myfile);
    OR

    Code:
    fwrite(data, sizeof(struct info), 7, myfile);
    3. The strcpy error has already been mentioned.
    Last edited by KONI; 03-25-2007 at 12:28 PM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Code:
    void showallseats(struct INFO airline[])
    { FILE *myfile;
    myfile=fopen("C:\\test3.DAT", "rb");
    	fread(&airline, sizeof(airline[0]), 12, myfile);
    	printf("seatnum %i",airline[4].seatnum);
    	fclose(myfile);
    }
    
    void initialize(struct INFO airline[])
    { int i;
    	FILE *myfile;
    
    	myfile=fopen("C:\\test3.DAT", "ab");
    
    	for(i=0;i<12; i++)
    	{
    		 airline[i].seatnum=i+1;
    
    	}
    	fwrite(&airline, sizeof(airline[0]), 12, myfile);
    	fflush(stdin);
    	fclose(myfile);
    }
    These are my two functions. One should initialize all seatnums to one above the array number.

    and one should read and printf airline[4].seatnum as 5 but the program just quits. funtion showallseats is accessed by a switch. and initialize is called before it. so theoretically i should be getting 5 as teh seat num. what is going wrong with it?

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    1. struct INFO is not the same as "struct info", C is case-sensitive. Please pay attention to that.

    2. If you specify an array like the following:
    Code:
    struct info airline[100];
    then the address of the first element is either:

    airline (and NOT &airline)
    OR
    &(airline[0])

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    thank you so much my prog is functioning much better. but there is another problem

    Code:
    void assign(INFO airline[])
    {
    	int seat, arrnum;
    	FILE *myfile;
    
    	myfile=fopen("C:\\test6.DAT", "a+b");
    
    	printf("\nSeat number: ");
    	scanf("%i", &seat);
    
    	arrnum=seat-1;
    
    	fflush(stdin);
    	airline[arrnum].seatnum=seat;
    	airline[arrnum].marker=1;
    
    	printf("\nFirst name:");
    	gets(airline[arrnum].firstname);
    
    	printf("\nLast name:");
    	gets(airline[arrnum].lastname);
    
    	fwrite(airline, sizeof(airline[0]), 12, myfile);
    
    	fclose(myfile);
    
    
    }
    
    void showallseats(INFO airline[])
    {
    	int i;
    	FILE *myfile;
    
    	myfile=fopen("C:\\test6.DAT", "rb");
    	printf("\nSeat\tFirst name\t\tLastname");
    
    	fread(airline, sizeof(airline[0]), 12, myfile);
    	for(i=0; i<12; i++)
       {
    		printf("\n%i\t",airline[i].seatnum);
    
    		if(airline[i].marker==1)
    	  	{
    			printf("%s\t",airline[i].firstname);
    			printf("%s",airline[i].lastname);
    	  	}
    	}
    
    	fclose(myfile);
    	getch();
       
    }
    
    void initialize(INFO airline[])
    {
    	int i;
    	FILE *myfile;
    
    	myfile=fopen("C:\\test6.DAT", "a+b");
    
    	for(i=0;i<12; i++)
    	{
    		 airline[i].seatnum=i+1;
    	}
    
    	fwrite(airline, sizeof(airline[0]), 12, myfile);
    	fclose(myfile);
    //022233068
    }
    the seatnums are being written and read properly however, the strings that I am writing are not being retrieved from the file in function showallseats. even though I am setting the marker as 1 of the structure. even if i use new files. the problem persists.

    why isnt the function assign writing the strings to the file?

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    There are several things you need to check:

    1. It seems to me that you don't want to append to the file but rather overwrite the old content. In that case, open your files with "wb" and not "a+b". Otherwise, each call to assign will write 12 seats into the file but showallseats will only read the first 12.

    2. Did you check if assign has the correct values for arrnum, firstname and lastname before writing them to the file ?

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also you may want to pass array size as a parameter instead of using hardcoded values

    and replace gets with fgets calls as FAQ suggests
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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