Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I am very new in C++ and I have question about console output. As practice I have wrote code for reading text from file line by line and output that to console window. In Visual Studio I'm getting normal results, but in Code::Blocks 20.03 after each character I have whitespace character inserted.
Visual Studio 2019: HERRERA FOR MEN 100ML 1936 7450,00
Code::Blocks 20.03: H E R R E R A F O R M E N 1 0 0 M L 1 9 3 6
Could someone explain to me why is this happening?
Thanks in advance.

What I have tried:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readfromfile(string filenamestring)
{
    fstream newfile;
    newfile.open(filenamestring,ios::in);
    if(newfile.is_open())
    {
        string textline;
        while(getline(newfile,textline))
        {
            cout<<textline<<"\n";
        }
        newfile.close();
    }
}

int main()
{
    string filename="I:\\KREZA\\Podaci.txt";
    readfromfile(filename);
}
Posted
Comments
0x01AA 2-May-22 11:32am    
That is most propably a Unicode problem.See e.g. here: How to make CB editor and Windows agree on a character set?[^]
Gruja82 2-May-22 12:00pm    
After adding this line as first line in main(): system("cls"); everything works fine.
jeron1 2-May-22 12:23pm    
Can't say I saw that one coming...
Michael Haephrati 9-Aug-22 11:01am    
Try opening the file in ios::binary mode.

1 solution

My first guess is that you have a Unicode/ANSI mismatch. Probably the file is encoded in Unicode which means that, for the conent which you showed, every other byte is a zero. You read this into a string which in essence is a sequence of bytes, and then output to cout which writes every byte as a separate character.

The solution is to replace fstream, string, and cout by wfstream, wstring ('textline' variable only), and wcout. This will treat pairs of bytes as 16-bit Unicode characters. As far as I understand the documentation, the file name itself can remain ANSI.

If you want to dive deeper into this matter, start with Wikipedia articles about Unicode and character encoding.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900