Is there any way to clear the output stream?
Is there any way to clear the output stream?
You can fflush() it, but that only forces characters out from the buffer, onwards to their destination.
What's up with printf?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Ok here's the problem.... I am wrinting a database system and I am using fscanf to grab records from a file. The problem is that when when i try to printf after the fscanf all the proper string isn't displayed but part of the data pulled from the file is. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "record.h"
int numRecords;
int main(void) {
FILE *fp;
int choice;
numRecords = 0;
initFILE(&fp, "a+");
countRecords(fp);
while (1) {
clrscr();
printf("Choose on of the following\n"
"0 - Enter a new record (at the end)\n"
"1 - Print all the records\n"
"2 - List accounts with zero balance\n"
"3 - List accounts with credit balances\n"
"4 - List accounts with debit balances\n"
"5 - List accounts with last names that begin with a letter\n"
"6 - List accounts with first names that begin with a letter\n"
"7 - List accounts with last names that match a name\n"
"8 - List accounts with first names that match a name\n"
"9 - Search by index\n"
"10 - Delete a record by index (totally removed, leaving no empty file space)\n"
"11 - Modify a record by index (leaving no empty file space)\n"
"12 - End of run\n"
"?: ");
scanf("%d",&choice);
switch (choice) {
case 0:
printf("\nENTER A NEW RECORD\n");
addRecord(fp);
++numRecords;
break;
case 1:
printf("\nPRINTING ALL RECORDS\n");
printRecords(fp);
break;
case 12:
fclose(fp);
printf("\nEXITING PROGRAM...");
return 0;
default:
printf("Invaild input. Please try again\n");
}
}
}
void anyKey() {
printf("\nPress any key to continue.");
getch();
}
void initFILE(FILE** fpp, mode m) {
char* filename ="";
printf("Please enter the name of the file to open: ");
scanf("%s",filename);
if((*fpp=fopen(filename,m)) == NULL) { /* Can't open file */
printf("ERROR: unable to open %s",filename);
exit(1);
}
else
printf("Opened file: %s\n",filename);
}
void addRecord(FILE* fp) {
Record client;
printf("\nPlease enter firstname, lastname, and balance: ");
scanf("%s%s%f",client.firstname,client.lastname,&c lient.balance);
fprintf(fp,"%d %s %s %.2f\n", numRecords, client.firstname, client.lastname, client.balance);
}
void countRecords(FILE* fp) {
Record client;
rewind(fp);
fscanf(fp,"%d%s%s%f",&client.index, client.firstname, client.lastname, &client.balance);
while (!feof(fp)) {
++numRecords;
fscanf(fp,"%d%s%s%f",&client.index, client.firstname, client.lastname, &client.balance);
}
printf("Number of Records: %d",numRecords);
anyKey();
}
void printRecords(FILE* fp) {
Record client;
printf("INDEX FIRST NAME LAST NAME BALANCE\n");
printf("---------------------------------------------\n");
rewind(fp);
fscanf(fp,"%d%s%s%f",&client.index,client.firstnam e,client.lastname,&client.balance);
while (!feof(fp)){
printf("%5d %12s %15s %10.2f\n",client.index,client.firstname,client.las tname,client.balance);
fscanf(fp,"%d%s%s%f",&client.index,client.firstnam e,client.lastname,&client.balance);
}
anyKey();
}
This only seems to happen after countRecords is run and the file actually contains records in it. Any thoughts? I think there is a problem with the countRecords() function.
First off, you shouldn't loop using this as your control:
>while (!feof(fp))
You should be calling fscanf() and testing it for EOF.
I'll continue looking, but in the meantime, make this change.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Is there a way to read an entire line from the file at a time without using the function fread()?
fgets() gets a line of text, upto a given length.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
> char* filename ="";
> printf("Please enter the name of the file to open: ");
> scanf("%s",filename);
Your code is broken here
Although you initialise the pointer (good), you initialise it with something which cant hold too many chars (none at all). In addition, it may also be non-writable, though it is in your case
char filename[100];
printf("Please enter the name of the file to open: ");
scanf("%s",filename);
> while (!feof(fp)) {
The dreaded !feof "bug"
See my "copy file" example here
You could write
Which loops until end of fileCode:while ( fscanf( fp, "%d%s%s%f", &client.index, client.firstname, client.lastname, &client.balance) != EOF ) {
Or perhaps with some error checking
Which loops until either EOF, or a bad record is foundCode:while ( fscanf( fp, "%d%s%s%f", &client.index, client.firstname, client.lastname, &client.balance) == 4 ) {
Noting your first bug, are client.firstname and client.lastname arrays (char[]) or pointers (char*). Because they need to be arrays.