|
There are built in library and MFC functions to perform this. Please explain what you mean by "change string to uppercase using integer values"?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hint: There's an offset of 32 between lower and upper cased letters. For example, the ascii value (int) of lower case 'c' is 99, and the same of upper case 'C' is 67.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
The integer difference between a lowercase char and a uppercase char when subtracted is 32.
simply subtract the lowercase by 32 to get a uppercase char.
#include <stdio.h>
#include <string.h>
int main(void)
{
char strchr[32] = "the quick fox";
int strlength=strlen(strchr);
printf("Before\n" );
printf("%s\n\n", strchr);
for (int i=0; i<strlength; i++)
{
if((strchr[i] >= 97) && (strchr[i] <= 122))
{
strchr[i] = strchr[i] -32;
}
}
printf("After\n" );
printf("%s\n\n", strchr);
return(0);
}
|
|
|
|
|
Using hexadecimal rather than decimal integer values makes the code much more readable.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
TopCoder23 wrote: char strchr[32] = "the quick fox"; I would consider a different variable name as strchr is the name of a CRT function.
TopCoder23 wrote: if((strchr[i] >= 97) && (strchr[i] <= 122)) What about:
if (islower(strchr[i]))
strchr[i] = toupper(strchr[i]);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
Hi,dear all,
I have a project created using C++ 6.0. Now I need to update it to VS2008, now I have a problem when I try to open a file.
In C++ 6.0
#include <fstream.h>
ifstream INPFile(INPFileName, ios::nocreate);
In VS2008
#include <fstream>
ifstream INPFile(INPFileName, ios::nocreate);
the second line get the following error:
Error 23 error C2039: 'nocreate' : is not a member of 'std::basic_ios<_Elem,_Traits>'
Error 24 error C2065: 'nocreate' : undeclared identifier
If I change the second line to the following:
ifstream INPFile(INPFileName)
then get new error as following
error 2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
How can I solve this issue?
Thanks!
|
|
|
|
|
This talks about the reason for the first error:
http://www.devx.com/tips/Tip/14544[^]
After your change however, the error about the private member doesn't make sense, you probably need to show a bit more of your code.
|
|
|
|
|
Albert, thanks for your quick reply.
My project has around 50 files, I already search all header files to check if any function pass fstream as argument, and modify them all to be passed as reference instead of as object.
I don't understand why if I use following code:
ifstream fin(OutputF, ios::in,ios::nocreate);
I got error like "C2039: 'nocreate' : is not...", but if I modify it to:
ifstream fin(OutputF, ios::in,ios::_Nocreate);
I got other kind of error messge:
"error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private.."
Thanks!
|
|
|
|
|
Wait, if you're doing an ifstream (input), why are you specifiying "no create" at all? That doesn't make sense, it's not going to get created if it can't find it. That's only even applicable at all if you're opening in read/write or write modes.
|
|
|
|
|
I do not think the nocreate option exists anymore, and it should not be necessary on an input stream. You need to use the latest definitions as described here[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks, Albert and Richard,
Both of you are right, yes, there is no nocreate.
I solve it by remove it, but the main reason I got the " cannot access private member ..." error is that I have some functions that pass ifstream object as value, they should be passed by reference.
Thanks again!
|
|
|
|
|
Hi,
I solve my issue posted yesterday, but today has another problem related to it.
When I migrate another small project from C++ to VS2008, I double check all the function delcaration to make sure fstream object is passed by reference or pointer, but when I compile program, I still got two error messages, they are same and the only two errors.
C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
In this case how should I find where the error come from?
|
|
|
|
|
I found some functions that pass std::istream or std::istrstream by value, not by reference, is that the reason to cause the problem?
|
|
|
|
|
Yes, they also need to be passed by reference, now can be compile now.
|
|
|
|
|
I would suppose that passing the fstream by value in 6.0 was wrong too. It might have worked but only by luck.
A fstream is a representation of a computer resource. Passing by value implicitly duplicates the representation without duplicating the actual resource. Thus it represents a broken design/implementation.
|
|
|
|
|
PLease show me how to search for a word in a text file using a listbox, String objects and Indexof, after i opened it in a richtextbox ,in visual C++, please help!!
|
|
|
|
|
byank wrote: PLease show me how to search for a word in a text file using a listbox...
What does a listbox have to do with searching a file?
Are you using MFC or Win32?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
it's just that i've seen here on codeproject the code for searching words in a text files using vb, i think. I need it in c++. thanks
|
|
|
|
|
I understand that, but what does a listbox have to do with anything? It just seems to be "noise" in the overall problem.
Have you tried:
open the file
while not EOF
read line from file
if line contains text, exit loop
end
close file The details deopend on whether you are using MFC, STL, Win32, or some combination thereof.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
If you are searching for a string in a text file do this:
CString oneLine; bool flag = FALSE; CStdioFile fS (_path_, CFile::modeRead ); int lineNumber = 0; while( fS.readLine(oneLine)) {
if( oneLine.Find(L"String")!= -1 ) { flag = TRUE; break; }
lineNumber++; }
See also:
Find
CStdioFile
|
|
|
|
|
Shaunak De wrote: If you are searching for a string in a text file...
Which I'm not, but thanks anyway, though.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
tahnks for your help ...i need this code in visual c++ and i use visual c++ 2008/2010. could you help me with this? I place the word that i want to search in a textbox and after i click on a button(search) i want to see where the word is. The file is loaded in a richtextbox . thanks a lot
|
|
|
|
|
|
your help was appreciated but what i wana do is to write the word in a text box and after i click on a button(search) it will show me where the word is (in my richtextbox where the text file is loaded) all these in my windows form. any idea? thanks a lot
|
|
|
|