CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    4

    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 &lt; and &gt;
    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( "&lt;");
    string replaceString2("&gt;");



    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;
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    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?

  3. #3
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Help, cannot figure this out

    Here is a clue:
    Code:
    fstream file( filename.c_str() );

  4. #4
    Join Date
    Oct 2008
    Posts
    4

    Re: Help, cannot figure this out

    problem solved
    thanks for your input

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    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.
    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
  •  





Click Here to Expand Forum to Full Width

Featured