I would like to write all the data types as a whole to the disk file. Iv'e heard something about "fillers". Could someone tell me a little bit what a filler is and how to use it?
And how can I make it so when i open the disk file, or text file, it doesn't display the record number?
Code:
void write_records(void);
void read_records(void);
void main()
{
int reply = 0;
int items = 3;
system("cls");
printf(" Welcome to my program!");
printf("\n\nThis program accepts four different data types and writes them");
printf(" to a disk file\nwhich can then be read in text format");
while (reply < items)
{
printf("\n\nPlease select from the following menu: ");
printf("\n%d. This function writes records to a disk file", 1);
printf("\n%d. This function reads records to a disk file", 2);
printf("\n%d. ( or %d, ..., etc. ) EXITS the program.", items, (items+1));
printf ("\n");
printf("\nTo exit choose option %d, or higher ");
printf("\nOption number: ", items);
scanf("%d", &reply);
printf("\nYou selected option number %d", reply);
if(reply == 1)
{
printf("\nThis function writes (a) record/s to a disk file");
write_records();
}
if (reply == 2)
{
printf("\nThis function reads (a) record/s from a disk file");
read_records();
}
if (reply >= items)
{
printf(" which exits the program!");
}
if ( reply < 1)
{
printf("\nChoice %d out of range (retype 1, ..., %d).",reply, items);
}
}
printf("\n\nThank you for using this program. Good-bye!");
printf("\n");
system("pause");
}
void write_records()
{
FILE *tofile;
char name_in[20];
char reply;
char telephone_number[6];
int ID_number;
int amount_in, rec_no=1;
tofile = fopen ("results.txt", "w");
do
{
rec_no++;
printf("\nType in a 5 digit ID number(e.g. 23028): ");
scanf("%d", &ID_number);
printf("\Type in your name(e.g. Jan): ");
scanf("%s", &name_in);
printf("Type in a 6 figure telephone number(e.g. 939808): ");
scanf("%s", &telephone_number);
printf("\Type in a 6 digit amount(e.g 123456): ");
scanf("%d", &amount_in);
fprintf(tofile, "\nRecord %d: ID no. = %d, Name = %s, Telephone no. = %s, amount = %d",
rec_no, ID_number, name_in, telephone_number, amount_in );
printf("Would you like to input another record (y/n): ");
reply = getche();
if ((reply != 'n') && (reply != 'N'))
{
printf("\nTo quit entering inputs you need to reply N or n -");
printf(" your reply was %c", reply);
}
}
while ((reply == 'y') || (reply == 'Y'));
fclose(tofile);
}
void read_records()
{
FILE *fromfile;
char content_in[60];
int rec_no = 0;
fromfile = fopen ("results.txt", "r");
while (!feof(fromfile))
{
rec_no++;
//fscan (fromfile, "%s", content_in); //This reads up to a next space
fgets (content_in, 60, fromfile);
printf ("\nThese are the records in the file", content_in);
if (rec_no >= LinesPerScreen)
{
printf ("\n");
system("pause");
system("cls");
}
}
fclose(fromfile);
}