Problems with <new line> even if I use "\r\n" - UNICODE

M

MIUSS

Hello everyone!

I got a problem with creating new line...

I tried this: lstrcpy(NewLineIdr, TEXT("\r\n"));
I already tried this: NewLineIdr[0] = 0x000D; NewLineIdr[1] =
0x000A; NewLineIdr[2] = 0x0000;

I need it to use it in my saving procedure because I need to create
tabullary text file. But my effort usually results in one or two small
rectangles meaning probably unknown characters...

More interesting is one thing:

This is a part of my first func providing saving to file with new
line. It works well:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
lstrcpy(NewLine, TEXT("\r\n")); lstrcpy(Space, TEXT(" "));
lstrcpy(ParC[0], TEXT("ID: ")); lstrcpy(ParC[1],
TEXT("Datum: ")); lstrcpy(ParC[2], TEXT("ID_zakazky"));
lstrcpy(ParC[3], TEXT("PocetHodin")); lstrcpy(ParC[4],
TEXT("Popis "));

lpString = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, ((5*10)/*Polozky*/
+(4*3)/*Mezery*/+3+2 +40/*delka popisku*/)*sizeof(TCHAR));

Buf[0] = 0xFEFF; Buf[1] = 0x0000; // First two TCHARS informing
that file uses UNICODE
lstrcpy(lpString, Buf); // Copies unicode-inform-TCHARs to
initiation of string

for(a=0;a<5;a++){ // Copies adapted ParCS strings to
lpString pointer
lstrcat(lpString, ParC[a]);
if(a!=4) lstrcat(lpString, Space);
}
for(a=0;a<4;a++) lstrcat(lpString, TEXT("Popis "));
lstrcat(lpString, NewLine);

hSoubor = CreateFile(szSoubor, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
0, NULL);

