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

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question

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<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;  
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Header Files
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning