Thread: Strange problem with notepad

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Strange problem with notepad

    Hey there.

    One of my programs prints prime numbers to a file, and everything compiles and runs as expected. It's written in C.

    However, when I open it in notepad, and ONLY notepad, I simply see the following:

    ″‵‷ㄱㄠ″㜱ㄠ‹㌲㈠‹ㄳ㌠‷ㄴ㐠″㜴㔠″㤵㘠‱㜶

    And so on.

    Yes, I know what you're thinking. "You're printing the ASCII equivalent of the prime numbers as opposed to the numbrers."

    No, that was my first thought, too.

    "Then you must be ........ing something up when opening the file. Have you tried using the binary and text flags of fopen() ?"

    No to ........ing up and yes to flags.

    Anyway, what pretty much proves that I'm doing it correctly is that the file is viewed correctly in wordpad. Now, I understand this might not necessarily be a programming problem, but I couldn't really find an appropriate forum for it.

    Any ideas?

    All suggestions greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well without seeing the code, I'm guessing you're using one of the more recent compilers from Microsoft, and that you've managed to create a UNICODE text file.

    Wordpad will understand this, and notepad (being simpler) does not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Salem View Post
    Well without seeing the code, I'm guessing you're using one of the more recent compilers from Microsoft, and that you've managed to create a UNICODE text file.

    Wordpad will understand this, and notepad (being simpler) does not.
    Yes, this is what I thought too, as the chinese(Asian?) letters aren't a part of the ASCII set, but no, I'm simply using the Dev-C++ IDE which uses MinGW.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm simply using the Dev-C++ IDE which uses MinGW.
    Rather strange. I second Salem: post the smallest and simplest compilable code that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So post a short program which demonstrates the problem so other people can try it to see if they observe the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Salem View Post
    So post a short program which demonstrates the problem so other people can try it to see if they observe the same thing.
    I can put as little as "fprintf(out, "%d ", number);" and it get the same results.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    How about writing an entire program out as requested?

    Then after that, check the flags your IDE has set for when it compiles and links.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by MacGyver View Post
    How about writing an entire program out as requested?

    Then after that, check the flags your IDE has set for when it compiles and links.
    This is the code in question. I have however narrowed it to a problem with notepad, using the > operator in cmd to redirect stdout to primes.txt gives the same results.

    I also removed optimization from the compiling process - No change.

    Implementation of Sieve of Eratosthenes.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(int argc, char **argv)
    {
        
        unsigned int limit = 100000000, *digits = malloc(sizeof(unsigned int)*limit), idx, idx1 = 1, max = sqrt(limit), store = 0;
        
       
        fprintf( stderr, "Generating primes.. ");
        
        for(idx = 0; idx < limit; idx++)
                digits[idx] = idx1+=2;
                
        for(idx = 0; idx < max; idx++)
        {
                if(digits[idx] == 0) continue;
                
                store = digits[idx];
                for(idx1 = idx; idx1 < limit; idx1 += store)
                {
                         if(digits[idx1] == store) continue;
                         
                         digits[idx1] = 0;
                }
                         
        }
        
        
        fprintf( stderr, "Done!\n");
        fprintf( stderr, "Writing primes to file.. ");
        
        
        FILE *out = fopen("primes.txt", "w+");
        
    
        
        for(idx = 0; idx < 1000; idx++) {
                
                if(digits[idx] != 0) { fprintf( stdout,  "&#37;d ", digits[idx]); }
                
        }
             
        fclose(out);
        
        fprintf( stderr, "Done!\n");
        
        
        
        
        free(digits);
                
                
        
        
    }

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is nothing to do with your code. Notepad is buggy in how it determines whether a file is unicode or not and in this case has gotten it wrong.

    I came across this myself one day by opening Notepad and typing in an innocent short English sentence, closed and reopened the file, and got this problem. It is reported in various places all over the net. I think one of the more well known examples is something like "Bush hid the facts", which comes up in chinese.

    Notepad ultimately sux!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by iMalc View Post
    This is nothing to do with your code. Notepad is buggy in how it determines whether a file is unicode or not and in this case has gotten it wrong.

    I came across this myself one day by opening Notepad and typing in an innocent short English sentence, closed and reopened the file, and got this problem. It is reported in various places all over the net. I think one of the more well known examples is something like "Bush hid the facts", which comes up in chinese.

    Notepad ultimately sux!
    Yes, this I had discovered already, as you can see in my previous post.

    Anyway, is there any way to prevent this? This has never happened before, and tbh, changing file extension associations to make wordpad default is an annoying thing to do when I only need a text application to view simple, unformatted text.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It seems that notepad needs at least ONE \n within the first 500 or so characters to recognise the file as a text file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I wouldn't bash Notepad just yet guys, I recently used it to scan through 75 MB of binary and text, both in unicode and ASCII (and some UTF-8) and it perfomed pretty well.
    What I had to do when encountering parts that were Unicode, etc was:
    1) Open notepad.exe,
    2) File->Open
    3) Set File type to All Files and Encoding to the encoding that you expect the file to be in.
    It should work fine now.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or use a hex editor and you can see all the characters (or their hex values) even if Notepad doesn't show them. Then you can see pretty fast whether it's in Unicode or not.

  14. #14
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by @nthony View Post
    I wouldn't bash Notepad just yet guys, I recently used it to scan through 75 MB of binary and text, both in unicode and ASCII (and some UTF-8) and it perfomed pretty well.
    What I had to do when encountering parts that were Unicode, etc was:
    1) Open notepad.exe,
    2) File->Open
    3) Set File type to All Files and Encoding to the encoding that you expect the file to be in.
    It should work fine now.
    Ah, thank you. It seems it was defaulting to Unicode encoding when I opened the file(Using "notepad textfile" from command line), but when I opened it with UTF-8, it shows what it's supposed to show.

    thanks =)

  15. #15
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    If there is no BOM, Notepad tries to figure the encoding of the text, it does that by creating a histogram of the text and comparing it to histograms of different encodings. not a stupid program after all.
    because the file you created is short and not typical text it is type casted with the wrong encoding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM