In C: Using the friends.dat file from challenge number one, build another progra
ID: 654091 • Letter: I
Question
In C: Using the friends.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.
This is my code from challenge number 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Open Main
main()
{
//Create file pointers
FILE *fNew;
//Define variables
char fName[20];
char lName[20];
const char EXIT[20]="EXIT";
//Open file for writing
fNew=fopen("friends.dat", "w");
//Test for ability to open file
if (fNew== NULL)
{
printf("File can not open");
exit(EXIT_FAILURE);
}
else
{
//Ask for Names
printf(" Enter your friends first name and last name separtated by spaces (Enter ""EXIT"" to stop) ");
//Assigns variables
scanf("%s%s", fName, lName);
//Test for Exit & loop for more names
while (strcmp(fName,EXIT) !=0)
{
fprintf(fNew,fName);
fprintf(fNew, " ");
fprintf(fNew, lName);
fprintf(fNew, " ");
printf(" Enter your friends first name and last name separtated by spaces (Enter ""EXIT NOW"" to stop) ");
scanf("%s%s", fName, lName);
}
printf(" Data has been uploaded successfully!");
//Close file for writing
fclose(fNew);
//Open file for reading
fNew=fopen("friends.dat","r");
//Test for ability to open file
if(fNew == NULL)
{
printf(" The file can not be opened...");
exit(EXIT_FAILURE);
}
printf("The file has opened successfully!");
printf("The data in the file is:");
int status=fscanf(fNew,"%s", fName);
//Read txt file
while(status != EOF)
{
fscanf(fNew, "%s", lName);
printf("%s %s ",fName,lName);
status=fscanf(fNew,"%s",fName);
}
//Close file
fclose(fNew);
return 0;
}
}
Explanation / Answer
//**********************************************************************************
// Library Includes
//**********************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//**********************************************************************************
// Declaration of "main"
//**********************************************************************************
void main ()
{
//**********************************************************************************
// Assign a file pointer to read data
//**********************************************************************************
FILE *pntRead;
char charFirstName[20];
char charLastName[20];
clrscr();
//**********************************************************************************
// Read data from friends.dat and assign to pointer
//**********************************************************************************
pntRead = fopen("friends.dat", "r");
//**********************************************************************************
// Check that file is read correctly
//**********************************************************************************
if (pntRead == NULL)
goto ErrorHandle;
//**********************************************************************************
// Assign data from pointer to variables
//**********************************************************************************
else
printf("nYour friendsnn");
fscanf(pntRead, "%s%s", charFirstName, charLastName);
//**********************************************************************************
// Print the data from the "friends.dat" file
//**********************************************************************************
while (!feof(pntRead))
{
printf("%s %sn", charFirstName, charLastName);
fscanf(pntRead, "%s%s", charFirstName, charLastName);
}
//**********************************************************************************
// Exit with no errors
//**********************************************************************************
exit(EXIT_SUCCESS);
//**********************************************************************************
// Exit and display an error code
//**********************************************************************************
ErrorHandle:
perror("The following error occured");
exit(EXIT_FAILURE);
getch();
}