Hi,

I am trying to develop a small programme that writes a three field "record" to an external text file and then reads that same data back from that external file.

So far, the programme does write the "records" to the external file in a one-field-per-line format whereas I would prefer to have all three fields of a "record" on one line in the external file. The contents of the external file are shown below.

Fred Zodiac
12345
3.8
Barney Tempest
23456
3.5
Wilma Shore
34567
3.7

This clearly shows that I am having a problem understanding how to write data to an external file in the required three-fields-per-line format, in the function saveStudentInformation().

The programme also does find a target "record" in the external file but displays it on-screen in the one-field-per-line format. Again this shows that I am having trouble understanding how to get printf() in function showStudentInformation() to display the data in the required three-fields-per-line format.

The code is shown below.
Code:
/* Create a program that uses a menu with options to enter student information*
 * (name, ID, GPA), print student information, or quit the program. Use data      *
 * files and FILE pointers to store and print information entered.                         */

#include <stdio.h>
#include <stdlib.h>  // For fgets()
#include <string.h>  // For strcpy()
#include <ctype.h>   // For toupper()


static void showMenu();
static void enterStudentInformation(char [], char [], char []);
static void saveStudentInformation(char [], char [], char []);
static void findStudentInformation(char [], char [], char []);
static void showStudentInformation(char [], char [], char []);


int main()
{
   char studentName[20] = "";
   char id[20] = "";
   char gpa[20] = "";
   char response;
   char newline;

   showMenu();
   (void) scanf("%c%c", &response, &newline);  // second %c swallows \n
   response = toupper(response);

   while (response != 'Q')
   {
      switch (response)
      {
         case 'A': enterStudentInformation(studentName, id, gpa);
                   saveStudentInformation(studentName, id, gpa);
                   break;
         case 'B': findStudentInformation(studentName, id, gpa);
                   showStudentInformation(studentName, id, gpa);
                   break;
      }

      showMenu();
      (void) scanf("%c%c", &response, &newline);
      response = toupper(response);
   }
   return 0;
} // end main()


void showMenu()
{
   printf("\tMenu\n");
   printf("\nA. Enter student information. <A/a>: ");
   printf("\nB. Show student information. <B/b>: ");
   printf("\nC. Quit the program. <Q/q>: ");
}


void enterStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
   int stringSize = 19;

   printf("\nEnter student name (firstname lastname): ");
   (void) fgets(aStudentName, stringSize, stdin);
   printf("\nEnter student ID: ");
   (void) fgets(anID, stringSize, stdin);
   printf("\nEnter student gpa: ");
   (void) fgets(aGPA, stringSize, stdin);
}


void saveStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
   FILE * ptrWriteRecord;

   ptrWriteRecord = fopen("studentInformation.dat", "a");
   if (ptrWriteRecord != NULL)
   {
      fprintf(ptrWriteRecord, "%s%s%s", aStudentName, anID, aGPA);
      (void) fclose(ptrWriteRecord);
   }
   else
   {
      printf("\nFile studentInformation.dat cannot be opened.\n");
   }
}


void findStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
   FILE * ptrReadRecord;
   char temp[20] = "";
   int stringSize = 20;

   ptrReadRecord = fopen("studentInformation.dat", "r");
   if (ptrReadRecord != NULL)
   {
      printf("\nEnter student name (firstname lastname): ");
      (void) fgets(temp, stringSize, stdin);

      while (feof(ptrReadRecord) != EOF)
      {
         (void) fgets(aStudentName, stringSize, ptrReadRecord);
         if (strcmp(aStudentName, temp)== 0)
         {
            (void) fgets(anID, stringSize, ptrReadRecord);
            (void) fgets(aGPA, stringSize, ptrReadRecord);
            fclose(ptrReadRecord);
            return;  // student information has been found
         }
      }  // end while()
   }
   else
   {
      printf("\nFile studentInformation.dat cannot be opened.\n");
      fclose(ptrReadRecord);
   }
}  // end findStudentInformation()


void showStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
   printf("\nName\t\tID\t\tGPA\n");
   printf("%s%s%s\n", aStudentName, anID, aGPA);
}

How can I write a "record" to the external file in a three-fields-per-line format and then display the data on-screen in that same format. For reading the external file in function findStudentInformation() I have tried using both fgets() and fscanf(), to no avail.

Stuart