if(hSoubor == INVALID_HANDLE_VALUE){
HeapFree(GetProcessHeap(), 0, lpString);
MessageBox(g_hWnd, TEXT("An error in writing file occured. Save
failed!"), TEXT("Read/Write file error"), MB_ICONERROR);
return;
}
if(!WriteFile(hSoubor, lpString, (((5*10)/*Polozky*/+(4*3)/*Mezery*/+
40/*delka popisku*/ +3)*sizeof(TCHAR)), &dwZapsano, NULL)){
MessageBox(g_hWnd, TEXT("An error in writing file occured. Save
failed!"), TEXT("Read/Write file error"), MB_ICONERROR);
}
return;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


And this is a part of my second func that should save to text file
with new lines:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GetWindowText(hWndID, PolozkaProZapis[0], 11);
GetWindowText(hWndDatum, PolozkaProZapis[1], 11);
GetWindowText(hWndIDzakazky, PolozkaProZapis[2], 11);
GetWindowText(hWndPocetHodin, PolozkaProZapis[3], 11);
GetWindowText(hWndPopis, PolozkaProZapis[4], 51);

lstrcpy(Space, TEXT(" "));

DelkaStr=lstrlen(PolozkaProZapis[4]);
for(a=0;a<(50-DelkaStr);a++) PolozkaProZapis[4][a+DelkaStr] = 0x0020;
PolozkaProZapis[4][49] = 0x0000;

for(a=0;a<4;a++){
DelkaStr = lstrlen(PolozkaProZapis[a]);
for(b=0;b<DelkaStr;b++){
TempPolozka[b+(10-DelkaStr)] = PolozkaProZapis[a];
}
for(b=0;b<(10-DelkaStr);b++){
TempPolozka = 0x0020;
}
TempPolozka[10]=0x0000;
lstrcpy(PolozkaProZapis[a], TempPolozka);
}

lstrcpy(Line, TEXT(""));
for(a=0;a<5;a++){ lstrcat(Line, PolozkaProZapis[a]); lstrcat(Line,
Space);}

lstrcpy(NewLineIdr, TEXT("\r\n")); //NewLineIdr[0] = 0x000D;
NewLineIdr[1] = 0x000A; NewLineIdr[2] = 0x0000;
lstrcat(Line, NewLineIdr);lstrcat(Line, TEXT(""));

DelkaLinky = lstrlen(Line);

if((stream = _wfopen(szSoubor, L"a")) != NULL){
nw = fwrite(&Line, sizeof(TCHAR), DelkaLinky, stream);
}
fclose(stream);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I use almost the same way to add new line to string, but when I use
fwrite instead of writefile I get two rectangles instead of lines when
I look to saved file in notepad.... How is it possible?

Any help will be appreciated...

Have a nice day.
 
J

John Harrison

MIUSS said:
Hello everyone!

I got a problem with creating new line...

I tried this: lstrcpy(NewLineIdr, TEXT("\r\n"));
I already tried this: NewLineIdr[0] = 0x000D; NewLineIdr[1] =
0x000A; NewLineIdr[2] = 0x0000;

I need it to use it in my saving procedure because I need to create
tabullary text file. But my effort usually results in one or two small
rectangles meaning probably unknown characters...

More interesting is one thing:

This is a part of my first func providing saving to file with new
line. It works well:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
lstrcpy(NewLine, TEXT("\r\n")); lstrcpy(Space, TEXT(" "));
lstrcpy(ParC[0], TEXT("ID: ")); lstrcpy(ParC[1],
TEXT("Datum: ")); lstrcpy(ParC[2], TEXT("ID_zakazky"));
lstrcpy(ParC[3], TEXT("PocetHodin")); lstrcpy(ParC[4],
TEXT("Popis "));

lpString = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, ((5*10)/*Polozky*/
+(4*3)/*Mezery*/+3+2 +40/*delka popisku*/)*sizeof(TCHAR));

Buf[0] = 0xFEFF; Buf[1] = 0x0000; // First two TCHARS informing
that file uses UNICODE
lstrcpy(lpString, Buf); // Copies unicode-inform-TCHARs to
initiation of string

for(a=0;a<5;a++){ // Copies adapted ParCS strings to
lpString pointer
lstrcat(lpString, ParC[a]);
if(a!=4) lstrcat(lpString, Space);
}
for(a=0;a<4;a++) lstrcat(lpString, TEXT("Popis "));
lstrcat(lpString, NewLine);

hSoubor = CreateFile(szSoubor, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
0, NULL);

if(hSoubor == INVALID_HANDLE_VALUE){
HeapFree(GetProcessHeap(), 0, lpString);
MessageBox(g_hWnd, TEXT("An error in writing file occured. Save
failed!"), TEXT("Read/Write file error"), MB_ICONERROR);
return;
}
if(!WriteFile(hSoubor, lpString, (((5*10)/*Polozky*/+(4*3)/*Mezery*/+
40/*delka popisku*/ +3)*sizeof(TCHAR)), &dwZapsano, NULL)){
MessageBox(g_hWnd, TEXT("An error in writing file occured. Save
failed!"), TEXT("Read/Write file error"), MB_ICONERROR);
}
return;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


And this is a part of my second func that should save to text file
with new lines:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GetWindowText(hWndID, PolozkaProZapis[0], 11);
GetWindowText(hWndDatum, PolozkaProZapis[1], 11);
GetWindowText(hWndIDzakazky, PolozkaProZapis[2], 11);
GetWindowText(hWndPocetHodin, PolozkaProZapis[3], 11);
GetWindowText(hWndPopis, PolozkaProZapis[4], 51);

lstrcpy(Space, TEXT(" "));

DelkaStr=lstrlen(PolozkaProZapis[4]);
for(a=0;a<(50-DelkaStr);a++) PolozkaProZapis[4][a+DelkaStr] = 0x0020;
PolozkaProZapis[4][49] = 0x0000;

for(a=0;a<4;a++){
DelkaStr = lstrlen(PolozkaProZapis[a]);
for(b=0;b<DelkaStr;b++){
TempPolozka[b+(10-DelkaStr)] = PolozkaProZapis[a];
}
for(b=0;b<(10-DelkaStr);b++){
TempPolozka = 0x0020;
}
TempPolozka[10]=0x0000;
lstrcpy(PolozkaProZapis[a], TempPolozka);
}

lstrcpy(Line, TEXT(""));
for(a=0;a<5;a++){ lstrcat(Line, PolozkaProZapis[a]); lstrcat(Line,
Space);}

lstrcpy(NewLineIdr, TEXT("\r\n")); //NewLineIdr[0] = 0x000D;
NewLineIdr[1] = 0x000A; NewLineIdr[2] = 0x0000;
lstrcat(Line, NewLineIdr);lstrcat(Line, TEXT(""));

DelkaLinky = lstrlen(Line);

if((stream = _wfopen(szSoubor, L"a")) != NULL){
nw = fwrite(&Line, sizeof(TCHAR), DelkaLinky, stream);
}
fclose(stream);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I use almost the same way to add new line to string, but when I use
fwrite instead of writefile I get two rectangles instead of lines when
I look to saved file in notepad.... How is it possible?

Any help will be appreciated...

Have a nice day.


That's becuase you are opening the file in text mode. Do you know the
difference between text mode and binary mode?

Either open the file in binary mode by using "ab"

if((stream = _wfopen(szSoubor, L"ab")) != NULL){

or use "\n" instead of "\r\n" for a new line

lstrcpy(NewLineIdr, TEXT("\n"));

john
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello everyone!

I got a problem with creating new line...

I tried this: lstrcpy(NewLineIdr, TEXT("\r\n"));

I've not worked with Unicode but in general it's best to use just \n
and the library/implementation usually takes care of the rest. Since I
have not read your code I don't know if you use any specific unicode-
functions to work with the text or not. If you are working on raw
bytes you might be interested in using LS (0x2028) instead, which is
the Unicode character for linebreaks (Line Separator).

For more information the Unicode Newline Guidelines might be useful:
http://unicode.org/reports/tr13/tr13-9.html
 
M

MIUSS

Hello John!

Thank you very much!

The difference between text and binary mode I know, but I've had not
idea that I can't use \r\n when I'm opening the file in text mode.
However, I used your advice and now the last part of line "\r\n" I
save in binary mode like this:

NovaLinka[0] = 0x000D; NovaLinka[1] = 0x000A; NovaLinka[2] = 0x0000;

if((stream = _wfopen(szSoubor, L"ab")) != NULL){
nw = fwrite(&NovaLinka, sizeof(TCHAR), 2, stream);
}
fclose(stream);

And it works! I really appreciate your help, now I can continue in
work:)

Have a nice day!

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top