VC++ _popen to call ftp and pipe output to a file, cannot open file?

Discussion in 'C++' started by Michael, Dec 1, 2004.

  1. Michael

    Michael Guest

    Hello,

    Not sure if this is the right message board but I have an interesting
    problem.

    Using Visual Studio .NET, C++, I want to write an all-encompassing C++
    program to download new files from an FTP Server. The C++ program will
    ftp to the server and download a list of files, match those files with
    the ones I've already received, put together a new ftp script and
    download.

    I am using _popen to call FTP.

    My problem is this, _popen calls ftp and pipes the output to a .txt
    file, then the C++ program tried to open the .txt file and fails. It's
    as if the C++ program is not waiting for the _popen to complete and
    _popen still has the file open so my next statement to attempt opening
    fails. Is there a way around this?

    Here is a bit of my code:

    // Call _popen and capture output for errors
    FILE *pPipe;
    char my_command[200];
    sprintf(my_command,"ftp -v -i -s:%s >
    %s",ftp_filename,output_filename);
    if ((pPipe = _popen(my_command,"rt"))==NULL)
    {
    cout << "FTP FAILURE" << endl;
    exit(1);
    }
    fclose(pPipe);

    // Open Output File
    sfile.open(output_filename,ios::in);
    if(!sfile)
    {
    cout << "Could not open: " << output_filename << endl;
    return -1;
    }

    The "if(!sfile)" is always TRUE, so the program fails.

    Thanks in advance for the assistance!
    Michael, Dec 1, 2004
    #1
    1. Advertising

  2. Michael

    Ron Natalie Guest

    Re: VC++ _popen to call ftp and pipe output to a file, cannot openfile?

    Michael wrote:

    > // Open Output File
    > sfile.open(output_filename,ios::in);


    Does output_filename actually exist?
    What is sfile? Hopefully it's an ifstream or such.

    > if(!sfile)
    > {
    > cout << "Could not open: " << output_filename << endl;


    You might want to try GetLastError or some other Windows call that might
    give you a better explanation of the failure.
    Ron Natalie, Dec 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    480
    Jack Klein
    Nov 2, 2004
  2. Michael

    system() or _popen()?

    Michael, Nov 11, 2004, in forum: C++
    Replies:
    2
    Views:
    3,142
    Michiel Salters
    Nov 12, 2004
  3. Replies:
    15
    Views:
    916
  4. Replies:
    2
    Views:
    102
  5. D. Buck
    Replies:
    2
    Views:
    466
    D. Buck
    Jun 29, 2004
Loading...

Share This Page