|
-
Oct 29th, 2001, 02:55 PM
#1
Thread Starter
Hyperactive Member
Chr$(10) equivalent ??
what would be the equialent of Chr$(10) in c++... in visual basic typing Chr$(10) would give a linefeed... How I would go about writing an ascii value to a textbox so that it shows as text and not the integer value??
thanx
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 02:59 PM
#2
Member
cout << (char)10 works, I believe.
-
Oct 29th, 2001, 03:07 PM
#3
Thread Starter
Hyperactive Member
i'm sure it does but i need to use this in a win32 application, not in a console app... the reason why i need this is because Edit Controls don't recognize the line feed char as a its supposed to, instead it recongnizes it as a symbol... what i need to do is search thru a given string and replace all of the linefeeds with carriage returns... i've tried using \lf, and replacing it with \r but the edit control only recongnizes them as symbols therefore i need to do them by thier direct ascii values... but i don't know how in c++, in vb its like this Replace(String, chr$(10), chr$(13))
thanx..
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 03:10 PM
#4
Member
You can still use my technique (called casting, BTW). So...
Code:
char letterVersion;
letterVersion = (char)10;
Then you can play with letterVersion if you want.
-
Oct 29th, 2001, 03:11 PM
#5
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Oct 29th, 2001, 03:22 PM
#6
Thread Starter
Hyperactive Member
ok definetly getting closer now... i never thought of doing "\10" or "\13" and it does work but the edit box still doesn't recognize it as a an actual carriage return, it spits out a symbol how would i go about outputting a carriage return to the edit control... i've tried \n, \lf, \r, \10 which is more than likely the same thing as \lf and i've tried \13 which is more than likely \r... any suggestions ??
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 03:51 PM
#7
Thread Starter
Hyperactive Member
ok this is weird, i went into visual basic and attempted to grab the ascii of a textbox.. i tried hitting enter and it gave me 13 ofcourse, but then i tried setting the textbox text to vbcrlf and the ascii return is nothing why ?? what is the ascii equivalent of vbcrlf in visual basic??
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 04:28 PM
#8
First off - just because there are control character(s) in a file does not mean the code has to bring all of them in to your program.
BASIC, C, and other languages selectively discard some characters from input streams. VbCrLf makes a newline - it isn't just one character. In C \n is a new line, even if it pretends to be just one character. \r is ascii 13 \t is ascii 9. But \n gets written to PC files as \13\10
The reason for this is that different machines use different underlying characters as control codes. C is old, and was written when this control code thing was really horrible, so it had to make up a way to deal with it. VB is new, stays in one OS family, and so go along with what the OS is doing and not have problems.
-
Oct 29th, 2001, 04:52 PM
#9
DOS/windows text files use a \r\n (carriage return/linefeed) pair to mark a new line. Unix uses only \n. console always uses only \n. I think edit boxes use \r\n, if you have only either \n or \n it will show as a symbol. I haven't tested this, but I think that's the way it is.
This is also the reason why there is the text mode and the binary mode in fopen(). If opened in text mode in dos or windows, fgets() or fscanf will replace any \r\n pairs with a single \n. It could be \n\r btw, I don't know.
And I think \13 and \10 are the same as \n and \r, though I don't know which is which. I think I'll try.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2001, 04:53 PM
#10
Thread Starter
Hyperactive Member
first off thanx for the explanation... but the problem wasn't really understanding the history of the character codes... the problem is adding a carriage return properly to an edit control... i wasn't thinking when i posted that last post about vbcrlf since it stand for carriagereturn, line feed.. but how come when i attempt SetWindowText(hwndoftext, "\13") it simply outputs a symbol which resembles a block.. ?? i need to convert the linefeeds in a string of text to carriage returns because some txt files that i'm loading were created in Unix/Linux/Dos/Win.. its a whole variety of Os's that these files were created in... i don't want to edit the file directly either, i just simply want to edit the text that insides the variable...
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 04:59 PM
#11
Thread Starter
Hyperactive Member
ok umm "\r\n" adds a new line to a textbox.. i had tried "\13\10" but it hadn't worked and then i tried "\r\n" and it worked... now i'm having some problems with illegal opertions when trying to replace the characters but i haven't looked into enough yet and am still trying to find the problem.. thanx for all the help everyone...
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 05:09 PM
#12
Thread Starter
Hyperactive Member
ok perhaps you all could help me with this illegal operation
when i attempt to use the code and try replacing all "\n" with "\r\n" i get an illegal operation..
Code:
char* replacestr(const char* str,const char* crt,const char* rep,char*trg){
char* temp=trg;
for(;*str; ){
for(const char*i=crt,*j=str;*j&&*i&&*i==*j;j++,i++);
if (!*i){
for(i=rep;*i;*trg++=*i++)str++;
str=j;
}else
*trg++=*str++;
}
*trg=0;
return temp;
};
i am calling the code as follows
Code:
replacestr(pszFileText, "\r", "\r\n", pszFileText);
pszfiletext is defined as LPSTR pszFileText and contains the data from a previously loaded txt file..
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 05:16 PM
#13
Thread Starter
Hyperactive Member
oops i forgot to add the function i am using to load the text file...
Code:
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, NULL, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0;
replacestr(pszFileText, "\n", "\r\n", pszFileText);
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE;
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
i believe the problem is the GlobalAlloc function which i believe locks the memory that the data from the file resides in and thus trying to edit this it performs and illegal opertion... the program doesn't perform an illegal operation unless a "\n" is actuall in the text file...
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 29th, 2001, 05:27 PM
#14
I'd say the problem is that \r\n are wo characters, while \n is just one. You just allocate enough memory for the exact file length (you should use HeapAlloc btw) and if you want to replace a \n by a \r\n the string is too long for the memory, causing an access violation.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2001, 05:39 PM
#15
Thread Starter
Hyperactive Member
ahhaa i didn't think of that LOL and u are right.. what i did was define a new variable with a buffer size of [9999] just for test purposes and used that as the target string for the replaced text and it worked... now i guess i guess i could loop thru first string and find the number of occurences of "\n" and create a buffer of the size of the original string plus the number of occurences of "\n", i think that would work..
what do u think i should do ??
This Business Is Binary. Your a 1 or a 0. Alive or Dead. (AntiTrust)
-
Oct 30th, 2001, 06:03 AM
#16
transcendental analytic
replacestr is my function. It does assume that you have enough buffer to store the result.
All kinds of similar algoritms that has a variable output length can assume to increase performance but will be unsafe to use; if you're making a text editor that has to handle all kinds of input and output ranges that can be fatal.
A solution would be to count the amount of linefeeds before the allocating the buffer, another would be to let replacestr to handle resizable strings; which isn't in good because in general you want to be able to operate on the stack as well. And if you reallocate often, even once in the latter part the copying process will be slower than a search algoritm itself.
To automate the process of counting occurances of search criteria and multiply it with length of rep-length of crt, and as well save performance by generating an array of pointers to the found substrings in the source string, I could split up the algoritm in two functions, one that will be search, and the other replace. Search will fill an array with found results and return the number of total founds for the sake if the array gets filled. Replace will then process a target string with the replacements using the found elements array. Then you could safely allocate the buffer and pass it as target.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|