Question

C Program:Write a program that will read names, ids, dept names, and cgpа of some students from a file and will show the results. Consi

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here we take file name as input_csv.txt. It is considered as text file.

here on output screen one line gives one record of individual student.

/* PROGRAM */

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
   char name[50];
   int id;
   char dept[20];
   double cgpa;
  
}student;
  
int main()
{
FILE *fptr;
student std[100];
char s[80];
char rec[100][100];
double rec1[100][100];
char filename[100];
double d;
string s1;
   int i,j,n,l,c=0,k=0,p,x,m=0,r,h=0,u=0;
c=0;
   i=0;
   k=0;
fptr = fopen("input_csv.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
  
while(fgets(s,1000,fptr)!=NULL)
{
    m=0;
    u=0;
    m=0;
    h=0;
    s1="";
    k=0;
    c=0;
   l=strlen(s);
   i=0;
   
    while(i<l)
   {
       if(s[i]==',')
       {
       while(k<c)
       {
       s1=s1+s[k];
       k++;  
       }  
  
   if(m==1)
       {
           rec1[u][0]=atoi(s1.c_str());
           u++;
       }
   else    strcpy(rec[m], s1.c_str());
  
   m++;
            
      
   p=0;
  
       i++;
       k++;
       c++;
       s1="";
      
       }
       else if(i==l-1)
       {
       while(k<=c)
       {
       s1=s1+s[k];
       k++;  
       }
         
         
   if(m==3)
       {
      
       rec1[u][0]=atof(s1.c_str());
           u++;
       }
   else    strcpy(rec[m], s1.c_str());
  
   m++;
  
   p=0;
       i++;
       k++;
       c++;
       s1='\0';
       }  
       else
       {
       i++;
       c++;
      
       }
   }
  
strcpy(std[h].name,rec[0]);
std[h].id=rec1[0][0];
strcpy(std[h].dept,rec[2]);
std[h].cgpa=rec1[1][0];

printf("%s %d %s %0.2f\n",std[h].name,std[h].id,std[h].dept,std[h].cgpa);
  
   
}

fclose(fptr);

}

SCREEN SHOT

#include <stdio.h> #include <stdlib.h> #include<string.h> #include<iostream> #include <bits/stdc++.h> using namespace std; ty

while(fgets(5,1000, fptr)!=NULL) m= ; u=0; ni=0; h=0; $1= k= ; C= ; 1=strlen(s); i=0; while(i<l) if(s[i]==,) while(k<c) s

p=0; i++; s1=; else if (i==1-1) while(k<=c) s1=51+s[k]; k++; if (m==3) rec1[u][@]=atof(s1.c_str(); ++; else strcpy(rec[m],

else i++; C++; strcpy(std[h]-name, rec[@]); std[h].id=rec1[@][@]; strcpy(std[h].dept,rec[2]); std[h] .cgpa-rec1[1][@]; printf

when file is

LOX input_csv - Notepad File Edit Format View Help David Smith, 21, Computer Science, 3.98 John Smith, 11, Finance, 3.45

OUTPUT

- 10 x C:\Users\cse\Desktop\CHEGG\file_struct.exe David Smith 21 Computer Science 3.98 John Smith 11 Finance 3.45 Process exi

when file is:

- OX input_csv - Notepad File Edit Format View Help David Smith, 21, Computer Science, 3.98 John Smith, 11, Finance, 3.45 Dev

Output

LOX C:\Users\cse\Desktop\CHEGG\file_struct.exe David Smith 21 Computer Science 3.98 John Smith 11 Finance 3.45 Devid Morison

Add a comment
Know the answer?
Add Answer to:
C Program: Write a program that will read names, ids, dept names, and cgpа of some...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • In C program Consider a file that contains employee records, called "empstat.txt." The data for each...

    In C program Consider a file that contains employee records, called "empstat.txt." The data for each employee consist of the employee’s last name (up to 20 characters), employee id number (up to 11 characters), gross pay for the week (double), taxes deducted (double) for the week, and net pay (double) for the week. Create a struct to hold the employee information. Write a void function called writeEmpFile that prompts the user to enter last name, id number, gross pay and...

  • In C. I want to read in the data of a text file into an array...

    In C. I want to read in the data of a text file into an array of dynamically allocated structs. The file is read as a command-line argument The text file format is: <number of classes in this file> <department name of class 1>:<number of class 1>:<location of class 1> <department name of class 2>:<number of class 2>:<location of class 2> ... Assume that no piece of information in the file contains a colon: the colons are only used as...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • c++ The program will be recieved from the user as input name of an input file...

    c++ The program will be recieved from the user as input name of an input file and and output.file. then read in a list of name, id# and score from input file (inputFile.txt) and initilized the array of struct. find the student with the higher score and output the students info in the output file obtain the sum of all score and output the resurl in output file. prompt the user for a name to search for, when it find...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT