Mysterious fprintf problem

This is a discussion on Mysterious fprintf problem within the C Programming forums, part of the General Programming Boards category; I have experienced some mysterious problems when trying to write something into a file. The output file is created by ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Question Mysterious fprintf problem

    I have experienced some mysterious problems when trying to write something into a file.

    The output file is created by fopen in the beginning of the program. The idea is at the end of the run I want to write some data into the file, using fprintf.

    1 If I write something (e.g. "Hello world \n") into the output file at the beginning of my program, everything is fine. At the end of the program the data I really want is written into the output file (after the "Hello world\n" statement).

    2 If I don't write any statement at the beginning of the run, the progran crashes when it is time to write data into the file. A segmentation fault message is generated, and the output file remains empty.

    Can anyone explain such mysterious behavior?

    Thanks,

    Tor

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Can you show us some code?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    Post your source code also.

    Or atleast the part that is relavent to your problem.

    Other's can't just guess your code and reply.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Source code

    OK, in the beginning of main we have

    FILE *infile, *outfile;

    main(int argc, char *argv[])
    {
    outfile = fopen(argv[2], "w");

    fprintf(outfile, "Hello world\n");

    8<---------------------------
    Big fat program that never calls outfile with fprintf or anything else. At the end of main, the function report is called.
    8<---------------------


    void report(void)
    {
    /* Compute and write estimates of desired measures of performance. */

    printf("Before fprintf\n");

    fprintf(outfile, "Simulation results: \n\n");
    8<----------------------------

    Observations:

    1 the program works when the "Hello world" clause is there (except for the annoying greeting at the beginning of the data file).

    2 When "Hello world" is removed, the program crashed at "Simulation results:". It says Segmentation fault. I know that it crashes at that statment because I have used printf command such as the one shown to determine exactly where it goes wrong.

    Let me know if anybody has a clue. I am using a Sun workstation if it helps.

    Best,

    Tor

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    19

    Question Update

    By moving around the "Hello world" statement I have moved closer to a resolution.

    The program consists of several separately compiled .o but linked .o files.


    OK:
    outfile = fopen("Data.out", "w");
    fprintf(outfile, "Hello world\n");
    function_that_lives_in_different_object();

    Also OK:
    outfile = fopen("Data.out", "w");
    fprintf(outfile, "Hello world\n");
    function_that_lives_in_different_object();
    fprintf(outfile, "Hello world again\n");

    Not OK (Segmentation fault)
    outfile = fopen("Data.out", "w");
    function_that_lives_in_different_object();
    fprintf(outfile, "Hello world\n");

    It seems like the data.out file has to be accessed at least once before I start calling other objects. Can anybody explain why this would be...?

    Tor

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    It seems like the data.out file has to be accessed at least once before I start calling other objects.
    Not True.

    It must be compiler dependent. Both work for me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    FILE *outfile;
    void report(void);
    
    int main(int argc, char *argv[])
    {
    	outfile = fopen(argv[2], "w");
    
    	printf("heyyy");
    
    	report();
    
    	return 0;
    }
    
    
    void report(void) 
    {
    	int nothing;
    
    	printf("Before fprintf\n"); 
    
    	fprintf(outfile, "Simulation results: \n\n");
    
    	nothing = getch();
    
    }
    And

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    FILE *outfile;
    void report(void);
    
    int main(int argc, char *argv[])
    {
    	outfile = fopen(argv[2], "w");
    
    	printf("heyyy");
    
    	fprintf(outfile, "Hey World.\n");
    
    	report();
    
    	return 0;
    }
    
    
    void report(void) 
    {
    	int nothing;
    
    	printf("Before fprintf\n"); 
    
    	fprintf(outfile, "Simulation results: \n\n");
    
    	nothing = getch();
    
    }
    Ran from command line, c:/testing/test.exe 1 test.dat
    It ran and created the file, saving both entries just fine.
    Of course it did not work unless you run it from command/run line.

    - Using Digital Mars free compiler. Found on Cnet/downlaods.
    Last edited by stumon; 03-25-2003 at 11:31 AM.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Just curious what this function_that_lives_in_different_object();
    do?

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    For the two of you, try not creating a global FILE pointer. Create it in main, open it in main, and pass it to the other functions.

    Code:
    //prototype for report function.
    void report(FILE *);
    
    //call report like this.
    report(outfile);
    
    //receive the file pointer like this in the report function.
    void report(FILE *outfile2)

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Also in the code, I don't see you close the files, with fclose().

  10. #10
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    arsh, forgot myself to close. hope my system closed it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mysterious Linker Error
    By Night_Blade in forum Windows Programming
    Replies: 0
    Last Post: 04-30-2006, 02:50 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 07:00 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21