|
You can't be serious. Before posting here, trying reading the documentation for the objects you are using.
get_array is a CStringArray, which (as the name hints) is an array of strings.
file represents a... file. GetLength does what is says - it gets the length of the file.
You've just written...
"An array of strings" = 973.
You could always try and compile your code to find out if your syntax is correct. Mr Pallini's time is more precious than your computers.
What CPallini told you is to read the file one line at a time, and add it to the CStringArray.
If you look at CStringArray, it has a member function called Add, which takes a string or TCHAR *. You need to use that inside some sort of loop.
Also, if you look at CStdioFile instead of CFile, it has a friendly method called ReadString, which makes reading from a text file much simpler than reading from a CFile.
Hopefully you should be able to put something together now.
Good luck!
Iain.
|
|
|
|
|
Hi,
I need to determine and anticipate when the exception 'Out of memory' occur. The first easy case is scanning the virtual memory size due to 2GB limtation in 32Bit OS (or 3GB, depends of the config).
But, this exception can occur in other circumstances. I suspect that the limit value 'Commit Charge' is reached. I don't know how to get this value.
Somebody can help me in this case ?
Thanks
André Rios
|
|
|
|
|
If you're being paranoid (which is what we do - I'm not criticising!), then the only answer to give is...
"at any time, for no good reason".
After all, you could check for nice ambient conditions for your software, then 1s later, some other program goes on a ram-guzzling spree.
Iain.
|
|
|
|
|
Hi,
Thanks for your response !
But I don't think what Windows handle the 'Out of Memory' exception "at any time, for no good reason"...
The problem is : I don't know how to get the 'Commit charge' values showed by the TaskManager. I'm sure that is a/the way to determine this case.
Somebody can help me to get these values ?
André
|
|
|
|
|
Hi all,
I add a file name(a string) into a buffer as follows.
int iL = strlen(m_strTemp.c_str()) + 1;
char *buff = new char[iL];
strcpy(buff, m_strTemp.c_str());
Then later I want to get that string from a buffer. So I do the following.
string *pp = new string(buff);
But gives an error on strlen.asm file as follows.
Unhandled exception at 0x102157d0 (msvcr71d.dll) in GroupServer.exe: 0xC0000005:
Access violation reading location 0x00000000.
Can you guys found where I'm wrong.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Did you use your debugger to check where the code crashes exactly ? On which exact line does it crash ?
|
|
|
|
|
Yes, I do. Error got in line where I copied the buffer into string,
string *pp = new string(buff);
I appreciate your help all the time...
Eranga
|
|
|
|
|
What is the value of buff ?
What happens exactly between the line were you alocate buff and this line ?
BTW, you really should learn how to use a debugger efficiently: try to inspect your variables see if everything is correct. Try putting breakpoints relevant places in your code and step into it to see what happens before the crash.
|
|
|
|
|
I copied your code snippet and run. It works well though.
Maxwell Chen
|
|
|
|
|
Not for me.
Actually one thing to say. I've add name to the buffer in one member function, and get a string from one member function in the same class.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Actually one thing to say. I've add name to the buffer in one member function, and get a string from one member function in the same class.
Any threading in your program?
Maxwell Chen
|
|
|
|
|
No sir, I don't use any threading there. Just only that part have in my code.
Seems I can't use the buffer in the other function, which I'm not define the buffer.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Then later I want to get that string from a buffer. So I do the following.
what does exactly mean later on (is there a chance that buff var was overwritten in the meantime?)?
(i.e. follow Cedric Moonen suggestion and post intermediate code too).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
CPallini wrote: what does exactly mean later on (is there a chance that buff var was overwritten in the meantime?)?
No, at a time buffer not overwrite. Actually it is because of my incomplete question. As I said my last post, I've add name into the buffer in one member function and get the name from another member function.
Define the buffer globally and used.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Since you've also declared buff inside the assigning method, then it is lost when method terminates (it goes out of scope). You probably have a global buff that is never initialised (hence the error), and a local one, that is fully initilised and then lost for ever.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Ok, actually I've try to just print the buffer elements within the buffer adding function. It works. Then I try to display elements within the second function. It wont display. Debugger point that buffer is empty. Actually I don't delete the buffer until it use in the second function.
That mean, there is an issue with the scope of the buffer.
I've declared the buffer in .h file as follows.
public:
char* buff;
and I suppose in this way, I can use it globally. Is that wrong. If so, how can I define the buffer globally.
I appreciate your help all the time...
Eranga
|
|
|
|
|
If you declared buff that way (i.e. a class member), then you can indeed use it inside all class methods (and also outside the class methods, if you have a reference to a class instance).
But if you declare a local instance of buff inside such methods, then the local instance will hide the class member one.
If you really need a global variable, though, then you have to declare it global, i.e. scoped in the global namespace, outside any class declaration.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Ok, I'll try it and let you know what happened on my code.
I appreciate your help all the time...
Eranga
|
|
|
|
|
I have done it as you said.
Declared a buffer outside of the class definition, i mean just after the include statements of the *.cpp file. Then I done the work using it. After doing all the process, delete the buffer inside the destructor.
Everything fine.
Now my question is that why it is not work, define in the class and use it? You have any comments for me?
I appreciate your help all the time...
Eranga
|
|
|
|
|
In fact, You can also use for the purpose buff as class member, provided you access it in class methods (or, if you are outside the class context, you have an instance of the class and buff is declared public ).
You code failed because inside a method (or a function) you declared again buff and the latter, local declaration shadowed the class member one so that you initialised local instance of buff but then accessed the (uninitialised) class member one.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Ok, thanks a lot pal. Now I got the point. At the same time. One of friend says that, as a good practice in programming, as much as possible it is better to avoid global variables and used member variables. Is that true?
Why I'm asking is, one is that I'm really new for C++, two is I most familiar with Java and there is no such a way, so that those tips can be really helpful to me
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: Ok, thanks a lot pal. Now I got the point. At the same time. One of friend says that, as a good practice in programming, as much as possible it is better to avoid global variables and used member variables. Is that true?
Well, I'm not dogmatic about, but yes, I suggest you to use sparingly global variables.
Eranga Thennakoon wrote: Why I'm asking is, one is that I'm really new for C++, two is I most familiar with Java and there is no such a way, so that those tips can be really helpful to me
Well if you're familiar with Java then you should be familiar with OOP principles too, such encapsulation and information hiding that both go against global variables usage.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
CPallini wrote: Well if you're familiar with Java then you should be familiar with OOP principles too, such encapsulation and information hiding that both go against global variables usage.
Yep, thats true. In any language SECURITY must be the most important word
I appreciate your help all the time...
Eranga
|
|
|
|
|
Hello everyone,
Bjarne mentioned in his book, that there is no way to catch exceptions from the initialization of global variables (section 14.7). But I do not think it is true, because we can add function try block to its constructor, and catch appropriate types of exceptions or using catch(...) to catch all exceptions.
Am I correct? Any comments?
thanks in advance,
George
|
|
|
|
|
Why don't you try by yourself?!
Maxwell Chen
|
|
|
|