Problums writeing struct to a file C++
hi all, C++ beginner needs help.
I decided today to give C++ FileIo Streams a little go. since I not played with them much before and have not started out very well :(
anyway I desided to make a little project that can save the contents of a struct to a file. pretty basic in fact.
My problum is reading back the info. the letter and number displays ok but for some reason str is not. anyway I left my code below if that helps.
VB Code:
#include <iostream>
#include <fstream>
using namespace std;
struct test
{
int number;
char letter;
char *str;
} aTest;
int main(int agvc)
{
ofstream fout;
ifstream fin;
// fill struct with some random data
aTest.number = 245;
aTest.letter = 'A';
//What am I doing wrong here I wonder
aTest.str = (char*) malloc(100);
strcpy(aTest.str,"Hello World this is a long string");
//write the struct to the file
fout.open("C:\\test.txt",ios::binary);
fout.write((char*)(&aTest),sizeof(aTest));
fout.close();
// Clear struct of it's data I am only new in C++
// So i am not sure this is the correct way to clear stuff out
// tho for the example it be ok I think
aTest.letter = '\0';
aTest.number = 0;
strcpy(aTest.str,"\0");
//Load the struct back in so we can display the data
fin.open("c:\\test.txt",ios::binary);
fin.read((char*)(&aTest),sizeof(aTest));
fin.close();
// display the data
std::cout << "Number: " << aTest.number << "\n";
std::cout << "Letter: " << aTest.letter << "\n";
std::cout << "String: " << aTest.str << "\n"; // :( why is my str not showing
}
anyway hope someone can help
Thanks.
Re: Problums writeing struct to a file C++
well i haven't tried your code but i think u r making 1 mistake n not writing 1 statement:
1) u r closing the file before displaying the values, and
2) use getch(); [header file conio.h] at the end of the code, before the closing bracelet of main().
i think that shud work!!! maybe closing file after wont change anything but yes, u need to put getch() in your code!!!
Re: Problums writeing struct to a file C++
hi, Harsh
I am not sure what you mean. all data has been written to the file. that's why I am closeing it.
I only cleared the struct information once it's been saved to the file. and then I reload the file back in.
The problum is eveything else in the struct is showing all except this line.
std::cout << "String: " << aTest.str << "\n"; // why is my str not showing
any other ideas.
Re: Problums writeing struct to a file C++
Quote:
Originally Posted by dreamvb
hi, Harsh
I am not sure what you mean. all data has been written to the file. that's why I am closeing it.
I only cleared the struct information once it's been saved to the file. and then I reload the file back in.
The problum is eveything else in the struct is showing all except this line.
std::cout << "String: " << aTest.str << "\n"; // why is my str not showing
any other ideas.
sorry didn't read the whole post, my bad!!!
maybe in that case there is something wrong in just comment it out n then try compiling it.........probably it may work!!!!
Re: Problums writeing struct to a file C++
You're saving a pointer, not the character data. You need to save character data independently, usually with the length saved first.
Re: Problums writeing struct to a file C++
Hi CornedBee,
Can you give me a little example as I am still really leraning. if you have time thanks.
Re: Problums writeing struct to a file C++
Re: Problums writeing struct to a file C++
Writing the string
Code:
char *str;
int stringLen = 100;
str = new char[stringLen]; // Allocate memory the C++ way if you're using C++
memset(str, 0, stringLen); // The new operator doesn't clear memory automatically, and you certainly don't want to have garbage in your file
strcpy(str, "This is a long string that should be less than 100 chars"); // The string to be copied should be at most 99 chars
std::ofstream fOut("c:\\temp\\myfile.dat", std::ios::binary);
// Check if the file has opened, etc...
fOut.write(reinterpret_cast<char *>(&stringLen), sizeof(int)); // Using reinterpret_cast, so this ugly syntax reminds you that you're doing something that you shouldn't, but have to. It's also the proper C++ way to cast
fOut.write(str, stringLen);
fOut.close();
delete[] str; // Clear the allocated memory
Alternatively, you can calculate the length of the actual string (stringLen = strlen(str), and write that value to the file.
Reading the string:
Code:
char *str;
int stringLen;
std::ifstream fIn("c:\\temp\\myfile.dat", std::ios::binary);
// Check if the file has opened, etc...
fIn.read(reinterpret_cast<char *>(&stringLen), sizeof(int)); // Read the length first
str = new char[stringLen + 1]; // Allocate sufficient memory, including the null character
fIn.read(str, stringLen);
fIn.close();
// Note that when you didn't calculate the new value for stringLen in the above code, this stringLen will hold a value which is larger than the actual string size
std::cout << "String length: " << stringLen << std::endl;
std::cout << "String value: " << str << std::endl;
delete[] str;
Hope this helps
Re: Problums writeing struct to a file C++
In case you want this to be a part of a struct/class, you should create methods named "read" and "write" (or what you prefer). Since these methods are part of the struct/class, they have the knowledge how to read from/write to the file.
If the file also contains information that doesn't belong to this class, then the calling code should be responsible for opening/closing the file and reading/writing other information.
Re: Problums writeing struct to a file C++
Quote:
Originally Posted by dreamvb
fout.open("C:\\test.txt",ios::binary);
I wouldn't give a binary file a TXT extension ;)