Hey guys. So I basically read in a bunch of data from a file into a data structure. But while I was doing that I included the spaces (which I dont want). So now I want to compare each element to see if it does have a blank space, and if it does i want to ignore it by incrementing some random variable, and if it isnt I want to put it into a new array so I could print the contents of the file without space. I realize that this is bad coding and there must be a different way to do it, any suggestions? This is my code. Im not really good with data structures so thats why im struggling a little bit. *c programming
This is an example input file:
Gates, William ,H,1.89
Jobs, Steven, P,2.56
Brin , Sergey , , 3.89
Page, Lawrence ,, 3.77
Buffett, Warren, E, 3.34
Expected output file:
William H. Gates 1.89
Steven P. Jobs 2.56
Sergey Brin 3.89
Lawrence Page 3.77
Warren E. Buffett 3.34
My code:
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#define TEN 10
#define THREE 3
#define TWENTY 20
#define FIFTY 50
#define ONE_THOU 1000
typedef struct {
char first_name[TEN];
char middle_name[TWENTY];
char last_name[TWENTY];
double users_gpa;
} format_of_file;
int main () {
/* Declarations: File I/O */
FILE *inp, *outp;
char first_file[FIFTY],second_file[FIFTY],str[ONE_THOU],*ptr;
format_of_file organized[ONE_THOU];
int i, count;
double sum,min_value,max_value,average_value;
/* Get users inputs */
printf("Enter the input file name: ");
scanf("%s", first_file);
printf("Enter the output file name: ");
scanf("%s", second_file);
inp = fopen(first_file, "r");
outp = fopen(second_file, "w");
/* Test if both files for validity */
if (inp == NULL) {
perror("Invalid File");
exit(EXIT_FAILURE);
}
if (outp == NULL) {
perror("Invalid File");
exit(EXIT_FAILURE);
}
/* Begin Program execution and get each name/gpa until , and then continue */
count = 0;
while(fgets(str,sizeof(str),inp) != NULL) {
if ((ptr = strtok(str,",\n")) != NULL) {
strcpy(organized[count].last_name,ptr);
}
else {
organized[count].last_name[0]='\0';
}
if ((ptr = strtok(NULL,",\n")) != NULL) {
strcpy(organized[count].first_name,ptr);
}
else {
organized[count].first_name[0]='\0';
}
if ((ptr = strtok(NULL,",\n")) != NULL) {
strcpy(organized[count].middle_name,ptr);
}
else {
organized[count].middle_name[0]='\0';
}
if ((ptr = strtok(NULL,",\n")) != NULL) {
organized[count].users_gpa=atof(ptr);
}
else {
organized[count].users_gpa=0;
}
count++;
/* Check to see if the size of the file is under One thousand*/
if (count > ONE_THOU) {
printf("Error with file size\n");
return 0;
}
}
/* Close first File */
fclose(inp);
/* Find the highest/lowest value/sum */
max_value = organized[0].users_gpa;
min_value = organized[0].users_gpa;
sum = 0;
for (i=0;i < count;i++) {
if (max_value < organized[i].users_gpa) {
max_value = organized[i].users_gpa;
}
if (min_value > organized[i].users_gpa) {
min_value = organized[i].users_gpa;
}
sum = sum + organized[i].users_gpa;
}
/* Find the average value */
average_value = (sum/count);
printf("%d\n", count);
/* Display Results */
fprintf(outp,"Highest gpa: %.2f\n", max_value);
fprintf(outp,"Average gpa: %.2f\n", average_value);
fprintf(outp,"Lowest gpa: %.2f\n\n", min_value);
for (i=0;i < count;i++) {
fprintf(outp,"%s ", organized[i].first_name);
fprintf(outp,"%s. ", organized[i].middle_name);
fprintf(outp,"%s ", organized[i].last_name);
fprintf(outp,"%.2f\n", organized[i].users_gpa);
}
/* Close files */
fclose(outp);
return 0;
}