From: hamishd on
I have the following 4 lines of data in a text file which I want to
read (note the large number of spaces in line2):
..5,-90,50
"¥ª
°¼
",0,50
-15630,166,205,0
0,0

CStdioFileEx file; CString str;
CString Data[10];

file.Open(_T("c:\\file.txt"), CFile::modeRead){
while(file.ReadString(str)){
Data[i++] = str;
}
file.Close();
}

But it doesn't read the 2nd line correctly... here's the result
Data[0] = .5,-90,50
Data[1] = "¥ª
°¼
"
Data[2] = 0
Data[3] = -15630,166,205,0
Data[4] = 0,0

It should be:
Data[0] = .5,-90,50
Data[1] = "¥ª
°¼
",0,50
Data[2] = -15630,166,205,0
Data[3] = 0,0

From: Joseph M. Newcomer on
The exmples below don't seem to illustrate much. Perhaps you should try to illustrate the
spaces with some printable character.

Try to avoid putting more than one declaration per line.

Also, you would be better served by using

CStringArray Data;

and just doing Data.Add(string)

to append the data to the array.
joe

On Thu, 21 Jun 2007 19:21:08 -0700, hamishd <Hamish.Dean(a)gmail.com> wrote:

>I have the following 4 lines of data in a text file which I want to
>read (note the large number of spaces in line2):
>.5,-90,50
>"��
>��
>",0,50
>-15630,166,205,0
>0,0
>
>CStdioFileEx file; CString str;
>CString Data[10];
>
>file.Open(_T("c:\\file.txt"), CFile::modeRead){
> while(file.ReadString(str)){
> Data[i++] = str;
> }
> file.Close();
>}
>
>But it doesn't read the 2nd line correctly... here's the result
>Data[0] = .5,-90,50
>Data[1] = "��
>��
>"
>Data[2] = 0
>Data[3] = -15630,166,205,0
>Data[4] = 0,0
>
>It should be:
>Data[0] = .5,-90,50
>Data[1] = "��
>��
>",0,50
>Data[2] = -15630,166,205,0
>Data[3] = 0,0
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: MrAsm on
On Thu, 21 Jun 2007 19:21:08 -0700, hamishd <Hamish.Dean(a)gmail.com>
wrote:

>I have the following 4 lines of data in a text file which I want to
>read (note the large number of spaces in line2):

If your file contains Unicode characters, don't expect CStdioFile to
work.

MrAsm
From: MrAsm on
On Thu, 21 Jun 2007 19:21:08 -0700, hamishd <Hamish.Dean(a)gmail.com>
wrote:

>CStdioFileEx file; CString str;

BTW: you wrote CStdioFile in the subject, but it seems that you are
actually using CStdioFileEx... please don't be misleading in the
subject :)


>But it doesn't read the 2nd line correctly... here's the result
>Data[0] = .5,-90,50
>Data[1] = "��
>��
>"

Maybe your line is longer than the maximum storage for line buffer
used by CStdioFileEx? (I don't know how big the line buffer is for
CStdioFileEx, you may read the source...and maybe modify considering
your own needs.)

Or maybe you are reading a file which stores Unicode characters
encoded with method X (e.g. UTF-8), but CStdioFileEx is expecting
Unicode Y (e.g. UTF-16LE). There are several Unicode encodings:
UTF-16LE, UTF-16BE, UTF-32LE, UTF32-BE, UTF-8...

[The more robust and versatile CStdioFile extension I've seen is by a
friend who post in this group ;) Mister T. - his implementation checks
the BOM, etc.]

I tend to prefer a lot UTF-8 to store Unicode strings to files and
read them back, so I don't have CPU endiannes problems; moreover UTF-8
tends to be the "standard" Unicode format for Internet communications
and things like XML...
Inside the app, I use native Windows Unicode (UTF-16), and I convert
UTF-8 <-> Windows UTF-16 on app boundaries.

MrAsm
From: MrAsm on
On Fri, 22 Jun 2007 01:16:42 -0400, Joseph M. Newcomer
<newcomer(a)flounder.com> wrote:

>Also, you would be better served by using
>
>CStringArray Data;
>
>and just doing Data.Add(string)

I completely agree.

With modern C++ templates like std::vector or MFC containers, I see no
need to use old-C style arrays [*], without bounds checking (they are
prone to buffer overruns and attacks, so they tend to be fragile code
elements).

[*] There would be very special cases, of course, but uncommon.

MrAsm
 |  Next  |  Last
Pages: 1 2 3
Prev: Debug Assertation Failed
Next: Runtime error-help plz.