Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a data file called superheroes.dat using any text-based editor and enter

ID: 3661410 • Letter: C

Question

Create a data file called superheroes.dat using any text-based editor and enter at least three records storing your superheroes’ name and main superpower. Make sure that each field in the record is separated by a white space.

Using the superheroes.dat file from challenge number one, build another program that uses the fscanf() function for reading each record and printing field information to standard output until the end-of-file is reached. Include an error-handling routine that notifies the user of any system errors and exits the program.

Explanation / Answer

#include <stdio.h> /* Header files*/
#include <stdlib.h>

/* Declaring main method*/
int main ()
{
    FILE *ipFile; /* pointer to read data*/


    char* firstNm[20];

    char* lastNm[20];


/* Reads data from superheroes.dat then points to ipFile*/
    ipFile = fopen("superheroes.dat", "r");

/* ensures file is reading correctly*/
    if(ipFile == NULL)
    {
        printf("Error ");
        system("pause");
        return -1;
    }



/* adds data from ipFile to variables*/
    printf(" Your friends ");


    fscanf(ipFile, "%s%s", firstNm, lastNm);



/*prints from superheroes.dat file*/
    while (!feof(ipFile))
    {


        printf("%s %s ", firstNm, lastNm);


        fscanf(ipFile, "%s%s", firstNm, lastNm);


    }

/*closes the file*/
    fclose(ipFile);
    system("pause");
    return 0;


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote