How to access the stdout file

A

Andrew

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
 
K

Keith Thompson

Andrew said:
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.
 
C

Cong Wang

Andrew said:
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?
 
P

Papastefanos Serafeim

Andrew said:
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
 
E

Elijah Cardon

Papastefanos Serafeim said:
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.
 
D

David Wade

Andrew said:
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.
 
K

Kenny McCormack

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

David Wade

Kenny McCormack said:
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.
 
C

Clever Monkey

David said:
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."
 
M

Martin Ambuhl

David said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
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.
 
A

Andrew

thanks much. that did it.


David said:
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.


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


Dave.
 
S

santosh

Andrew said:
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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top