468,505 Members | 2,006 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

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 #1
13 3168

Andrew wrote:
Anyway, any ideas? Thanks in advance.
What OS are you using?

Oct 4 '06 #2
"Andrew" <an************@gmail.comwrites:
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.
Ask in a newsgroup that deals with your operating system. Standard C
doesn't even have a concept of directories.

--
Keith Thompson (The_Other_Keith) 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 #3

Andrew wrote:
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
If you can use system("dir"); , why don't you use the command "dir * >
filenames.txt" to get what you want?

Oct 4 '06 #4

"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.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"); 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
Probably your operating system has a specific system call for
enumarating the contents of directories. If it is windows, as it
seems from the dir command you are using then check the
FindFirstFile and FindNextFile system calls.

For more info, you should ask again in a related group.

Serafeim
Oct 4 '06 #5

"Papastefanos Serafeim" <se******@otenet.grwrote in message
news:ef***********@ulysses.noc.ntua.gr...
>
"Andrew" <an************@gmail.comwrote in message
[system specific question}
Probably your operating system has a specific system call for
enumarating the contents of directories. If it is windows, as it
seems from the dir command you are using then check the
FindFirstFile and FindNextFile system calls.

For more info, you should ask again in a related group.

Serafeim
I was going to accuse you of misspelling seraphim in your moniker, but since
you're greek, I'm going to let that one go. OP needs to rephrase the
question to make it topical. Since he has the odor of windows, he can
expect the customary tomato in the face in clc.

Set the "sort by" on your newsreader to "subject" and look for "Parsing a
path name" to see how it is that one can talk about path in a manner
relevant to the C programming language. The other half of the question is
how to redirect stdout to a file, which I don't pretend to know. EC

p.s. Don't get stung by '/' vs '\'; I can only ever remember it's one or the
other.
Oct 4 '06 #6

"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.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.txt");

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 4 '06 #7
In article <Uc********************@eclipse.net.uk>,
David Wade <g8***@yahoo.comwrote:
>
"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegro ups.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.
Actually, standard "C" has a system() function. It just doesn't say
anything (*) about what it does.

(*) There's an exception to this - others will no doubt chime in with it
by and by.

Oct 4 '06 #8

"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
news:eg**********@news.xmission.com...
In article <Uc********************@eclipse.net.uk>,
David Wade <g8***@yahoo.comwrote:

"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.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.

Actually, standard "C" has a system() function. It just doesn't say
anything (*) about what it does.

(*) There's an exception to this - others will no doubt chime in with it
by and by.
Kenny,
I must be getting old. I have read "The Standard C Library" many times and
always missed the stuff on SYSTEM. I guess as it inlcudes "getenv()" you can
"sort of" do this....
Dave.
Oct 4 '06 #9
David Wade wrote:
"Kenny McCormack" <ga*****@xmission.xmission.comwrote in message
news:eg**********@news.xmission.com...
>In article <Uc********************@eclipse.net.uk>,
David Wade <g8***@yahoo.comwrote:
>>"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegr oups.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.
Actually, standard "C" has a system() function. It just doesn't say
anything (*) about what it does.

(*) There's an exception to this - others will no doubt chime in with it
by and by.

Kenny,
I must be getting old. I have read "The Standard C Library" many times and
always missed the stuff on SYSTEM. I guess as it inlcudes "getenv()" you can
"sort of" do this....
Dave.
system(const char *s) is in stlib.h

"If s is not a null pointer, the function passes the string s to be
executed by a command processor, supplied by the target environment, and
returns the status reported by the command processor. If s is a null
pointer, the function returns nonzero only if the target environment
supplies a command processor. Each implementation defines what strings
its command processor accepts."
Oct 4 '06 #10
David Wade wrote:
"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.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*****@earthlink.netwrites:
David Wade wrote:
>"Andrew" <an************@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegro ups.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_Keith) 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********************@h48g2000cwc.googlegrou ps.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.txt");

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tsai Li Ming | last post: by
4 posts views Thread by Leon | last post: by
7 posts views Thread by Mike | last post: by
31 posts views Thread by Randy Yates | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
7 posts views Thread by MisterPete | last post: by
5 posts views Thread by =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post: by
4 posts views Thread by lovecreatesbea... | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.