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

How would I make this code write the ouput into a text file instead of just disp

ID: 3915304 • Letter: H

Question

How would I make this code write the ouput into a text file instead of just displaying it into the console.

#include<stdio.h>
#include<stdlib.h>
#include<Nfl.h>
#define MAX 5


void printData(Nfl d[], int n)
{
for(int i = 0; i < n; i++)
{
printf(" Name: %s", d[i].name);
printf(" Colors: %s", d[i].color);
printf(" Age: %i", d[i].age);
printf(" Championships: %i", d[i].championships);
printf(" Conference Championships: %i ", d[i].conferenceChampionships);
}
}

int main()
{
Nfl d[MAX];
int n = 0;
FILE *f = fopen("Nfl.dat", "rb");
if(f == NULL)
printf("Cannot open file");
else
{
int i = 0;
while(fread(&d[i], sizeof(Nfl), 1, f))
i++;
n = i;
fclose(f);
}
printData(d, n);
return 0;
}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

#include<Nfl.h>

#define MAX 5

/* Printdata() function writes ouput into a text file & also displays it into the console */

void printData(Nfl d[], int n)

{

                FILE *f = fopen("Nfltext.dat", "w");   /* Open text file for writing */

               

    if(f == NULL)

    printf("Cannot open file for writing");

                else

                for(int i = 0; i < n; i++)

                {

                                /* displays the ouput into the console */

                    printf(" Name: %s", d[i].name);

                                printf(" Colors: %s", d[i].color);

                                printf(" Age: %i", d[i].age);

                                printf(" Championships: %i", d[i].championships);

                                printf(" Conference Championships: %i ", d[i].conferenceChampionships);

                               

                                /* fprintf() function writes the data into the text file */

                                fprintf(f, "%s %s %i %i %i ", d[i].name, d[i].color, d[i].age, d[i].championships, d[i].conferenceChampionships);

                }

                fclose(f);    /* closing text file */

}

int main()

{

                Nfl d[MAX];

                int n = 0;

               

                FILE *f = fopen("Nfl.dat", "rb"); /* open binary file for reading */

                if(f == NULL)

                printf("Cannot open file");

                else

                {

                                /* reading & counting number of records in file */

                                int i = 0;

                                while(fread(&d[i], sizeof(Nfl), 1, f))

                                                i++;

                                               

                                n = i;

                                fclose(f);   /* Closing Binary file */

                }

                printData(d, n);   /* calling function to write data into text file & displays it on screen */

                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