Like Tree1Likes
  • 1 Post By nvoigt

Using fstream to open a file that is created only during runtime

This is a discussion on Using fstream to open a file that is created only during runtime within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using Visual C++ 6.0 and I'm trying to use fstream to open and read a file that is ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Using fstream to open a file that is created only during runtime

    Hi,

    I'm using Visual C++ 6.0 and I'm trying to use fstream to open and read a file that is created only during runtime. This file is written by another function running on another thread, and my program will keep trying to "open" the file until it can be opened, i.e. after it's created, then read 3 numbers from it and execute the rest of its code.

    The file test.txt has the content

    Code:
    1
    3
    4
    My program that polls and opens the file is as follows:

    Code:
    ifstream fin;
    std::string tfile, snum1, snum2, snum3;
    long int num2, num3;
        
    tfile.assign(argv[1]);
    printf("Begin prog %s\n", tfile.c_str());
        
    fp: fin.open(donefile.c_str(), ifstream::in);
    if (fin.is_open())
    {
       printf("fin is open\n");
       getline(fin, snum1);
       getline(fin, snum2);
       getline(fin, snum3);
       num2 = atol(snum2.c_str());
       num3 = atol(snum3.c_str());
       printf("snum1 = %s\n", snum1.c_str());
       printf("num2 = %d num3 = %d\n", num2, num3);
       fin.close();
    }
    else
    {
       printf("Cannot open file %s\n", tfile.c_str());
       fin.close();
       Sleep(500);
       goto fp;
    }
        
    remove(tfile.c_str());
    printf("End of prog\n");
    I executed the program by

    Code:
    test_prog.exe "C:\test.txt"
    and waited about 3 seconds before putting the test.txt file into C:\

    My output was

    Code:
    Begin prog C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    fin is open
    snum1 = 
    num2 = 0 num3 = 0
    End of prog
    The test.txt file disappears after I refresh the C:\ folder.

    So the values for snum1, num2 and num3 are all wrong, as if the file was not read correctly.

    If I put a while fin.good() loop after printf("fin is open\n"); for that entire block (until printing the values of num2 and num3), then I get

    Code:
    Begin prog C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    Cannot open file C:\test.txt
    fin is open
    End of prog
    How can I correctly read a file that is only created during runtime?

    Thank you.
    Last edited by hjazz; 11-20-2012 at 03:28 AM.

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    108
    Quote Originally Posted by hjazz View Post
    Code:
    tfile.assign(argv[1]);
    printf("Begin prog %s\n", tfile.c_str());
    fin.open(donefile.c_str(), ifstream::in);
    You're opening the wrong filename: donefile.

    The correct format for long int in printf is %ld.
    Last edited by DRK; 11-20-2012 at 03:41 AM.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Matticus likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Please upgrade your bizarrely outdated IDE and get rid of printf in favor out std::cout.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Quote Originally Posted by DRK View Post
    You're opening the wrong filename: donefile.

    The correct format for long int in printf is %ld.
    Thanks for spotting the mistake. I replaced "donefile" with "tfile", and still got the same output.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    I've also added a

    Code:
    if (fin.rdstate() & ifstream::failbit) != 0)
       printf("failbit set\n");
    check right after the

    Code:
    printf("fin is open\n");
    line, and the failbit was set when the file is put into the folder during runtime.

    Everything runs correctly when the file is in place before execution.

    ETA:
    I finally found the problem.

    The failbit was set after the first fin.open failed, and was never reset when the fin.open succeeded. Once I did a fin.clear() after Line 24 of my original post, the program ran successfully.
    Last edited by hjazz; 11-20-2012 at 09:05 PM. Reason: Found the solution to my question

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fstream open method causing me a few headaches
    By Finchie_88 in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2010, 06:44 PM
  2. fstream open parameters
    By Ancient Dragon in forum C++ Programming
    Replies: 5
    Last Post: 01-27-2006, 10:34 AM
  3. Help: Finding only Last modified file or Created file
    By lagrz in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2006, 03:49 PM
  4. fstream lets you open a file for output twice (?!)
    By DonFiasco in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2005, 07:41 PM
  5. Why won't fstream file open?
    By Brown Drake in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2001, 10:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21