473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Linux/C++ system call problem

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::out);
while (i < 100)
{myfile << i << "\n";
i++;
}
system ("echo Third last line of file is:");
system ("tail -3 temp.txt | head -1");
}
Thanks.
Jun 27 '08 #1
9 4444
C++ Newbie wrote:
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::out);
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.

BTW, you forgot to include the necessary header for the 'system'
function. IIRC, it's <stddef.h>.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
Victor Bazarov wrote:
[..]
BTW, you forgot to include the necessary header for the 'system'
function. IIRC, it's <stddef.h>.
OK, wrong. It's <stdlib.h>.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
"C++ Newbie" <ne********@googlemail.comwrites:
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::out);
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__
Jun 27 '08 #4
* 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::out);
Just use an ofstream,

ofstream myfile( "temp.txt" );
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.

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.

}
Thanks.
Well, you're welcome.
Cheers, & hth.,

- Alf
Jun 27 '08 #5
"Victor Bazarov" <v.********@comAcast.netwrites:
Victor Bazarov wrote:
>[..]
BTW, you forgot to include the necessary header for the 'system'
function. IIRC, it's <stddef.h>.

OK, wrong. It's <stdlib.h>.
Shouldn't that be <cstdlibwhen included from C++?

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Jun 27 '08 #6
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:fu**********@news.datemas.de...
>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::out);
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.
Jun 27 '08 #7
On 2008-04-18 11:14:13 -0400, Sherman Pendley <sp******@dot-app.orgsaid:
"Victor Bazarov" <v.********@comAcast.netwrites:
>Victor Bazarov wrote:
>>[..]
BTW, you forgot to include the necessary header for the 'system'
function. IIRC, it's <stddef.h>.

OK, wrong. It's <stdlib.h>.

Shouldn't that be <cstdlibwhen included from C++?
Both are valid. The C library isn't going to go away anytime soon.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #8
On 18 avr, 16:07, "Alf P. Steinbach" <al...@start.nowrote:
* 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::out);
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:ja*********@gmail.com
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
Jun 27 '08 #9
* James Kanze:
On 18 avr, 16:07, "Alf P. Steinbach" <al...@start.nowrote:
>* 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::out);
>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.
Well yes, that's what I wrote, and Andrew Koenig then also wrote that.

Seems to be full agreement. :-)
Cheers,

- Alf
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kieron Briggs | last post by:
I am trying to run the java VM in a virtual linux webserver environment with WebCentral: http://www.webcentral.com.au/docs/products/display.cgi?id=104&productCategory=Linux%20Servers When I...
34
by: Maboroshi | last post by:
Hello My question has to do with python and linux - I was interested in finding out what it would take to reimplement the Linux Kernel in python basically just taking the source code from linux...
21
by: Travis 'Bailo' Bickel | last post by:
Lately, I have been having a bear of a time trying to acquire a Linux driver for my S3/ProSavage chipset -- but that quest is now almost at an end as I have located a noble band of people who are...
26
by: Simon | last post by:
I'm doing a survey. When do you think GNU/Linux will be ready for the average Joe? What obstacles must it overcome first?
8
by: Xela | last post by:
Hi A have a very annoying problem. I have written java strored procedures for DB2 v8.1. Their deployement and usage is fine as long as the server is a Windows one. But under Solaris 8 and Linux...
2
by: rsd | last post by:
Hi, I'm trying get Samsung YH-920 mp3 player to work with Debian GNU/Linux. To do that I need to run http://www.paul.sladen.org/toys/samsung-yh-925/yh-925-db-0.1.py script, the idea behind the...
3
by: Bruno LIVERNAIS | last post by:
Hi, We are currently installing a DB2 V9 ESE on a Linux server (RHEL4U4-x86_64). Installation runs successfully on each node. Database user environment is OK and the instance is well created. To...
2
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.