Like Tree1Likes
  • 1 Post By Adak

writing an fprintf statement toa file as text

This is a discussion on writing an fprintf statement toa file as text within the C Programming forums, part of the General Programming Boards category; Hi, I want to write an fprintf statement to a text file but the compiler is replacing the "%e" in ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    9

    writing an fprintf statement toa file as text

    Hi, I want to write an fprintf statement to a text file but the compiler is replacing the "%e" in the command with some random value. This is what i want to be printed:-
    fprintf(of "AC\nGain=%e\nPhase=%e\nUGF=%e\n\n", Gain, x, ugf) .
    Its printing:
    fprintf(of "AC\nGain=1.030403e-312\nPhase=0.000000e+00\nUGF=2.076246e-317\n\n", Gain, x, ugf) .
    I tried "\%e" but it doesn't work....same output any ideas??

    Thanks in advance.
    Gaurav.
    Last edited by gfotedar; 02-06-2012 at 04:07 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Double your % - so %%e.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Do you literally want to write this in it's entirety?

    Code:
    fprintf(of "AC\nGain=%e\nPhase=%e\nUGF=%e\n\n", Gain, x, ugf)
    If so why was, Gain, x and ugf's values printed? Do you think you could post some code, I don't really know what you are trying to do.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    9
    @Adak Thanks the double % worked!! :)

    @subsonic yes I wanted that line to be printed literally and the values printed were not the values of the variables but were some garbage values.

    Gaurav.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Yeah, the trick then is that you need to escape all special characters. I guess I was confused by the fact that your variables and new lines where printed. Still don't know what you did, can you post some code?

    Code:
    #include <stdio.h>
    
    #define FPRINTF_STRING "fprintf(of \"AC\\nGain=\%e\\nPhase=\%e\\nUGF=\%e\\n\\n\", Gain, x, ugf)"
    
    int main()
    {
        printf("%s\n", FPRINTF_STRING );
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gfotedar View Post
    @Adak Thanks the double % worked!!

    Gaurav.
    You don't *HAVE* to act so surprised, you know!
    gfotedar likes this.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    9
    the corrected code is :

    fprintf(fa1,"fprintf(of \"AC\\nGain=%%e\\nPhase=%%e\\nUGF=%%e\\n\\n\", Gain, x, ugf)\n");

    The "Gain","x","ugf" are variables defined in the file i am writing to. So there are actually 3 files.
    1st file containes the above code .
    2nd contains the command inside
    final values of Gain ,ugf etc are printed to 3rd file.
    Its a bit confusing i know

    Gaurav.
    Last edited by gfotedar; 02-06-2012 at 05:41 AM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Right, but that will actually write %%e to your file, not %e.

    It's confusing because you refused to post code, which left me guessing what you where doing.
    Last edited by Subsonics; 02-06-2012 at 05:46 AM.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    9
    No I've already tried it, its printing %e

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Because you hard code it inside the fprintf statement, if you create a variable it wont.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Subsonics View Post
    Right, but that will actually write %%e to your file, not %e.
    To write %%e you would have to use:
    Code:
    fprintf( fp, "%%%%e" );

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

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by quzah View Post
    To write %%e you would have to use:
    Code:
    fprintf( fp, "%%%%e" );
    Not if the string is a variable like the example I gave.

    Code:
    const char *printf_string = "fprintf(of \"AC\\nGain=%%e\\nPhase=%%e\\nUGF=%%e\\n\\n\", Gain, x, ugf)";
    printf("%s\n", printf_string );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text File not writing?
    By ShiroiShu in forum C Programming
    Replies: 6
    Last Post: 11-29-2011, 06:36 AM
  2. Replies: 8
    Last Post: 05-05-2010, 03:43 PM
  3. Problem with writing in a file using fprintf
    By g_p in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 09:51 AM
  4. How to put quotes in fprintf statement
    By mashley in forum C Programming
    Replies: 2
    Last Post: 12-13-2003, 11:06 AM
  5. fprintf to text file
    By Ray Schmidt in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2003, 05:18 AM

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