Question

In: Computer Science

Write a C program that will read different data types from the following file and store...

Write a C program that will read different data types from the following file and store it in the array of structures.

Given file: (This file have more than 1000 lines of similar data):

time latitude longitude depth mag magType nst gap dmin
2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021
2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757
2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205
2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049
2020-10-19T22:42:26.224Z 54.4198 -159.9943 18.7 2.9 ml

Create a structure like below and make an array of structures in the main C file and store the data based on their data types.

struct data

{

   char time[100];

   float latitude;

   float longitude;

   float depth;

   float mag;

   char magType[5];

   char nst[5];

   int gap;

   float dmin;

};

Solutions

Expert Solution

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
struct data{
  char time[100];
  float latitude;
  float longitude;
  float depth;
  float mag;
  char magType[5];
  char nst[5];
  int gap;
  float dmin;
};
int main(){
  struct data records[1000];
  struct data temp;
char str[256];
int result,i=0;
FILE* f = fopen("/home/user/Downloads/file.csv", "r");// for a csv file   
result = fscanf(f, "%255[^;\n]", str);
result = fscanf(f, "%255[^;\n]", str);// read data from the user
  
while(result != EOF){
if(result == 0){
result = fscanf(f, "%*c");
}
else{
char *token = strtok(str,",");
         int j = 0;
         
         while(token != NULL) {
        if(j == 0) strcpy(records[i].time, token);
        else if(j == 1) records[i].latitude = atof(token);
        else if(j == 2) records[i].longitude = atof(token);
        else if(j == 3) records[i].depth = atof(token);
        else if(j == 4) records[i].mag = atof(token);
        else if(j == 5) strcpy(records[i].magType, token);
        else if(j == 6) strcpy(records[i].nst, token);
        else if(j == 7) records[i].gap = atoll(token);
        else records[i].dmin = atof(token);
        
        token = strtok(NULL, ",");
        j++;
         }
         
         i++;
         printf("\n");
}
result = fscanf(f, "%255[^;\n]", str);
}
  for(int x=0; x<i; x++){
     for(int y=0; y<i-1; y++){
        if(records[y].latitude > records[y+1].latitude){
           temp = records[y];
           records[y] = records[y+1];
           records[y+1] = temp;
        }
     }
  }
return 0;
}


Explanation:

Just create a file named file.csv and the program will read the text in it.

This will make the following line of code find your csv file..FILE* f = fopen("/home/user/Downloads/file.csv", "r");


Related Solutions

Write a c++ program that does the following, read temperatures from a file name temp.txt into...
Write a c++ program that does the following, read temperatures from a file name temp.txt into an array, and after reading all the temperatures, output the following information: the average temperature, the minimum temperature, and the total number of temperatures read. Thank you!
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of...
Question6: Write a program to read the file 202010mid.txt, store the content in a list of tuples, then print the list. [6 marks] Question7: Write Python code to do the following operations using request library. [12 marks] Check if the following webpage is available or not: https://edition.cnn.com/news.html [4 marks] Send a get request to the following webpage and show the result: http://api.open-notify.org/iss-pass.json [4 marks] The webpage from part 2 is expecting some parameters as shown from the results. Now create...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT