I get an error when I try to use cin.getline(line). Also, I can get strcpy(line, tempString) to work. It says no conversion exists between sting and const char*. Not sure why I get this when both are declared as string.
// Purpose: prompts the user for a 5 line limerick and writes the data to the root directory in binary format
// Date: 9/8/2014
// Filename: SaveLimerick.cpp
// Input: 5 line limerick poem
// Output: 5 line limerick in binary format
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
usingnamespace std;
int main()
{
string line;
constchar QUIT = 'Q';
constchar* FILENAME = "C:\\Limerick.dat";
ofstream writingFile(FILENAME);
if (!writingFile.good())
cout << "File could not be opened for writing" <<
endl;
else
{
cout << "Enter a line or Q to quit: ";
cin.getline(line);
while (line[0] != QUIT)
{
string tempString = " ";
writingFile.write(reinterpret_cast <constchar*>(&line), sizeof(line));
cout << "Enter a line or Q to quit ";
cin.getline (tempString);
strcpy(line, tempString);
}
}
//closes file
writingFile.close();
system("Pause");
return 0;
}
Hey, Thanks. That getline(cin, line); seemed to work. I revised my code as shown. I changed it a bit because I am required to store it in binary output.
// Purpose: prompts the user for a 5 line limerick and writes the data to the root directory in binary format
// Date: 9/8/2014
// Filename: SaveLimerick.cpp
// Input: 5 line limerick poem
// Output: 5 line limerick in binary format
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
usingnamespace std;
int main()
{
string line;
int lineNum = 1;
constchar* FILENAME = "C:\\Users\\Admin\\Limerick.dat";
ofstream writingFile(FILENAME, ios::out | ios::binary);
if (!writingFile.good())
cout << "File could not be opened for writing" <<
endl;
else
{
cout << "You will be prompted for 5 lines of a Limerick poem." << endl << endl;
while (lineNum != 6)
{
string tempString;
cout << "Enter line " << lineNum << ": ";
getline(cin, tempString);
writingFile.write(reinterpret_cast <constchar*>(&tempString), sizeof(tempString));
++lineNum;
}
}
//closes file
writingFile.close();
system("Pause");
return 0;
}
however when I try to retrieve the Limerickdat file using a seperate program i get gibberish for some of the lines. I'm not sure where the problem lies. Any ideas.
hmm...so a string is the same size no matter how many characters it holds. I got 28 for both, line1 and line 2. I'm not sure how to apply this. I think you are telling me to change this somehow: writingFile.write(reinterpret_cast <const char*>(&tempString), sizeof(tempString)); I'm not sure. Can I get another hint?
The thing is that string allocates dynamically space for the text that it holds. In other words, the text is not within those 28 bytes (my environment has 8 byte strings).
Hey, thanks for the feedback. I revised my code to use a char array of size 100. I still get gibberish however. Not sure why. both programs compile but I get a blocks of lines when I try to retrieve the limerick. Any ideas?
// Purpose: prompts the user for a 5 line limerick
// Date: 9/8/2014
// Filename: SaveLimerick.cpp
// Input: 5 line limerick poem
// Output: 5 line limerick in binary format
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
usingnamespace std;
template<typename T>
ostream& binary_write(std ostream& stream, const T& value)
{
return stream.write(reinterpret_cast<constchar*>(&value), sizeof(T));
}
int main()
{
char tempChar[100];
constchar* FILENAME = "C:\\Users\\Admin\\Limerick.dat";
ofstream writingFile(FILENAME, ios::out | ios::binary);
//creates fixed length dat file
for (int x = 0; x < 6; ++x)
binary_write(cout, tempChar)
if (!writingFile.good())
cout << "File could not be opened for writing" <<
endl;
else
{
cout << "You will be prompted for 5 lines of a Limerick poem." << endl << endl;
for (int x = 0; x < 6; ++x)
{
cout << "Enter line " << x << ": ";
cin.getline(tempChar);
writingFile.seekp((x - 1) * sizeof(tempChar));
binary_write(stream, tempChar);
}
}
//closes file
writingFile.close();
system("Pause");
return 0;
}
If you still require help, please post the latest version of the actual code which you are running, otherwise there's no way to tell where the problems are.