Computing.Net > Forums > Programming > C program help needed

C program help needed

Reply to Message Icon

Original Message
Name: ouroboros
Date: December 15, 2002 at 09:30:34 Pacific
Subject: C program help needed
OS: windows 98
CPU/Ram: Intel Pentium III 800 Mhz
Comment:

hi
ive been trying for a loooong time to get the code to work. Actually when i compiled it using VC++ i did not get any errors only a few warnings. when i'd finally run the program nothing is written to the log.txt file. What im trying to do is run "dir" using system() call and copy the output that is printed on the screen and save it to a file. but it is not working. plz help me.

-heres the code-

#include
#include
#include
main()
{
char *buff;
FILE *fp;
if((buff=malloc(sizeof(char) * 200))==NULL)
{
fprintf(stderr,"malloc failed");
exit(1);
}
fp=fopen("c:/windows/desktop/log.txt","w");
if(fp==NULL)
{
fprintf(stderr,"error opening file");
exit(1);
}
buff=system("dir");
fputs(fp,buff);
getch();
return 0;
}



Report Offensive Message For Removal


Response Number 1
Name: SN
Date: December 15, 2002 at 10:04:27 Pacific
Subject: C program help needed
Reply: (edit)

Hi-
I only glanced at the c code, I'm not a c guy myself, but I am pretty sure that you should do this a different way. Rather than trying to execute dir and capture the output, you should have dir itself send the output to a file rather than the screen. Your system command should look something like:
dir>>file.txt

Or if the greater than signs didn't show up,
dir]]file.txt
(with gt signs rather than brackets)

Best of luck,
SN


Report Offensive Follow Up For Removal

Response Number 2
Name: Ronin1
Date: December 15, 2002 at 10:34:33 Pacific
Subject: C program help needed
Reply: (edit)

On my compiler at least, system is looking for an int return value and not a pointer to a char, so I don't think that is valid C code.

you could dump the dir to a txt file and read that if you wish to use system().

system("dir > dir.txt");

Your code is unclear. Do you wish to read the directory contents of windows\desktop or are you trying to write the contents of a dir listing to log.txt?

fopen("c:\\windows\\desktop\\log.txt", "mode");



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C program help needed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge