How to access the stdout file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    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

  • Roka

    #2
    Re: How to access the stdout file


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

    Comment

    • Keith Thompson

      #3
      Re: How to access the stdout file

      "Andrew" <andrewkgentile @gmail.comwrite s:
      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_Keit h) kst-u@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.

      Comment

      • Cong Wang

        #4
        Re: How to access the stdout file


        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?

        Comment

        • Papastefanos Serafeim

          #5
          Re: How to access the stdout file


          "Andrew" <andrewkgentile @gmail.comwrote in message
          news:1159938739 .071164.7510@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"); 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


          Comment

          • Elijah Cardon

            #6
            Re: How to access the stdout file


            "Papastefan os Serafeim" <serafeim@otene t.grwrote in message
            news:efvnf9$2is n$1@ulysses.noc .ntua.gr...
            >
            "Andrew" <andrewkgentile @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.


            Comment

            • David Wade

              #7
              Re: How to access the stdout file


              "Andrew" <andrewkgentile @gmail.comwrote in message
              news:1159938739 .071164.7510@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.


              Comment

              • Kenny McCormack

                #8
                Re: How to access the stdout file

                In article <Uc6dnfMIpt0ljr nYRVny2A@eclips e.net.uk>,
                David Wade <g8mqw@yahoo.co mwrote:
                >
                >"Andrew" <andrewkgentile @gmail.comwrote in message
                >news:115993873 9.071164.7510@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.
                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.

                Comment

                • David Wade

                  #9
                  Re: How to access the stdout file


                  "Kenny McCormack" <gazelle@xmissi on.xmission.com wrote in message
                  news:eg14c7$f3f $2@news.xmissio n.com...
                  In article <Uc6dnfMIpt0ljr nYRVny2A@eclips e.net.uk>,
                  David Wade <g8mqw@yahoo.co mwrote:

                  "Andrew" <andrewkgentile @gmail.comwrote in message
                  news:1159938739 .071164.7510@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.
                  >
                  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.


                  Comment

                  • Clever Monkey

                    #10
                    Re: How to access the stdout file

                    David Wade wrote:
                    "Kenny McCormack" <gazelle@xmissi on.xmission.com wrote in message
                    news:eg14c7$f3f $2@news.xmissio n.com...
                    >In article <Uc6dnfMIpt0ljr nYRVny2A@eclips e.net.uk>,
                    >David Wade <g8mqw@yahoo.co mwrote:
                    >>"Andrew" <andrewkgentile @gmail.comwrote in message
                    >>news:11599387 39.071164.7510@ h48g2000cwc.goo glegroups.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."

                    Comment

                    • Martin Ambuhl

                      #11
                      Re: How to access the stdout file

                      David Wade wrote:
                      "Andrew" <andrewkgentile @gmail.comwrote in message
                      news:1159938739 .071164.7510@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.

                      Comment

                      • Keith Thompson

                        #12
                        Re: How to access the stdout file

                        Martin Ambuhl <mambuhl@earthl ink.netwrites:
                        David Wade wrote:
                        >"Andrew" <andrewkgentile @gmail.comwrote in message
                        >news:115993873 9.071164.7510@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) kst-u@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.

                        Comment

                        • Andrew

                          #13
                          Re: How to access the stdout file

                          thanks much. that did it.


                          David Wade wrote:
                          "Andrew" <andrewkgentile @gmail.comwrote in message
                          news:1159938739 .071164.7510@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.

                          Comment

                          • santosh

                            #14
                            Re: How to access the stdout file

                            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.

                            Comment

                            Working...