From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: file handling Date: Tue, 02 Jun 1998 20:10:43 -0400 Organization: Two pounds of chaos and a pinch of salt. Lines: 82 Message-ID: <35749483.6CE7@cs.com> References: <6l21m1$gpd$1 AT nclient1-gui DOT server DOT virgin DOT net> NNTP-Posting-Host: ppp134.cs.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk paul DOT r DOT thompson AT virgin DOT net wrote: > > i have a data file with 8 lines of integers. > > how do i read these lines in individual variables? > > i know it is a simple problem but it is doing our heads in. > > thanks for any help. > > to save you wasting any time please reply to this within two hours > of posting. Before I start, let me ask you some questions, and point out that this type of general programming question is off-topic for the DJGPP discussion group. I recommend you look in a standard C reference for file-handling and standard input functions, and/or ask on a more appropriate newsgroup such as comp.lang.c. Okay, now for the questions: How much programming experience do you have? Your question leaves it very vague if you are total beginners to C programming or need advice with a complex question. What exactly are the kinds of variables you want to input into? If you are trying to read in a set of values of identical types to operate on as a data set, then you are perfectly describing the role of arrays. Since a program to read values from a file into an array is a trivial part of any C textbook, it's somewhat silly to discuss here. If you want to read data items into the fields of a struct, then there are several possible solutions depending on how much data there is to collect and how many different types of fields there are to read into. When there are many dozens of values, I recommend creating functions to read each distinct data type from the file and return a value of that type (also checking for errors along the way), and then a main driver function that calls the appropriate input function for each data item. You might thus see a hierarchy like the following: int fread_int( FILE *fp ) { int num; if ( fscanf( fp, "%d", &num ) != 1 ) fprintf( stderr, "File empty.\n" ); return num; } /* the rest are defined similarly */ float fread_float( FILE *fp ); char * fread_word( FILE *fp ); void read_data( struct my_data *dat ) { FILE *fp; if ( ( fp = fopen( "file.dat", "r" ) ) == NULL ) { perror( "file.dat" ); exit( EXIT_FAILURE ); } dat->int_element = fread_int( fp ); dat->int_element2 = fread_int( fp ); dat->float_element = fread_float( fp ); dat->string_element = fread_word( fp ); fclose( fp ); return; } Your example, though, makes it sound like what you really want is an array. If you don't understand the role of arrays in C and still want immediate help, please email me privately. hth! -- --------------------------------------------------------------------- | John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com | | Proud owner of what might one | http://www.cs.com/fighteer/ | | day be a spectacular MUD... | ICQ UIN#: 7406319 | | Plan: To make Bill Gates suffer | HEAT User ID: Fighteer | ---------------------------------------------------------------------