
March 24th, 2005, 08:49 PM
|
|
Contributing User
|
|
Join Date: Jan 2005
Posts: 600

Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
|
|
you can use streams with strings:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 2)
{
cout << "ERROR: wrong input! You need to give me a file to open (remember, if there are backslashes \\ use forward slashes / or double backslashes \\\\, and if there are spaces quote the whole thing. Press enter to exit." << endl;
cin.get();
return 0;
}
ifstream in_file(argv[1]);
if (in_file.bad())
{
cout << "Sorry, could not open file. Press enter to exit." << endl;
cin.get();
return 0;
}
string txt_from_file = "";
getline(in_file, txt_from_file);
cout << "In " << argv[1] << " was: " << txt_from_file << endl;
in_file.close();
ofstream out_file(argv[1]);
if (out_file.bad())
{
cout << "Sorry, could not open file. Press enter to exit." << endl;
cin.get();
return 0;
}
string txt_to_go_into_file = "";
cout << "Gimme some stuff to put into " << argv[1] << ": ";
getline(cin, txt_to_go_into_file);
out_file << txt_to_go_into_file;
out_file.close();
return 0;
}
this program opens the file you give it in the first command-line argument (remember to use forward slashes or double backslashes, and quotes if there are spaces in the path/filename), reads the first line into a string, and prints the string to the console. Then it asks you for a line of text, puts that into a string, and then writes the string to the same file. compiles with dev-c++
|