|
-
October 28th, 2008, 01:25 AM
#1
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;
}
-
October 28th, 2008, 02:27 AM
#2
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.
Last edited by Odiee; October 28th, 2008 at 02:32 AM.
You just divided by zero, didn't you?
-
October 28th, 2008, 04:35 AM
#3
Re: Help, cannot figure this out
Here is a clue:
Code:
fstream file( filename.c_str() );
-
October 28th, 2008, 10:47 PM
#4
Re: Help, cannot figure this out
problem solved
thanks for your input
-
October 28th, 2008, 10:52 PM
#5
Re: Help, cannot figure this out
 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.
-
October 29th, 2008, 03:40 AM
#6
Re: Help, cannot figure this out
 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.
You just divided by zero, didn't you?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|