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;
}
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;
}