简体   繁体   中英

MSVC++ 2008 text file parsing difference between win7 <-> xp

Work environment

  • Windows 7 Ultimate X64 SP1
  • Microsoft Visual Studio 2008 SP1 (C++)

Text file

My program is parsing pure text-file and running. the next code is that text-file.

/*main.txt*/

_function main()
{
    a = 3;      //a is 3
    b = "three";
    c = 4.3;    //automatic typecast
    _outd(b+"3\n");
    _outd(b+a+c);
    d = a/c;
    _outd("\n"+d+"\n"); ///test commantation
    /*//
    comment
    */
}

it is a some kind of simple c-styple script that editable at a text editor like notepad.(the contents in this text is not important)

Code

Text open code.

std::wstring wsFileName; //from 

char* sz_FileName = new char[wsFileName.size()+1];
ZeroMemory(sz_FileName,sizeof(char)*(wsFileName.size()+1));

//////////////////////////////////////////////////////////////////////////
//for using 'fopen', change wsFileName to ascii
WideCharToMultiByte(CP_UTF8,0,wsFileName.c_str(),-1,sz_FileName,wsFileName.size(),NULL,NULL);

//open ascii text file
FILE* fp=fopen(sz_FileName,"rt");

std::string ch;
while(!feof(fp))
{
     ch.push_back(fgetc(fp));   //get textfile in ascii
}

Problem

Watching std::string ch in debugger that:

在此处输入图片说明

That is exactly same i expected result in Windows 7. And the released-execution-file is work fine too. But in Windows XP SP3, it invokes a runtime error.

I dug around a whole night, and finally I found it. I installed the visual studio 2008 sp1 on windows xp sp3 and debugged it.

Watching std::string ch :

在此处输入图片说明

I guessed the "newline mark" is the reason why that program crashed in windows xp. and i guessed this problem based 'text encoding between windows 7 and windows xp'. so i make the new script text using windows xp's notepad. and try to parse it. But the windows xp has the same problem. i know that : The problem occurs not only the text file orignated windows 7 but also a new generated text in windows xp. i don't know why the problem happens.

Question

a pure text file (generated by notepad, editplus, ultraedit, and others...) that generated any windows os(xp & vista & 7), how my program can read that text same result?
i want each user can edit script to control and customize my program in their own OS, using their own simple text editor.

Why don't test the char before adding to the string if it contains a unwanted character? Eg something like:

char temp;
while(...)
{
  temp = fgetc(fp);
  if (temp != "\r")
    ch.push_back(temp);
}

I haven't tested the code, but it should work, if the "wrong" char is indeed "\\r".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM