Thread: Writing to single file in multiple functions

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    4

    Unhappy Writing to single file in multiple functions

    Hi all from a beginner,

    I'm trying to print all my output from a program to a single text file (running in unix) (I tried the redirection ">" but didn't work)

    I opened the file in main(), and written some output to it using fprintf(), then I called a function, opened the file again with
    fopen("output.txt","a")
    and tried to use fprintf again to append output to the same file, but when I run the program, only the output from main() were printed to the file, nothing was printed after the call to the second function.

    How do I do this?
    Cheers.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Are you checking the return status of "fopen()" command in the function you are calling to see if it is really opening the file or not?

    -Harsha.
    Help everyone you can

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    4
    Yes I am, sorry I didn't make that clear.

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Is it not feasible for you to declare a global file pointer for that file and open it first in the main function (before you start redirecting out put to it) and then use that same filepointer everywhere. You can open the file in the append mode, so you are sure the pointer will always be at the end and what ever you write into it will be appended at the end...

    -Harsha.
    Help everyone you can

  5. #5
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Like this...
    Code:
    /* File myfile.c */
    /* Global Variable */
    FILE *outputfile;
    ...
    ...
    int main()
    {
      ...
      ...
      outputfile = fopen("OutPutFile","a");
      if(outputfile == NULL)
      { 
          printf("Error Opening Output file\n");
          .........
      }
      ....
      ....
      fprintf(outputfile,<<format>>...);
      ... 
      ....
      some_function_that_prints_to_the_file();
      another_function_that_prints_to_the_file();
      ...
      return 0;
    }
    ...
    ...
    Help everyone you can

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Why didn't the redirectional operator > not work? How did you use it?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by vsriharsha
    Is it not feasible for you to declare a global file pointer for that file and open it first in the main function (before you start redirecting out put to it) and then use that same filepointer everywhere.
    Except that you are using global variables when you can easily just pass it around and have things be a lot safer.

    Example:
    Code:
    #include <stdio.h>
    
    void func(FILE *);
    
    int main(void)
    {
      FILE *out = stdout;
    
      if ( out == NULL )
        return 1;
    
      fprintf(out, "%s", "Hello world\n");
      func(out);
      return 0;
    }
    
    void func (FILE *out)
    {
      fprintf(out, "Hello my name is bob\n");
    }
    Now just replace
    Code:
    FILE *out = stdout;
    with an fopen() call.
    Last edited by Thantos; 08-21-2004 at 11:18 AM.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    4
    Thanks all for helping out...
    When I used the redirection operator >, the output are not in the correct order.
    The structure of my program is like this:
    main() prints to output.txt, then calls a function solver(),
    solver() append output to output.txt, then use system("./generate > dev/null") to call another program generate,
    Is it because I didn't declare the file globally?
    or because I opened the file in main() (in write mode) and then opened it again in solver() (in append mode)?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Either
    - open the file once, and use it until you're finished.
    or
    - open it, use it, close it, open it, use it, close it, open it, use it, close it, etc etc

    Always make sure you close it before opening it again, and before ending the program.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    When I used the redirection operator >, the output are not in the correct order.
    Um hate to tell you but using redirection doesn't magically change the order of the output, all it does is takes the output to stdout and sends it to the file. If there is something wrong with the order of the output then I'd suggest you look at your program.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    4
    I don't know why, but when I run the program without the redirection operator, the output are in the correct order, but when I call the program with a redirection to a text file AND within the program a system call to another program also with a redirection to the same text file, the order changed...

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If there is something wrong with the order of the output then I'd suggest you look at your program.
    Post your current code so we can take a look at it.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    How about using fflush() on that stream after every fwrite() ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. writing to file with multiple forked process
    By hiawatha in forum C Programming
    Replies: 7
    Last Post: 04-18-2007, 06:30 PM
  3. single File Pointer to Multiple Files
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 12:49 AM
  4. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM