Linux/C++ system call problem

C

C++ Newbie

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.
 
P

Pascal J. Bourguignon

C++ Newbie said:
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.
 
A

Andrew Koenig

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.
 
J

James Kanze

* C++ Newbie:
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'.
Just use an ofstream,
ofstream myfile( "temp.txt" );

And check for errors afterwards.
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.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top