Inconsistency with running command 'iconv' through commandline vs C++ application

139 Views Asked by At

I am trying to convert a windows file CP1252 format into UTF-8 format for parsing in a C++ application. I use the following command :

iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf

When I try running the same command in a C++ application using :

system("iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf");

some of the characters are not encoded properly. For eg. the German characters ü, ö, ä are encoded as �

I understand that the operator > is a shell feature and I think using it in a C++ operation causes this discrepancy.

I have also tried using popen("iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf", "r"); but to no success.

Is there any other way to redirect the conversion to out.ldf without using the > operator in a C++ application.

EDIT: the question linked to be as duplicate is a completely different problem as to one addressed here.

int main (int argc, char* argv[])
{
    string FileName = "Invalid";
    if (argc == 2) {
        FileName = argv[1];
        system("iconv -f CP1252 -t UTF-8" + FileName + "|dos2unix > out.ldf");
        //system("iconv -f CP1252 -t UTF-8 file.ldf |dos2unix > out.ldf");
        //do further parsing on file                        
    }
    else
        cout << "ERROR:: invalid number of arguments"<< endl;
    return 0;
}


//file.ldf -- windows file (CP1252 format)
physical_value, 0, 254, 0.5, -20, "�C";

//out.ldf -- after conversion using 'iconv' command on the command line
physical_value, 0, 254, 0.5, -20, "üC";

//out.ldf -- after conversion using the 'system' API
physical_value, 0, 254, 0.5, -20, "�C";
0

There are 0 best solutions below