473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
3 7058
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.