Linux/C++ system call problem

Discussion in 'C++' started by C++ Newbie, Apr 18, 2008.

  1. C++ Newbie

    C++ Newbie Guest

    Hi,

    Why doesn't the following C++ program echo the value of 97? If you
    manually "head -3 temp.txt | tail -1" the temp.txt file, you will get
    97. Use it in a system call, and it doesn't work.

    #include <fstream>

    using std::fstream;
    using std::ios;

    int main()
    {
    int i = 0;
    fstream myfile;
    myfile.open ("temp.txt", ios::eek:ut);
    while (i < 100)
    {myfile << i << "\n";
    i++;
    }
    system ("echo Third last line of file is:");
    system ("tail -3 temp.txt | head -1");
    }


    Thanks.
     
    C++ Newbie, Apr 18, 2008
    #1
    1. Advertising

  2. "C++ Newbie" <> writes:

    > Hi,
    >
    > Why doesn't the following C++ program echo the value of 97? If you
    > manually "head -3 temp.txt | tail -1" the temp.txt file, you will get
    > 97. Use it in a system call, and it doesn't work.
    >
    > #include <fstream>
    >
    > using std::fstream;
    > using std::ios;
    >
    > int main()
    > {
    > int i = 0;
    > fstream myfile;
    > myfile.open ("temp.txt", ios::eek:ut);
    > while (i < 100)
    > {myfile << i << "\n";
    > i++;
    > }
    > system ("echo Third last line of file is:");
    > system ("tail -3 temp.txt | head -1");
    > }
    >
    >
    > Thanks.


    "Buffer".

    Use flush or close the file before tail.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Apr 18, 2008
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:fua9hc$i97$...

    >> Why doesn't the following C++ program echo the value of 97? If you
    >> manually "head -3 temp.txt | tail -1" the temp.txt file, you will get
    >> 97. Use it in a system call, and it doesn't work.
    >>
    >> #include <fstream>
    >>
    >> using std::fstream;
    >> using std::ios;
    >>
    >> int main()
    >> {
    >> int i = 0;
    >> fstream myfile;
    >> myfile.open ("temp.txt", ios::eek:ut);
    >> while (i < 100)
    >> {myfile << i << "\n";
    >> i++;
    >> }
    >> system ("echo Third last line of file is:");
    >> system ("tail -3 temp.txt | head -1");
    >> }


    > Your question has nothing to do with C++, sorry. The behaviour of
    > the 'system' function is... (you guessed it!) system-specific. Ask
    > in a Linux newsgroup.


    Actually, it might have something to do with C++, because part of how C++ is
    defined is that it is not required to write information into a file until
    you flush the file's buffer. So it is at least possible that the reason the
    program is failing is that the calls to "system" are happening at a point
    when the file does not contain its intended contents.

    You might try changing the program as follows:

    while (i < 100) {
    myfile << i << "\n";
    i++;
    }
    myfile << std::flush;

    and see if it now dows what you intend.
     
    Andrew Koenig, Apr 18, 2008
    #3
  4. C++ Newbie

    James Kanze Guest

    On 18 avr, 16:07, "Alf P. Steinbach" <> wrote:
    > * C++ Newbie:
    > > Why doesn't the following C++ program echo the value of 97?
    > > If you manually "head -3 temp.txt | tail -1" the temp.txt
    > > file, you will get 97. Use it in a system call, and it
    > > doesn't work.


    > > #include <fstream>


    > > using std::fstream;
    > > using std::ios;


    > > int main()
    > > {
    > > int i = 0;


    > This variable has no business being declared at this point.


    > Declare it in the head of the 'for' loop you should have been
    > using instead of 'while'.


    > > fstream myfile;
    > > myfile.open ("temp.txt", ios::eek:ut);


    > Just use an ofstream,


    > ofstream myfile( "temp.txt" );


    And check for errors afterwards.

    > > while (i < 100)
    > > {myfile << i << "\n";
    > > i++;
    > > }


    > At this point it might be a good idea to close the file or at
    > least flush the file buffers.


    Again, and check for errors afterwards. Writes have been known
    to fail.

    > > system ("echo Third last line of file is:");
    > > system ("tail -3 temp.txt | head -1");


    > The effect of the system call is, as one might guess from the
    > function's name, system dependent.


    > But even if it worked as written, i.e. that that command is
    > passed to some command interpreter, that's not the command you
    > state you're giving manually.


    Yes, but in this case, this is the correct command, and what he
    stated he gave manually is wrong:).

    In fact, the problem is obvious: when he invokes his command
    here, the file has not yet been flushed, and so the OS hasn't
    yet received the data. When he enters the command manually, the
    program has terminated, the return from main has closed the
    file, and if he doesn't have any errors, the same command,
    entered on the command line, will work.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 18, 2008
    #4
    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. gooch

    linux kernel system call

    gooch, Oct 6, 2003, in forum: C Programming
    Replies:
    3
    Views:
    414
  2. markus
    Replies:
    22
    Views:
    689
    Dances With Crows
    Sep 22, 2004
  3. Nagaraj
    Replies:
    1
    Views:
    920
    Lionel B
    Mar 1, 2007
  4. Nick Birnie

    Linux system call to retrieve process status

    Nick Birnie, Apr 16, 2009, in forum: C Programming
    Replies:
    14
    Views:
    6,030
    Flash Gordon
    Apr 21, 2009
  5. fmbright
    Replies:
    8
    Views:
    227
Loading...

Share This Page