If the data structure of the file data is known, successive strings can be read from the text file using fscanf(). Each %d in the function call will account for a minus sign and store a negative as required. There must be no gap between the minus and the "attached" integer. Here's something I came up with:
Copy and paste the sample data onto an A: disk, taking care to preserve the spaces and end-of-lines. Name the file "SAMPLE.DAT"... The imaginary data structure consists of five integers (+/-) as declared in my program.
Data is:
32 20 -15 73 -2
70 -11 6 8 -3
44 -6 -5 7 200
45 -1 22 84 1
-9 30 -55 -425 -34
220 465 -1000 -9990 7260
(final item to be followed by newline)
Code:
#include<stdio.h>
#include<stdlib.h>
#define LIMIT 20
struct data{int a, b, c, d, e;};
int main()
{
int i=0, rec_count=0;
FILE *f_ptr;
struct data record[LIMIT]; /* In practice you would need to use malloc
for unknown/large numbers of file records.
This program will work for up to 20 records of 5 integers each. */
if((f_ptr=fopen("A:SAMPLE.DAT","rt"))==NULL)
{
printf("CANNOT OPEN FILE TO READ - PROGRAM STOPPED!\n\n"
"PLEASE ENSURE DISK IS PRESENT IN A:DRIVE!\a");
exit(1);
}
/* Read text file lines until EOF */
while((fscanf(f_ptr,"%d %d %d %d %d\n",&record[i].a,&record[i].b,
&record[i].c,&record[i].d,&record[i].e))!=EOF)
{
rec_count++;
i++;
}
/* Output the file data which is now in the array of structures in RAM
Format the screen output for better presentation */
printf("RECORDS FROM FILE A:SAMPLE.DAT ARE AS FOLLOWS:\n\n");
for(i=0; i<rec_count; i++)
printf("REC %d: %5d %5d %5d %5d %5d\n",i+1, record[i].a, record[i].b,
record[i].c, record[i].d, record[i].e);
printf("\n[END OF RECORDS]\n\n* * * END OF PROGRAM!! * * *\a");
fclose(f_ptr);
return 0;
} /* end of main */
Good luck from MisterBadger!!