By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,135 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,135 IT Pros & Developers. It's quick & easy.

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

P: n/a
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][b];
}
for(b=0;b<(10-DelkaStr);b++){
TempPolozka[b] = 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.

Mar 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
MIUSS wrote:
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][b];
}
for(b=0;b<(10-DelkaStr);b++){
TempPolozka[b] = 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
Mar 1 '07 #2

P: n/a
On Mar 1, 7:34 am, "MIUSS" <m...@seznam.czwrote:
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

--
Erik Wikström

Mar 1 '07 #3

P: n/a
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!

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
John Harrison wrote:
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
Mar 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.