fprintf isn't outputting anything to file

This is a discussion on fprintf isn't outputting anything to file within the C Programming forums, part of the General Programming Boards category; I am trying to use the following c code to print out an array that I have passed in. It ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    fprintf isn't outputting anything to file

    I am trying to use the following c code to print out an array that I have passed in. It should output the text in hexadecimal format one on each line. When I first wrote it, I had no problems with it working I opened the output file and my array was there. I changed the fileOutName parameter and now I can't get it to print out anything. Any help would be appreciated. Thanks

    Code:
     printoutput(int  output[],char * fileOutName){
        int i=0;
        FILE * pOutfile;
        pOutfile = fopen( fileOutName, "w" );
        while(output[i]!=0){
            fprintf( pOutfile, "0x%0.4X\n", output[i]);
            i++;
    }
    Last edited by RagingPenguin4; 09-16-2012 at 07:04 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your fprintf has a constant 5 as the output value. You probably want (and probably had before) output[i] there.

    Shouldn't you check that the file opened properly?

    Shouldn't you close the file before returning from the function?

    Your while loop is a perfect candidate for a for loop.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Sorry the 5 was an something that I had for testing that I didn't realize was there but I put the output[i] back in its place where it should be. I would have used a for loop but I didn't have the array length

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try this. Does it work? If not, what happens?
    Code:
    void printoutput(int output[], char *fileOutName) {
        int i;
        FILE *pOutFile = fopen(fileOutName, "w");
        if (pOutFile == NULL) {
            fprintf(stderr, "fopen failed\n");
            exit(1);
        }
        for (i=0; output[i]!=0; i++)
            fprintf(pOutfile, "0x%0.4X\n", output[i]);
        fclose(pOutFile);
    }
    Last edited by oogabooga; 09-16-2012 at 08:09 PM. Reason: added beginning brace
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Hey oogabooga - I think that you may have missed an opening '{' at the start of the function.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Click_here View Post
    Hey oogabooga - I think that you may have missed an opening '{' at the start of the
    function.
    Thanks click. I've fixed it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Hmm not sure why that fixed it but at least it works now. On a semi-related note whenever I make that function void, Visual studios doesn't like it however it isn't returning anything :/
    Thanks!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    What compiler error/warning do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    error C2371: 'printoutput' : redefinition; different basic types
    Although i think i figured it out, in my prototype I had left out the return type and I am assuming (can't remember exactly) that the default return type is an int so when I declared it void it clashed with the prototype.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Outputting File Data To Text File
    By noname88 in forum C Programming
    Replies: 1
    Last Post: 10-04-2011, 01:12 PM
  2. outputting to file?
    By TonyG in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:55 AM
  3. C++ Outputting into a PDF file
    By allen9190 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2010, 10:15 PM
  4. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  5. Dev-C++ outputting .exe file
    By Yuri in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2005, 11:14 AM

Tags for this Thread


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