Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with leftover characters in FILE input 1

Status
Not open for further replies.

darr01

Programmer
Oct 24, 2001
28
0
0
MY
Hi, guys. I am having problem with my FILE input.

Whenever I input a structured data into my FILE, my first record displays correctly. But the subsequent entries contain the left over characters from the previous input.

Say for e.g. I entered a title for a book as:
Teach Yourself C Programming (first record)

If I enter a second record, e.g with the input
'C Language for Dummies' I will get the result below
in my FILE:
C Language for Dummiesmming

Seems to me that the first record has some leftover characters which was not removed. I have tried using fflush(), flushall(), fgets() but to no avail.

My Question is: Is there any way to rectify this problem?

The funny thing is, when I display/view specific records, the data extracted was correct (i.e. without the leftover characters). It is only when I open the FILE (text file) with notepad that I see the leftover characters in the data.

For your info, I am using gets() to input my strings.

Thanks in advance,

Darryl H
 
There is obviously a bug in your code. Please post your code and we will help you.

Brudnakm
 
Hi, Brudnakm. Thanks for your fast response. Here is my code. I've shortened it to exclude certain lines as it is too long.

//Declaring pointer to FILE
FILE *bookPtr;

//Defining structure
struct bookData {
char isbn[14];
char title[100];
char author[100];
char publisher[100];
char bookType;
char month[4];
char year[5];
char price[8];
} item;

/****** FUNCTION DEFINITION: newEntry() ***********/

void newEntry()

