0

While trying to simulate the bash command , I also need to write into a file called errors.log any troubled output , if a given command from the user failed .

I've tried to enter a few wrong inputs , but the needed file is not updated as expected.

Here is the main , it's a lot of code but I put the entire main , if there would be any doubt regarding each one of the variables . The actual action of main takes place with the forking , and the calling to the method handleSonProcess , which is down below , at the bottom of the code :

#define BUFFER 4096
#define BUFFER2 1024
#define RUN_FOREVER 1
#define ERROR_SIGN -1
#define TRUE 1
#define FALSE 0

When I run the code with the intput ls ddddx , the output should be :

ls: cannot access ddddx: No such file or directory

and I want to write that output into the errors.log file , but it doesn't get written .

What's wrong ?

Thanks

EDIT

errorFile = open("errors.log",O_WRONLY | O_TRUNC | O_CREAT,0600);

// open errors file
// if(errorFile <= 0)
{
    sprintf(errorStr,"open: %s\n",strerror(errno));
    write(errorFile,errorStr,strlen(errorStr));
    fflush(stdout);
    perror("open");
    close(errorFile);
    return 0;
}
JAN
  • 21,236
  • 66
  • 181
  • 318
  • 1
    Two things: When writing strings to the error file, do _not_ use `strlen(...) + 1`, it will write the zero as well which is not needed. The second, and which might be related to your problem, maybe you need to call `fflush`? – Some programmer dude May 24 '12 at 06:36
  • @JoachimPileborg: Thanks , can you please take a look at the update in the post ? what you're suggesting still won't work. – JAN May 24 '12 at 07:20

1 Answers1

0

After you write the message to error.log (or, more precisely, its file pointer), immediately flush it with fflush (). Also, before your program exits, make sure you close it.

Since you're in main (), you might also consider returning with an error value instead of doing an exit (EXIT_FAILURE).

I am not sure, but if you wrote something to the file pointer and it isn't appearing, it's probably because it is being buffered. And the immediate exit may not let your program exit cleanly. Try doing all 3: fflush, fclose, and doing a return instead of an exit. See if that works...

Ray
  • 880
  • 1
  • 10
  • 18
  • I've tried exactly what you said but it doesn't work . Updated in the post – JAN May 24 '12 at 07:14
  • Actually, you should fflush (and fclose) the file pointer to which the error file is being sent to. Sorry for not being clear! Your edit says "fflush (stdout)". That's flushing standard out. What you should be doing is fflush (and fclose) errorFile. i.e., "fflush (errorFile)". Nothing wrong with flushing stdout, of course. But that isn't what you're after. – Ray May 24 '12 at 09:10
  • Yes ,indeed . What I want is to copy the exact error message from the command , into the file . I've tried countless ways but nothing seems to do the trick. – JAN May 24 '12 at 09:13
  • By the way, you may want to take a look at this [link](http://stackoverflow.com/questions/259355/how-can-you-flush-a-write-using-a-file-descriptor) which talks about the difference between open/close and fopen/fclose. You're mixing the two up. That probably isn't the cause of your problem, but it may be a bit confusing. If you're desperate, try opening the error file with fopen instead of open and then use fflush/fclose. – Ray May 24 '12 at 09:17
  • Actually I am desperate ,since my deadline is in 23:59 :) . thanks I'll try what you said . – JAN May 24 '12 at 09:24
  • Is what you have in your Edit the only thing you've modified? The fflush command flushes the buffer; it doesn't set the file pointer (or descriptor) to flush. So, every time you want the buffer flushed, you need to issue the "fflush" command. That means every time you fprintf to that file pointer, you need to fflush it. (However, you used open and write and I've rarely used them. I'm afraid I don't know what's the problem in that case, though.) Good luck and sorry I can't be of more help! – Ray May 24 '12 at 09:27