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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binary files writing not working as expected

Status
Not open for further replies.

grexroad

Programmer
Jan 30, 2008
7
US
I am trying to learn about binary files. How to open and write to then close the file. Reopen the file to read then close it again. Things aren't going quite well though. I wrote the following program to write and read a binary file.

Run the program and then when prompted:
Type in 2 for number of words.
Type in abcdefgh for the first word.
Type in 12345678 for the second word.

The printf will be:
abcde12345 on the first line.
12345 on the second line.

Inside of the binary file will be abcde122345.
Any ideas what I'm not doing correctly?

I wanted to see on the screen:
abcde on the first line
12345 on the second.

Below is the entire program, please help this unlearned newbee out.

#include "stdafx.h"
#include <iostream>
#using <mscorlib.dll>

#define LETTERS 5 //number of letters
#define WORDS 4 //number of words

struct MyStruct
{
char string[LETTERS];
};

int main(void)
{

char answer[LETTERS];
int numWords;

MyStruct First [WORDS];
MyStruct Second[WORDS];

printf("\nHow many words do you want to enter? ");
gets(answer);

numWords = atoi(answer);

for(int counterA = 0; counterA < WORDS; counterA++)
{
for(int counterB = 0; counterB < LETTERS; counterB++)
{
First [counterA].string[counterB] = '\0';
Second[counterA].string[counterB] = '\0';
}
}

FILE* fp;

fp = fopen("My_File.dat","wb");

for(int counter = 0; counter < numWords; counter++)
{
printf("\n\tType in a word: ");
gets(First[counter].string);

fwrite(&First[counter], sizeof(MyStruct), 1, fp);
}

fclose(fp);

fp = fopen("My_File.dat", "rb");

for(int counter = 0; counter < numWords; counter++)
{
fread(&Second[counter], sizeof(MyStruct), 1, fp);
}

for(int counter = 0; counter < numWords; counter++)
{
printf("\n\t%s", Second[counter].string);
}

fclose(fp);

return 0;
}
 
You're reading in more characters than your buffer can handle. You read in 5 characters, but the buffer is also 5 meaning there's no space for the null terminator.

What you really should be doing is using fgets() instead of gets() to read from stdin. fgets() allows you to specify the size of the buffer. You'll need to trim out the trailing newline that fgets() puts in, but that's minor.
 
I understand that I am overloading my buffer as I am do it intent because I see it as being a problem that needs solved. I see what a little bit of what you are trying to explain to me but am unfamiliar with how to use the fgets or how to trim out the trailing newline. Would you please give me a bit of a lead on how to do it.
 
The prototype for fgets (from the man page) is as follows:

char *fgets(char *s, int size, FILE *stream);

You would pass the size of the buffer in the second parameter, and stdin for the third.

As for trimming the newline, just check the last character in the string, and if it's '\n' zero it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top