Write a program that expands on the program below (i.e. reading data of each record in the file and storing it into data structure). The expansion involves writing all records to a file (use fprintf, call the name of the file csv). The displayed data must be accessed (and thus written) from data structure (i.e. array of structs). The format written to the file should be one line per record, with each field separated by a comma (‘,’) program should then output the number of lines in the file.test the program by creating a text file with at least 10 lines of text (of any composition). Use notepad++ to create the data file. #include #include //Structure struct Person{ char name[10]; int age; float wage; }; //Main int main (int argc, char **argv) { FILE* f; if (argc != 2) { printf("No filename in the argument"); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { printf("The file cannot be opened successfully: %s\n",argv[1]); return 1; } printf("The file has been opened successfully\n\n"); struct Person p[10]; int lines=0; int i=0; while( fscanf(f, "%s %d %f\n", p[i].name, &p[i].age, &p[i].wage) != EOF ) { lines++; i++; } i=0; while(i
Write a
program should then output the number of lines in the file.test the program by creating a text file with at least 10 lines of text (of any composition). Use notepad++ to create the data file.
#include<stdio.h>
#include<stdlib.h>
//Structure
struct Person{
char name[10];
int age;
float wage;
};
//Main
int main (int argc, char **argv)
{
FILE* f;
if (argc != 2)
{
printf("No filename in the argument");
return 1;
}
f = fopen(argv[1], "r");
if (f == NULL) {
printf("The file cannot be opened successfully: %s\n",argv[1]);
return 1;
}
printf("The file has been opened successfully\n\n");
struct Person p[10];
int lines=0;
int i=0;
while( fscanf(f, "%s %d %f\n", p[i].name, &p[i].age, &p[i].wage) != EOF )
{
lines++;
i++;
}
i=0;
while(i<lines)
{
printf("Name: %s\t\Age: %d\t\Wage: %.2f\n", p[i].name, p[i].age ,p[i].wage);
i++;
}
printf("Number of Records: %d",lines);
fclose(f);
return 0;
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images









