473,804 Members | 3,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access the stdout file

Hello,
I am trying to find a way to take the contents of a directory and
write it into a file. I have a directory that has several hundred text
files, and I want to create a file containing all of the names of those
files. I can get the contents of the directory onto the screen using
system("dir"); But I have not been able to get this data into a file.
I understand that stdout is a pointer to a file, and so I should be
able to open and read this file without physically viewing the screen.
Anyway, any ideas? Thanks in advance.

andrew

Oct 4 '06
13 3500
David Wade wrote:
"Andrew" <an************ @gmail.comwrote in message
news:11******** ************@h4 8g2000cwc.googl egroups.com...
>Hello,
I am trying to find a way to take the contents of a directory and
write it into a file. I have a directory that has several hundred text
files, and I want to create a file containing all of the names of those
files. I can get the contents of the directory onto the screen using
system("dir" );

I guess at this point I should include the usual disclaimer. Standard "C"
know nothing about the "system" call, its a (fairly common) extension. But
it is an extension.
At this point it is important to clear up confusion which may arise from
Mr. Wade's assertion above. The system() function is part of standard
C, and has long since been. C does not specify, however, what the
string argument to system() means, or what sorts of things might be
meaningful in that string. That is because system() is used
specifically for things which are outside of C itself.
Oct 4 '06 #11
Martin Ambuhl <ma*****@earthl ink.netwrites:
David Wade wrote:
>"Andrew" <an************ @gmail.comwrote in message
news:11******* *************@h 48g2000cwc.goog legroups.com...
>>Hello,
I am trying to find a way to take the contents of a directory and
write it into a file. I have a directory that has several hundred text
files, and I want to create a file containing all of the names of those
files. I can get the contents of the directory onto the screen using
system("dir") ;
I guess at this point I should include the usual
disclaimer. Standard "C"
know nothing about the "system" call, its a (fairly common) extension. But
it is an extension.

At this point it is important to clear up confusion which may arise
from Mr. Wade's assertion above. The system() function is part of
standard C, and has long since been. C does not specify, however,
what the string argument to system() means, or what sorts of things
might be meaningful in that string. That is because system() is used
specifically for things which are outside of C itself.
To be even more painfully precise, here's C99 7.20.4.6:

7.20.4.6 The system function

Synopsis
1 #include <stdlib.h>
int system(const char *string);

Description
2 If string is a null pointer, the system function determines whether
the host environment has a _command processor_. If string is not
a null pointer, the system function passes the string pointed to
by string to that command processor to be executed in a manner
which the implementation shall document; this might then cause
the program calling system to behave in a non-conforming manner
or to terminate.

Returns
3 If the argument is a null pointer, the system function returns
nonzero only if a command processor is available. If the argument
is not a null pointer, and the system function does return,
it returns an implementation-defined value.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 4 '06 #12
thanks much. that did it.
David Wade wrote:
"Andrew" <an************ @gmail.comwrote in message
news:11******** ************@h4 8g2000cwc.googl egroups.com...
Hello,
I am trying to find a way to take the contents of a directory and
write it into a file. I have a directory that has several hundred text
files, and I want to create a file containing all of the names of those
files. I can get the contents of the directory onto the screen using
system("dir");

I guess at this point I should include the usual disclaimer. Standard "C"
know nothing about the "system" call, its a (fairly common) extension. But
it is an extension.
But I have not been able to get this data into a file.
I understand that stdout is a pointer to a file, and so I should be
able to open and read this file without physically viewing the screen.

Thats because system creates a seperate process. As others have pointed out
you can do something like:-

system("dir /b >files.txt")l

but of course if there is already a "files.txt" you are in trouble....
Possiblly something like:-

system("dir /b >%tmp%\files.tx t");

is safer, but then you need to find out whats in %tmp% in order to open it
and read it....
Anyway, any ideas? Thanks in advance.

andrew

Dave.
Oct 10 '06 #13
Andrew wrote:
Hello,
I am trying to find a way to take the contents of a directory and
write it into a file.
Standard C doesn't know a thing about directories. Opening, reading and
writing directories will vary according to your host system.
I have a directory that has several hundred text
files, and I want to create a file containing all of the names of those
files. I can get the contents of the directory onto the screen using
system("dir"); But I have not been able to get this data into a file.
I understand that stdout is a pointer to a file, and so I should be
able to open and read this file without physically viewing the screen.
The C type 'FILE' is _not_ synonymous with disk files. Though a FILE
object can point to a disk file. It could also, concievably, control
access to any I/O device. Typically, when your program starts
execution, the object 'stdout' points to a FILE object which holds meta
information about the "Standard Output Stream". This is usually the
display device of the terminal. You can't "read" all FILE objects the
same way. Trying to read an output stream like stdout will produce
undefined behaviour.

The best way to do this task is to find out the system API for reading
directories and using it, read the list of it's contents to your file.

Oct 10 '06 #14

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

Similar topics

6
5641
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* . I have tried the simplest method of capturing stdout, stderr via: saveout = sys.stdout sys.stdout = file_obj
4
2320
by: Leon | last post by:
"stdout" is file object, it open mode is "w" and it have a close() fuction..... while it run close(), how to reopen it because I want to do rewrite it stdout content update
7
3979
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout, name='/tmp/stdout.log', mode='w',bufsize=-1): super(StdoutLog, self).__init__(name,mode,bufsize) self.stdout = stdout
31
2035
by: Randy Yates | last post by:
When I try fprintf(file, "Report of File %s\n", sMF->fileName); I get a core dump. This works fine if fprintf is changed to sprintf and a string buffer used instead as the first parameter. It looks like the standard descriptors are not really open.
9
18294
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for unsuccessful test. int Test_writeHello() {
4
11293
by: xman | last post by:
I'm looking for a C/C++ library that can get access to the stdin and stdout of a process. I may invoke a external program in a C/C++ program, but how do write data into the stdin of the external program, and read data out from the stdout of the external program? Suggestions are much appreciated.
7
384
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just set self.file to a file object rather than inheriting from file. I started with something like this (simplified for clarity): class Output(object): def __init__(self, file=sys.stdout, verbosity=1):
5
2615
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout sys.stdout = self
4
2297
by: lovecreatesbea... | last post by:
For example, in Bourne Shell both stdout and stderr can be re-directed to /dev/null, $ ./a.out 2>&1 /dev/null then is there any difference still?
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.