[comp.lang.c] read/write to file

amigo@milton.u.washington.edu (The Friend) (12/03/90)

s




     I'm looking for examples on how to create files - putting sequential data
in them, this includes a string and a integer. Specifically its
a name and age list. What I've brewed up so far hasn't
been the greatest of success, so I'm looking for better ways to go about it.

     First round for me using files, so this explains my problem..

     So I have 10 names & ages down in two arrays:
     
     static char names[10][]={........};
     static int ages[10][]={.........};

     Given I define pointes and the like:
     FILE *fp    
     fp=fopen("test_file","w+"); /* w+ so it reads later on, using rewind()
                                                       or some such */

     Next I have a loop set up to dump the array to the file, but
     thats where it gets complicated:
     
     fprintf(fp,"%s%d", names[10][], ages[10][]);

wallace@ynotme.enet.dec.com (Ray Wallace) (12/04/90)

In article <12167@milton.u.washington.edu>, amigo@milton.u.washington.edu (The Friend) writes...
>     I'm looking for examples on how to create files - putting sequential data

>     So I have 10 names & ages down in two arrays:
>     static int ages[10][]={.........};
This should be a single dimension array.
      static int ages[10]={.........};

>     fprintf(fp,"%s%d", names[10][], ages[10][]);
Try -
  for( next = 0; next < 10; ++next )
	fprintf( fp, "%s%d\n", names[next], ages[next] );

---
Ray Wallace		
		(INTERNET,UUCP) wallace@oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet@decwrl.dec.com
---