Help, cannot figure this out
I am trying to get a program that will allow a user to input a filename, then the program will search through the file for < and > and change them to < and >
then this will then output a newfile with the changes
I have tried but whenever I run it asks for the file name which I input a file, then asks for the name I should give the output which I do. Then it closes. It does create a new file but the new file has nothing inside of it.
I was wondering what is wrong with my code since it seems as the program never opens the input file.
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outStream;
string fileoutput_name;
string filename;
cout << "Name your file ";
cin >> filename;
getchar ();
fstream file( filename.c_str() );
string searchString("<");
string searchString2 (">");
string replaceString( "<");
string replaceString2(">");
string::size_type i = 0;
while ( (i = filename.find(searchString, i)) != string::npos ) {
filename.replace( i, searchString.size(), replaceString );
i++;
}
while ( (i = filename.find(searchString2, i)) !=string::npos ) {
filename.replace( i, searchString2.size(), replaceString2 );
i++;
}
cout << " What would you like to name the file to where these\n"
<< " random numbers will be stored?\n";
cin >> fileoutput_name;
outStream.open(fileoutput_name.c_str());
getchar ();
return 0;
}
Re: Help, cannot figure this out
Put it through debugger, and go line by line, and be sure to check all return values.
And BTW when you open files with ofstream, you must also close them.
but before that you should really try to acctually write something to the file. ;)
And welcome to codeguru.
Be sre to read this before you continue to post.
http://www.codeguru.com/forum/announ...nouncementid=6
Notice the part on including code in the post.
You can also edit your post according to rules stated there.
In the meantime.
Enjoy your stay.
Re: Help, cannot figure this out
Here is a clue:
Code:
fstream file( filename.c_str() );
Re: Help, cannot figure this out
problem solved
thanks for your input
Re: Help, cannot figure this out
Quote:
Originally Posted by Odiee
And BTW when you open files with ofstream, you must also close them.
Any open file should be closed by the ofstream destructor.
Personally I prefer to avoid the .open() and .close() methods altogether when I use the things.
Re: Help, cannot figure this out
Quote:
Originally Posted by Lindley
Any open file should be closed by the ofstream destructor.
Personally I prefer to avoid the .open() and .close() methods altogether when I use the things.
Sure, But I personally like to see something closing whenever I see it open.
It's a matter of preference probably.
And ye, he could close the file with close, but doesn't have to.