简体   繁体   中英

C program file read, reformat, write

I'm having trouble in the while loop on how to get the string and only displaying the name major and GPA as a list. Also I need help in the fgets function because each line is different every three lines so how would I display them in a list. What I would I put in the while loop to make this work?

#include <stdio.h>

int main(void)
{

    FILE *cfPtr;
    FILE *ofp;
    char name[20];
    char major[3];
    double gpa;
    ofp = fopen("outputFile.txt","w");
    fprintf(ofp,"Name\tMajor\tGPA\n");
    if((cfPtr = fopen("inputFile.txt","r")) == NULL)
    {
        printf("This file could not be opened\n");
    }
    else
    {
        while (!feof(cfPtr))
        {   

            fgets(name, 20, cfPtr );
            //How to read the string and only display
            //the name major and gpa?
            //fprintf(ofp,"%s\t%s\t%f\n", name, major, gpa);

        }

    }
    fclose(cfPtr);
    fclose(ofp);    
}

Input file is:

Name: John Milton
Major: EE
GPA: 3.98
Name: Karl Lewis
Major: CS
GPA: 3.6
Name: Homer Simpson
Major: CE
GPA: 4.0

and I need the output file as:

Name:           Major:  GPA:
John Milton     EE      3.98
Karl Lewis      CS      3.6
Homer Simpson   CE      4.0

Hi,you can do that like this.If you must use fegts.

You should declare these char arrays,

char name[20];//enough long
char major[10];//enough long
char gpa[10];//enough long
char name_tag[7];//According to the "Name: "
char major_tag[8];//According to the "Major: "
char gpa_tag[6];//According to the "GPA: "

And the while loop,

while (1)
{   
    if(fgets(name_tag,7,cfPtr) == NULL)
        break;
    if (feof(cfPtr))
    {
         break;
    }
    if(fgets(name, 20, cfPtr ) == NULL)
        break;
    if (name[strlen(name) - 1] == '\n')
    {
        name[strlen(name) - 1] = '\0';//remove '\n'
    }
    if(fgets(major_tag,8,cfPtr) == NULL)
        break;
    if(fgets(major,10,cfPtr) == NULL)
        break;
    if (major[strlen(major) - 1] == '\n')
    {
        major[strlen(major) - 1] = '\0';
    }
    if(fgets(gpa_tag,6,cfPtr) == NULL)
        break;
    if(fgets(gpa,10,cfPtr) == NULL)
        break;
    if (gpa[strlen(gpa) - 1] == '\n')
    {
        gpa[strlen(gpa) - 1] = '\0';
    }

    fprintf(ofp,"%s\t%s\t%s\n", name, major, gpa);
}

a sample code:

#include <stdio.h>

typedef struct data {
    char name[20];
    char major[3];
    double gpa;
} Data;

Data *fgets_data(Data *aData, FILE *fp){
    if( fscanf(fp, "Name: %19[^\n]%*c", aData->name)==1 &&
        fscanf(fp, "Major: %2s%*c", aData->major)==1 &&
        fscanf(fp, "GPA: %lf%*c", &(aData->gpa))==1 )
        return aData;

    return NULL;//fail
}

void fputs_data(Data *aData, FILE *fp){
    if(aData == NULL) //print caption
        fprintf(fp, "%-20s%-8s%s\n",
        "Name:", "Major:", "GPA:");
    else
        fprintf(fp, "%-20s%-8s%g\n",
            aData->name, aData->major, aData->gpa);
}

int main(void){
    FILE *cfPtr;
    Data aData;

    if((cfPtr = fopen("inputFile.txt","r")) == NULL){
        fprintf(stderr, "This file could not be opened\n");
    } else {
        FILE *ofp = fopen("outputFile.txt","w");

        fputs_data(NULL, ofp);//print fileld name
        while (fgets_data(&aData, cfPtr)!=NULL)
            fputs_data(&aData, ofp);
        fclose(ofp);
        fclose(cfPtr);
    }
    return 0;
}

I won't write code for you, but I will help you structure your solution somewhat.

If you look at your input file and expected output format something like this probably works:

open input and output files
while there are more lines in the input file
   name = readline(); major = readline(); gpa = readline();
   writeLine(name, major, gpa);
close files

Now it's just figuring out the code for each of those steps one at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM