Assignment: read each line from a text file and save each occurrence of 4 lines
ID: 3624804 • Letter: A
Question
Assignment: read each line from a text file and save each occurrence of 4 lines in in individual array.example:
text file contains:
hello
1111222233334444
423.23
12000
sandy cannon
0320117822663344
540.32
1000.00
array1[0] = hello
array1[1] = sandy cannon
array2[0] = 1111222233334444
array2[1] = 0320117822663344
array3[0] = 423.23
array3[1] = 540.32
array4[0] = 12000.00
array4[1] = 1000.00
MY PROGRAM:
#include <stdio.h>
int main(void)
{
int mark = 0; //initialization
int option;
char *cardname[50];
long int cardnum[50]; //needs to take 16 numbers per array number
float cardbal[50];
float cardlim[50];
FILE *dfile;
int x = 1;
char input[50];
while(x > 0)
{
printf(" Input file name (include extension): ");
scanf("%s", input);
dfile = fopen(input, "r");
if(dfile != NULL)
{
while(fgetc(dfile) != EOF)
{
fscanf(dfile,"%s %d %f %f", cardname[mark], &cardnum[mark], &cardbal[mark], &cardlim[mark]);
printf(" %s %d $%.2f $%.2f ", cardname[mark], cardnum[mark], cardbal[mark], cardlim[mark]);
++mark;
}
break;
}
else
{
printf(" Invalid File. Try Again ");
}
}
}
why does it crash? Please make it work
Explanation / Answer
fgetc will catch only one character.
u have to use fgets which can read one line at a time.
that too how u can read 4 lines at a time ?
u cant read 4 lines at a time and u cant scan them in single line.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.