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

    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; November 20th, 2012 at 05:28 AM.

  2. #2
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

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

    I think the issue here is with the -
    fin.open(donefile.c_str(), ifstream::in);
    line.
    According to your code, you have assigned the first argument as -
    tfile.assign(argv[1]);
    So, tfile contains the File Name: test.txt

    Hence the line would be -
    fin.open(tfile.c_str(), ifstream::in);

    Hope I am not missing anything here.

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

  3. #3
    Join Date
    Nov 2009
    Posts
    6

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

    Thanks for spotting the mistake. I replaced "donefile" with "tfile", and still got the same output.

    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.
    Last edited by hjazz; November 20th, 2012 at 10:20 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by hjazz View Post
    Thanks for spotting the mistake. I replaced "donefile" with "tfile", and still got the same output.
    You're using a non-trivial programming concept as threads and programming C++ using "goto"???

    Quit using "goto" and use structured looping constructs. A simple while() loop would have sufficed. By using goto(), you've reduced the number of persons willing to help you by a significant factor, since hardly anyone is going to waste time trying to untangle spaghetti code (and it really becomes difficult with C++, i.e. constructors, destructors, etc).

    Second, it would be helpful if you first hard-coded the names into the program to test if the program works instead of relying on inputting the file names. Then you wouldn't have come across the first issue already mentioned.

    As to the while() loop:
    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());
        
    bool bIsIopen = false;
    
    while (!bIsOpen)
    {
        fin.open(donefile.c_str(), ifstream::in);
        if (fin )
        {
           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();
           bIsOpen = true;
        }
        else
        {
           printf("Cannot open file %s\n", tfile.c_str());
           Sleep(500);
        }
    }
    All you need is a test for fIn being true to test if the stream was opened.
    This file is written by another function running on another thread,
    Then I suggest you use OS specific functions to test if a file is ready to be opened instead of general IOstreams (and the iostreams behaviour in VC 6.0 are non-standard anyway, since VC 6.0 is not an ANSI compliant compiler). Functions such as CreateFile() and such should be used.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2012
    Location
    Kolkata, WestBengal, India
    Posts
    15

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

    If you still don't find any answers, I think, it's the time you can start debugging your code - debug it line by line and check every data against every variable to make sure this is what exactly you want it.

    Raju2001006

    Please put [CODE][/CODE] tags around your code to preserve indentation and make it more readable...

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