Thread: fprintf and non-ascii symbols

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    fprintf and non-ascii symbols

    Hello,

    I'm trying to print non-ascii symbols on a file, using

    fprintf(file_name, "%c", xxx), where xxx is the symbol number

    I'm trying to print symbols 176, 177, 178 and 219 from http://en.wikipedia.org/wiki/Code_page_850.

    When I use printf only, the result is perfect. When I use fprintf, however, I get a different output. Is there any different thing to be done?

    I'm using Dev-cpp and I'm opening files with Window's Notepad, font Lucida Console.

    Thanks in advance

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sombrancelha View Post
    When I use printf only, the result is perfect. When I use fprintf, however, I get a different output. Is there any different thing to be done?
    Maybe post some code, because there should be no difference in the two.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    printf("%c", 176);

    fprintf(exit, "%c", 176);

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sombrancelha View Post
    printf("%c", 176);

    fprintf(exit, "%c", 176);
    No, I mean post the code that actually matters. I.e. all of it. There is no difference in functionality between those two lines.

    Naming a file stream "exit" is really weird. You are shadowing the exit() function.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    I'm Brazilian, so I give my variables names in portuguese. I don't use exit as a variable name, it was just for an example.

    Code:
         for (i = 0; i < m; i++){
             for (j = 0; j < n; j++)
                 if (MATRIX[i][j] == 0)
                    fprintf(saida, " ");
                 else if (MATRIX[i][j] % 4 == 0)
                    fprintf(saida, "%c", 176);
                 else if (MATRIX[i][j] % 4 == 1)
                    fprintf(saida, "%c", 177);
                 else if (MATRIX[i][j] % 4 == 2)
                    fprintf(saida, "%c", 178);
                 else
                     fprintf(saida, "%c", 219);
             fprintf(saida, "\n");
             }

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The output is the same. The easyest thing to do is just use the "Terminal" font in Notepad.

    gg

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    the problem is that someone else is going to correct my program (I'm making it for a University course).
    If I use this solution, the person will have to change theirs font.

    Is there any solution to that? (maybe it's possible to automatically open on Terminal font)

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is what happens when you use extended ASCII characters. They're not portable, between systems and even between different characters sets and fonts. As you have discovered.

    The simplest solution is to look at the file in a command-line program rather than in Notepad. For example, type type filename from the command-line, where filename is the name of your file.

    You could create a batch file to do this for your teacher automatically.
    Code:
    @type filename
    @pause
    Then in a README or something indicate how to view the file. I've done it before, believe me . . . .

    The best solution? Use ordinary ASCII characters. #, |, ", *, &#37;, @, $, whatever. Use your imagination. http://en.wikipedia.org/wiki/ASCII_art
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    dwks,

    i'm not sure I've understood your solution.. are you suggesting that the file should be read at a command-line program?

    And I didn't understand about the batch file.. what exactly is it? And how do I make one?

    Also, my program is quite simple, so there won't be a README...

    Thanks for the help

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    i'm not sure I've understood your solution.. are you suggesting that the file should be read at a command-line program?
    Right. Since your characters are messed up in the standard Windows character set, but appear fine in DOS, use DOS to view the file!

    The easiest way is to open a command prompt. Do that by going Start->Programs->Accessories->Command Prompt (on most modern Windows computers, I believe that's where it is located.) Then type
    Code:
    cd "whereever"
    where whereever is the location of your program. For example:
    Code:
    cd "\Documents and Settings\User\My Documents\programs"
    If the path contains no spaces, you can leave out the double quotes.

    Then, type
    Code:
    type filename
    and you should see your file.

    Here's a sample session:
    Code:
    MSDOS (c) Microsoft Corporation ...
    
    C:\Windows>cd "\Documents and Settings\User\My Documents\programs"
    
    C:\Documents and Settings\User\My Documents\programs>type something.txt
    This is the text in the file something.
    C:\Documents and Settings\User\My Documents\programs>exit
    Here's how you can do it so that your teacher or whatever can see your file without using the command prompt. Open notepad, and type
    Code:
    @type filename
    @pause
    Then save the file in the directory of your program's executable as "name.bat". Use the quotes! If you don't, you get a file called name.bat.txt, which you can't execute.

    Then just run that .bat file by double-clicking on it or whatever, and viola! you have your file, viewed from DOS.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sombrancelha View Post
    dwks,

    i'm not sure I've understood your solution.. are you suggesting that the file should be read at a command-line program?
    That's right.

    And I didn't understand about the batch file.. what exactly is it? And how do I make one?
    use notepad or your C editor to create a file called "show.bat", then type in the commands you want. If you prefix the command with a @ it will be "silent", meaning that the user don't see what the command is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you prefix the command with a @ it will be "silent", meaning that the user don't see what the command is.
    To elaborate:

    A batch file (ending with .bat) works just as if the user had typed every line directly into a DOS prompt. This means that with a file like
    Code:
    type something.txt
    pause
    you'd get
    Code:
    C:\Documents and Settings\User\My Documents\programs>type something.txt
    This is the text in the file something.
    C:\Documents and Settings\User\My Documents\programs>pause
    Press any key to continue ...
    To suppress those messages, you can put an @ in front of the lines you want suppressed, or you can put @echo off as a separate line. For example, with
    Code:
    @echo off
    type something.txt
    pause
    the user would see
    Code:
    This is the text in the file something.
    Press any key to continue ...
    In this case, you don't need the CD command in the batch file because the batch file's "current directory" is where it was run from by default.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    thanks for the help
    it worked perfectly
    however, i'll consider using ordinary ASCII characters, because it is a lot simpler, even though the output does not look as nice as with the other characters.
    however, if there is any other solution...

    thanks again

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One suggestion: use funky extended ASCII characters if you're printing to the screen, and ordinary ASCII ones if you're printing to a file. (You'll have to allow output to the screen as well as to a file, of course.)

    You can do it with something as simple as this:
    Code:
    FILE *fp;
    
    if(strcmp(filename "-") == 0) {  /* take a filename of "-" to mean the screen */
        fp = stdout;
    }
    else {
        fp = fopen(filename, "w");
        if(!fp) {
            perror(filename);
            fp = stdout;  /* use the screen for output if the file couldn't be opened */
        }
    }
    
    function(fp);
    
    void function(FILE *fp) {
        if(fp == stdout) putchar('#');
        else putchar(177);
    }
    and so on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    This is what happens when you use extended ASCII characters. They're not portable, between systems and even between different characters sets and fonts. As you have discovered.
    But as far as printf()/fprintf() is concerned, it's just a byte value. There should be NO DIFFERENCE in output between using printf() and fprintf(). As for what the character LOOKS LIKE when you open it in some application, that's anybody's guess.

    If the results are truly different between printf()/fprintf() then there is something wrong somewhere.

Popular pages Recent additions subscribe to a feed