Thread: Output to file produces crap

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Output to file produces crap

    This is driving me crazy, I have the following program:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    	ofstream myfile;
    	myfile.open("text.txt");
    	for(int i = 1; i < 9; i++)
    		for(int j = 1; j < 9; j++)
    		{
    			myfile << "x" << i << j << " + ";
    		}
    	myfile.close();
    }
    Which should just create a file text.txt and put there the string:
    x11 + x12 + .... + x88
    What is actually outputs is:
    Code:
    ㅸ‱‫ㅸ′‫ㅸ″‫ㅸ‴‫ㅸ‵‫ㅸ‶‫ㅸ‷‫ㅸ‸‫㉸‱‫㉸′‫㉸″‫㉸‴‫㉸‵‫㉸‶‫㉸‷‫㉸‸‫㍸‱‫㍸′‫㍸″‫㍸‴‫㍸‵‫㍸‶‫㍸‷‫㍸‸‫㑸‱‫㑸′‫㑸″‫㑸‴‫㑸‵‫㑸‶‫㑸‷‫㑸‸‫㕸‱‫㕸′‫㕸″‫㕸‴‫㕸‵‫㕸‶‫㕸‷‫㕸‸‫㙸‱‫㙸′‫㙸″‫㙸‴‫㙸‵‫㙸‶‫㙸‷‫㙸‸‫㝸‱‫㝸′‫㝸″‫㝸‴‫㝸‵‫㝸‶‫㝸‷‫㝸‸‫㡸‱‫㡸′‫㡸″‫㡸‴‫㡸‵‫㡸‶‫㡸‷‫㡸‸‫
    If I remove " + " it outputs the actual string without pluses, otherwise it outputs crap. Why?
    Last edited by kulfon; 12-04-2012 at 10:37 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What OS/compiler?
    How are you viewing the resulting text file?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Very strange.
    Seems to work exactly as expected (gcc, Linux)
    Code:
    $ cat bar.cpp
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
      ofstream myfile;
      myfile.open("text.txt");
      for(int i = 1; i < 9; i++)
        for(int j = 1; j < 9; j++)
        {
          myfile << "x" << i << j << " + ";
        }
      myfile.close();
    }
    $ g++ bar.cpp
    $ ./a.out 
    $ cat text.txt 
    x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x21 + x22 + x23 + x24 + x25 + x26 + x27 + x28 + x31 + x32 + x33 + x34 + x35 + x36 + x37 + x38 + x41 + x42 + x43 + x44 + x45 + x46 + x47 + x48 + x51 + x52 + x53 + x54 + x55 + x56 + x57 + x58 + x61 + x62 + x63 + x64 + x65 + x66 + x67 + x68 + x71 + x72 + x73 + x74 + x75 + x76 + x77 + x78 + x81 + x82 + x83 + x84 + x85 + x86 + x87 + x88 + $
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    19
    Quote Originally Posted by hk_mp5kpdw View Post
    What OS/compiler?
    How are you viewing the resulting text file?
    Visual Studio 2010 Professional
    and I am just using notepad to view the file

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your code works for me too (also gcc/Linux). Some suggestions: make sure you're not mixing up + and <<. If I put e.g.
    Code:
    myfile << "x" << i << j + " + ";
    then j will be indexing into the string " + " and you'll get some garbage characters, but not as many as you're seeing.

    Be absolutely certain you're compiling the code correctly. Delete the old executable and text.txt and recompile the code. Make a new project if you're paranoid.

    Some suggestions that may get around compiler bugs: try the code without using namespace std. Some compilers have an older fstream implementation in the global namespace and it's possible (though it shouldn't be happening) that you're picking up an operator << from the old implementation.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
     
    int main()
    {
        std::ofstream myfile;
        myfile.open("text.txt");
        for(int i = 1; i < 9; i++)
            for(int j = 1; j < 9; j++)
            {
                myfile << "x" << i << j << " + ";
            }
        myfile.close();
    }
    Finally, if it's really the " + " that's causing the problem, try putting that onto another line.
    Code:
                myfile << "x" << i << j;
                myfile << " + ";
    This would probably be an indication of a broken compiler.
    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.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Ok, this is a quirk of Notepad. I have the same setup as you do and my output file looked weird when viewing it in Notepad. I can open the file in the Visual Studio IDE itself (File->Open->File) and it looks fine. It also looks fine when I browse to the folder where the text file is located and open it using WordPad or MS Word. Notepad is known for some occasional idiosyncrasies.

    I knew I had seen something like this before... doing some research revealed this. It may be you've stumbled across something similar.

    [edit]
    Ok, here's how you get Notepad to view it correctly. With Notepad open go to File->Open and browse to the folder/file you wish to open. Before double-clicking on the file to open it go to the "Encoding" drop down box and select "ANSI" or "UTF-8" instead of the default of "Unicode". Now you can double-click on the file and have it open and display as it should.
    [/edit]
    Last edited by hk_mp5kpdw; 12-04-2012 at 01:31 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Des encryption produces wrong output
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-16-2012, 07:33 AM
  2. program compiles, runs, but produces no output
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 05:43 AM
  3. Build Produces no C#
    By Xarzu Athanasop in forum C# Programming
    Replies: 0
    Last Post: 01-09-2008, 12:16 PM
  4. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM
  5. output produces funky characters
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2002, 08:14 PM