Wie kann ich ein DIR Kommando auswerten?

Hallo zusammen,

mit dem Befehl

system(„dir *.txt“);

kann ich im akt. Verzeichnis alle txt-Dateien anzeigen lassen. Allerdings würde ich gerne haben das mein C-Programm das Ergebnis zurückbekommt. Anzeigen allein bringt mir nämlich nichts.

Es muß doch eine elegantere möglichkeit geben als die Ausgabe mit system(„dir *.txt > xxx.tmp“); in eine Datei zu schreiben und danach die Datei mir fread auszuwerten.

Wer weiß wie???

Viele Grüße
Albert

Auch hallo,

Du hast leider nicht erwähnt, in welcher Umgebung Du diesen Aufruf machen willst. Wenn ich mal WindowsXXX annehmen darf, funktioniert _popen:

Example

/\* POPEN.C: This program uses \_popen and \_pclose to receive a 
 \* stream of text from a system process.
 \*/

#include 
#include 

void main( void )
{

 char psBuffer[128];
 FILE \*chkdsk;

 /\* Run DIR so that it writes its output to a pipe. Open this
 \* pipe with read text attribute so that we can read it 
 \* like a text file. 
 \*/
 if( (chkdsk = \_popen( "dir \*.c /on /p", "rt" )) == NULL )
 exit( 1 );

 /\* Read pipe until end of file. End of file indicates that 
 \* CHKDSK closed its standard out (probably meaning it 
 \* terminated).
 \*/
 while( !feof( chkdsk ) )
 {
 if( fgets( psBuffer, 128, chkdsk ) != NULL )
 printf( psBuffer );
 }

 /\* Close pipe and print return value of CHKDSK. \*/
 printf( "\nProcess returned %d\n", \_pclose( chkdsk ) );
}

Output

 Volume in drive C is CDRIVE
 Volume Serial Number is 0E17-1702

 Directory of C:\dolphin\crt\code\pcode

05/02/94 01:05a 805 perror.c
05/02/94 01:05a 2,149 pipe.c
05/02/94 01:05a 882 popen.c
05/02/94 01:05a 206 pow.c
05/02/94 01:05a 1,514 printf.c
05/02/94 01:05a 454 putc.c
05/02/94 01:05a 162 puts.c
05/02/94 01:05a 654 putw.c
 8 File(s) 6,826 bytes
 86,597,632 bytes free

Process returned 0

Gruß, Ralf

Hallo Ralf,

vielen Dank für den Tipp, funktioniert tip-top. (Allerdings habe ich mir unter pop§en bis jetzt immer an etwas anderes gedacht…:wink:

Gruß
Albert