{
//Declaring local variables
char addEntry, isbnInput[14];
int strLength, titleLength, authorLength, publisherLength, priceLength;
int month, year;

printf("***********************************************************\n\n");
printf("ADD NEW ENTRY\n\n");


if ((bookPtr = fopen("bookdata.txt", "a+")) == NULL)
printf("File could not be opened");
else
{
//Ask input from user
fflush(stdin); //flush buffer to prevent buffer overflow
printf("Enter ISBN No.(X-XXXXX-XXX-X): ");
fgets(isbnInput);
strLength = strlen(isbnInput);
//Error checking for ISBN format entry
while(strLength != 13 || isbnInput[1] != '-' ||
isbnInput[7] != '-' || isbnInput[11] != '-')
{
printf("Error! ISBN No format incorrect!\n");
printf("Enter ISBN No.(X-XXXXX-XXX-X): ");
gets(isbnInput);
strLength = strlen(isbnInput);
}


//File is found
if(fseek(bookPtr, 0, SEEK_SET) == 0)
{
//While not End of File
while(!feof(bookPtr))
{
//Read 1 record from File
fread(&item, 1, sizeof(item), bookPtr);

//If both strings are the same
if((strcmp(item.isbn, isbnInput)) == 0)
{
printf("Error. This ISBN No already exist in database\n\n");
//Restart input process
newEntry();
}
}
//Copy string in isbnInput to item.isbn
strcpy(item.isbn, isbnInput);
}

printf("Enter Book Title: ");
gets(item.title);
//Error checking for Book Title entry
titleLength = strlen(item.title);
//While input is empty
while(titleLength == 0)
{
printf("Error! Title field empty. Please enter title: ");
gets(item.title);
titleLength = strlen(item.title);
}

printf("Enter Author? ");
gets(item.author);
//Error checking for Author entry
authorLength = strlen(item.author);
while(authorLength == 0)
{
printf("Error! Author field empty. Please enter Author: ");
gets(item.author);
authorLength = strlen(item.author);
}

printf("Enter Publisher: ");
gets(item.publisher);
//Error checking for Publisher entry
publisherLength = strlen(item.publisher);
while(publisherLength == 0)
{
printf("Error! Publisher field empty. Please enter Publisher: ");
gets(item.publisher);
publisherLength = strlen(item.publisher);
}

printf("Enter book category ('f' - Fiction / 'n' - Non-Fiction): ");
fflush(stdin);
scanf("%c", &item.bookType);
item.bookType = toupper(item.bookType);//Convert small letter to capital
//Error checking for Book Type entry
while(item.bookType != 'F' && item.bookType != 'N')
{
printf("Input error! You must enter either 'F' or 'N': ");
fflush(stdin);
scanf("%c", &item.bookType);
item.bookType = toupper(item.bookType);
}

fflush(stdin);
printf("Choose Publishing Month (mmm, e.g. 'Jan'): ");
gets(item.month);
//Error checking for Month entry
month = strlen(item.month);
item.month[0] = toupper(item.month[0]);
while(month != 3)
{
printf("Error! Please enter first 3 letter of month only (e.g. 'Jan')? ");
gets(item.month);
month = strlen(item.month);
item.month[0] = toupper(item.month[0]);
}

printf("Enter Publish Year(yyyy, e.g. '2001')? ");
gets(item.year);
//Error checking for Year entry
year = strlen(item.year);
while(year != 4)
{
printf("Error! Please enter year in 4 digits(yyyy, e.g. '2001')? ");
gets(item.year);
year = strlen(item.year);
}

printf("Enter price? ");
gets(item.price);
//Error checking for Price entry
priceLength = strlen(item.price);
while(priceLength == 0)
{
printf("Error! Price field empty. Please enter Price: ");
gets(item.price);
priceLength = strlen(item.price);
}

//Display all input for verification
printf("\nYou have just entered the information below:\n\n");
//Calling displayRecords function
displayRecords();

printf("\nAdd new entry to database('y' - Yes / 'n' - No)?: ");
scanf("%c", &addEntry);
addEntry = toupper(addEntry); //Capitalize character
//Error checking for Yes or No entry
while(addEntry != 'Y' && addEntry != 'N')
{
printf("Input error! You must enter 'y' or 'n': ");
scanf("%c", &addEntry);
addEntry = toupper(addEntry);
}

if(addEntry == 'Y')
{
if(fwrite(&item, 1,sizeof(item), bookPtr)!=sizeof(item))
fprintf(stderr, "Fail to write data into file!");
else
{
//Close FILE stream
fclose(bookPtr);
printf("Entry saved!");
strcpy(item.isbn, "");
strcpy(item.title, "");
strcpy(item.publisher, "");

}

printf("\n\nAdd more new entries('y' - Yes / 'n' - No)?: ");
fflush(stdin);
scanf("%c", &addEntry);
addEntry = toupper(addEntry);

//Error checking for Yes or No entry
while(addEntry != 'Y' && addEntry != 'N')
{
printf("Input error! Please enter either 'y' or 'n': ");
scanf("%c", &addEntry);
addEntry = toupper(addEntry);
}

if(addEntry == 'Y')
newEntry();
else
if(addEntry == 'N')
{
system("cls");
fflush(stdin);
choiceMenu(); //Back to Main Menu
}
}

else
if(addEntry == 'N')
choiceMenu(); //Back to Main Menu
}
}
 
darr01,

I have noticed a couple of things:

1) You should include stdio.h
2) You are using fgets where I think you intend to use gets.
3) You are mixing ascii and binary file functions.
- You open the file in ASCII mode (i.e. "a+".
- You read and write with binary functions i.e. fread and fwrite.
- You view the file in ASCII (notepad)

The reason that only one line appears in Notepad is that fwrite places non-printable characters (event if your strings do not fill up the allocated character array the whole array is copied to the file). These characters are confusing Notepad and misleading you because notepad expects only printable characters. You can verify this with a hex (binary) editor such as A.X.E.

Since all of your data is in ASCII, I recommend that you replace the freads and fwrites with fscanf and fprintf and keep everything consistently ASCII.

Brudnakm.
 
Hi, brudnakm. Thanks for your response. I am actually pretty new to C and find it really interesting. So much to learn, so little time. I guess I lack practical experience. That's why I find this forum a good way to improve my skills.

Well, thanks again.

Regards,

Darryl H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top