CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005
    Posts
    1

    fstream and variable filenames

    I've got a program that formats a data file and outputs it to a different filename.



    My input looks like this

    ifstream openFile ("BATCH002.DAT");
    if (openFile.is_open())
    {

    while (! openFile.eof() )
    {

    getline (openFile,line);

    oneArray[q] = stripSpace(line);

    q++;

    }

    openFile.close();

    my output looks like this:

    ofstream outputFile("OUTPUT2.TXT");
    if (outputFile.is_open())
    {
    for (int k = 0; k < arrayHeight; k++)
    {
    if (oneArray[k] != "") outputFile << oneArray[k] << "\t\t\t\t" << twoInts[k] <<"\n";
    }

    outputFile.close;
    }
    else cout << "Error opening file.";
    }

    I don't want the input and output filenames hard coded. I know you can use argv and argc in the main() to get the command line arguments into variables. The problem is that when I replace "BATCH002.DAT" or "OUTPUT2.TXT" with a variable my program errors.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: fstream and variable filenames

    How does your program "errors"?
    Compile time errors?
    Runtime errors?
    Post the exact error.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2004
    Posts
    296

    Re: fstream and variable filenames

    1)All functions expect particular arguments and the ifstream() constructor is no exception. The type you feed it must be the type it is expecting. Post your actual code, the errors, and put a comment on the line in your program indicating where the error is occuring.

    2)Use code tags around your code(click on the "vB code" link in the bottom left hand corner of the page to see how) .

    3) In order for the reading from the file to end correctly, your read statement should be the while loop conditional:

    while( getline(...) )

    getline() returns an object that will evaluate to false if an error occurs while reading from the file. End of file is considered an error. However, there are other errors that can occur as well, and the way you have it now, if one of those errors occurs, then you'll enter an infinite loop. That will happen because eof won't be reached, but every attempt to read from the file will fail because of the error, so your loop will go 'round and 'round, never hitting eof. You need to end your loop on any error--not just eof.
    Last edited by 7stud; November 3rd, 2005 at 02:37 PM.

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