Feb 8, 2011 at 11:34pm Feb 8, 2011 at 11:34pm UTC
I was using the example code that is on this site:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// print the content of a text file.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string test = "save" ;
if (test == "load" ) {
ifstream infile;
infile.open ("test.txt" , ifstream::in);
while (infile.good()){
cout << (char ) infile.get() << endl;
}
infile.close();
}
if (test == "save" ) {
char * buffer;
long size;
ifstream infile ("test.txt" ,ifstream::binary);
ofstream outfile ("new.txt" ,ofstream::binary);
// get size of file
infile.seekg(0,ifstream::end);
size=infile.tellg();
infile.seekg(0);
// allocate memory for file content
buffer = new char [size];
// read content of infile
infile.read (buffer,size);
// write to outfile
outfile.write (buffer,size);
// release dynamically-allocated memory
delete [] buffer;
outfile.close();
infile.close();
}
return 0;
}
The problem is that the saving portion involves 2 files and the data is copied from the first to the second. I want the data to be from a string the user types in and have that be saved to the new file. I changed the save part to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if (test == "save" ) {
string buffer;
long size;
cin >> buffer;
size = buffer.length();
ofstream outfile ("test.txt" ,ofstream::binary);
// write to outfile
outfile.write (buffer,size);
outfile.close();
}
I get an error on the outfile.write(buffer,size); line.
Error:
38 C:\Documents and Settings\programming\Desktop\Science Fair\C++\MEDS\test.cpp no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::write(std::string&, long int&)'
AND
note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:360 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
What am I doing wrong?
Last edited on Feb 8, 2011 at 11:34pm Feb 8, 2011 at 11:34pm UTC
Feb 9, 2011 at 12:39am Feb 9, 2011 at 12:39am UTC
I was just working on the code and I worked it out with a bit more research of this site. Heres my completed code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// print the content of a text file.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string saveload;
cout << "Welcome to the program!\n" << endl;
cout << "---------------------------" << endl;
cout << "| save - Save to a file |" << endl;
cout << "| load - Load from a file |" << endl;
cout << "---------------------------\n" << endl;
getline(cin,saveload);
if (saveload == "load" ) {
ifstream loadFile;
loadFile.open ("Save.txt" , ifstream::in);
cout << "The file contained: " ;
while (loadFile.good()){
cout << (char ) loadFile.get();
}
cout << "" << endl;
loadFile.close();
}
if (saveload == "save" ) {
string textToSave;
cout << "Enter the string you want saved: " << endl;
getline(cin,textToSave);
ofstream saveFile ("Save.txt" );
saveFile << textToSave;
saveFile.close();
}
return 0;
}
It works perfectly!
Last edited on Feb 9, 2011 at 12:39am Feb 9, 2011 at 12:39am UTC