Thread: fprintf

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    fprintf

    I have trouble fprintf to output.txt file..

    FILE* out;

    int number, result;
    number = 291;
    result = 15;

    d = number ^ result; /* OR to get answer to store to output file */
    fprintf((out, "%d", d));


    when compilling, I get an error... msg " error fprintf: too few arguments for call" and "out: undeclared identifier" can someone tell what is wrong to the code? I wrote this code in C.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ( and (( and ((( and (((( and ((((( might be interchangeable in math, but not in C.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why is there 2 sets of parenthesis in the fprintf() call?
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    FILE* out;

    int number, result;
    number = 291;
    result = 15;

    d = number ^ result; /* OR to get answer to store to output file */
    fprintf(out, "%d", d);

    Ok, I remove extra ( ) like you guys suggested. I recompile, then I still get this error
    "out undeclared identifier"

    the FILE pointer is already declared above, so I don't see why compiler complains...

    What can be wrong? I did re-check to see if output.txt is already exist in my work directory, the output.txt is there.

    What can be wrong? need help!!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'd have to post actual code for us to be certain, but apparently out was defined at some other scope. (Perhaps you are trying to print from a function, which will know nothing of variables in main?)

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    open attached file for code


    If return 1, then I want "d" write to output.txt file.. or else false return back function caller


    Just look at "int replaceNibble (int number, int nibble, int value, int newVal)" function difinition only and fprintf(out, "%d", d);

    I kept get compilling error "out undeclared identifier"..and don't know why...

    need your expert and help on this guys...

    wrote code in C.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, look at your code:
    Code:
    int replaceNibble (int number, int nibble, int value, int newVal) 
    {
    
    
    if (d == value)
    			{
    				int result, d;
    				result = newVal;
    				result = result << 12; /* new value that replace; now write to           output.txt file */
    				 d = number ^ result; /* OR to get answer to store to output file */
    				fprintf(out, "%d", d);
    
    				 printf("replace and has new number %d\n", d);
    				return 1;
    
                              }
    
    
    
    return 0;
    
    }
    There is indeed no declaration of a variable called "out" anywhere in that code. If you want to use the out that you have in main, then you need to pass it in to the function with everything else.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    int replaceNibble (int number, int nibble, int value, int newVal, FILE* out)

    After I passed " FILE* out" in the function, then the code works like a charm. Thanks you for that great support.

    Is good ideal to put "FILE* out" as global ? I try think a good reason why I want to use declare "FILE* out" as a global? Maybe someone can give me insight global is a good choice here? Global is dangerous, so I try to minimize the use of global.

    int replaceNibble (int number, int nibble, int value, int newVal, FILE* out)
    {


    if (d == value)
    {
    int result, d;
    result = newVal;
    result = result << 12; /* new value that replace; now write to output.txt file */
    d = number ^ result; /* OR to get answer to store to output file */
    fprintf(out, "%d", d); /* write to output.txt file */

    printf("replace and has new number %d\n", d);
    return 1;

    }

    return 0;

    }

    Thank you, Tabstop...and I should figure that out...but I kept refer to the error to see what mistake I have made... I don't see any mistake, so I kept ask myself why.... Thanks for pointing that out...
    Last edited by andy09; 10-08-2009 at 05:31 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Globals aren't dangerous. You just generally don't want to use a crapload of them because they're hard to keep track of once your programs get anywhere up in size.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 06:58 PM