How to access the stdout file

Discussion in 'C Programming' started by Andrew, Oct 4, 2006.

  1. Andrew

    Andrew Guest

    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
     
    Andrew, Oct 4, 2006
    #1
    1. Advertising

  2. Andrew

    Roka Guest

    Andrew wrote:
    > Anyway, any ideas? Thanks in advance.

    What OS are you using?
     
    Roka, Oct 4, 2006
    #2
    1. Advertising

  3. "Andrew" <> writes:
    > 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) <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.
     
    Keith Thompson, Oct 4, 2006
    #3
  4. Andrew

    Cong Wang Guest

    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?
     
    Cong Wang, Oct 4, 2006
    #4
  5. "Andrew" <> wrote in message
    news:...
    > 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
     
    Papastefanos Serafeim, Oct 4, 2006
    #5
  6. "Papastefanos Serafeim" <> wrote in message
    news:efvnf9$2isn$...
    >
    > "Andrew" <> wrote 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.
     
    Elijah Cardon, Oct 4, 2006
    #6
  7. Andrew

    David Wade Guest

    "Andrew" <> wrote in message
    news:...
    > 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.
     
    David Wade, Oct 4, 2006
    #7
  8. In article <>,
    David Wade <> wrote:
    >
    >"Andrew" <> wrote in message
    >news:...
    >> 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 McCormack, Oct 4, 2006
    #8
  9. Andrew

    David Wade Guest

    "Kenny McCormack" <> wrote in message
    news:eg14c7$f3f$...
    > In article <>,
    > David Wade <> wrote:
    > >
    > >"Andrew" <> wrote in message
    > >news:...
    > >> 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.
     
    David Wade, Oct 4, 2006
    #9
  10. David Wade wrote:
    > "Kenny McCormack" <> wrote in message
    > news:eg14c7$f3f$...
    >> In article <>,
    >> David Wade <> wrote:
    >>> "Andrew" <> wrote in message
    >>> news:...
    >>>> 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."
     
    Clever Monkey, Oct 4, 2006
    #10
  11. David Wade wrote:
    > "Andrew" <> wrote in message
    > news:...
    >> 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.
     
    Martin Ambuhl, Oct 5, 2006
    #11
  12. Martin Ambuhl <> writes:
    > David Wade wrote:
    >> "Andrew" <> wrote in message
    >> news:...
    >>> 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) <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.
     
    Keith Thompson, Oct 5, 2006
    #12
  13. Andrew

    Andrew Guest

    thanks much. that did it.


    David Wade wrote:
    > "Andrew" <> wrote in message
    > news:...
    > > 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.
     
    Andrew, Oct 10, 2006
    #13
  14. Andrew

    santosh Guest

    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.
     
    santosh, Oct 10, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Douwe
    Replies:
    1
    Views:
    5,716
    Mike Schilling
    Jan 12, 2004
  2. Elad
    Replies:
    0
    Views:
    434
  3. xman
    Replies:
    4
    Views:
    780
    Michael DOUBEZ
    May 14, 2007
  4. Andreas S
    Replies:
    3
    Views:
    285
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    379
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page