|
How are you writing said string to file?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
One solution can be that every time you insert a new string into file, you insert it in a new line. Now you can get every string using getline(...) function. How's that?
Good luck
We Believe in Excellence
www.aqueelmirza.cjb.net
|
|
|
|
|
One Classical way of doing this is to read the string twice! First time you read it, just read it into a buffer of one char, and count the chars.(I assume you want to read a zero terminated string.) In that case, you know you reached the End of the string when you read a 0. At that stage, you have a Character count, which is the size of the string you want to read. Now you can allocate an ample buffer, and read the lot. Simple enough? Yes but No.
Unfortunately, for reasons long since lost in the mist of history, not all characters always consume one byte. Our friends in Japan and China may have a word or two to say about that, but, atleast in their case it is usefull to their language. In the western case, it is a Hangover from the past. The long and short of it is, that the End of a Line in a Text file is recorded as a \r\n two byte sequence. (This is how it is recorded on your Disk).
When you read it back from your disk into a Buffer, the \r is stripped from the file, and all you will find in your buffer is a single \n.
This has important consequences, the main one being that the offset of a character in the buffer is potentially different to the offset of the same position in the File.
There is a way around all this, the translation mechanism depends on how you open the underlaying file.
If you open a file as fopen("File","r") or fopen("File","w")the Library will insert, or delete the \r bytes when reading or writing between buffer and file.
If you open a file as fopen("File","rb") or fopen("File","wb")the Library read or write between buffer and file without translation.
All tends to work well, if you read a file in the same mode it was written by, and if you do not do daft things, such as trying to count chars in a file. Armed with the above knowledge, I suggest you get creative, and do the needed
LateNightsInNewry
|
|
|
|
|
Hey guys,
I am receiving this error in VC++
testing_project.obj : error LNK2001: unresolved external symbol "bool __cdecl GetRSLocation_Reg(char *)" (?GetRSLocation_Reg@@YA_NPAD@Z)
C:\testing_project.exe : fatal error LNK1120: 1 unresolved externals
</pre
Where <code>char *</code> is a typedef for <code>LPSTR</code>
These are the libraries I have included in my project:
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib comctl32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
Here are my project options
<pre>
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib comctl32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/testing_project.pdb" /debug /machine:I386 /out:"C:\testing_project.exe" /pdbtype:sept
Anyone know what the problem could be?
Thanks in advance!
Robbie
|
|
|
|
|
I fixed it! In my function definition parameters, I was passing a value by reference but in my function prototype I wasnt. Sorry my bad lol.
|
|
|
|
|
Whenever I do:
CString userInput;
CString device,
cmd,
input1,
input2;
int i;
i = _stscanf(userInput,_T("%s %s %s %s"), &device, &cmd, &input1, &input2);
I get this warning:
c:\...cpp(72) : warning C4996: 'swscanf' was declared deprecated
c:\program files\microsoft visual studio 8\vc\include\wchar.h(809) : see declaration of 'swscanf'
Message: 'This function or variable may be unsafe. Consider using swscanf_s instead.
So, I run my application everything prints ok. Then when I goto close my application I get:
"An unhandled win32 exception occurred in LEDs_try2.exe [2860]."
I then click to debug then I get:
Unhandled exception at 0x783252f8 (mfc80ud.dll) in LEDs_try2.exe: 0xC0000005: Access violation writing location 0x0035002e.
-AND-
MS Visual Studio opens up the file: atlsimpstr.h
Points to line 109 which is:
"if( _AtlInterlockedDecrement( &nRefs ) <= 0 )"
Would please someone explain to me what I'm doing wrong?
Thanks.
Kitty5
|
|
|
|
|
I think you should allocate a space for your destination strings, using GetBuffer :
i = _stscanf(userInput,_T("%s %s %s %s"),
device.GetBuffer(256),
cmd.GetBuffer(256),
input.GetBuffer(256),
input2.GetBuffer(256));
device.ReleaseBuffer();
cmd.ReleaseBuffer();
input.ReleaseBuffer();
input2.ReleaseBuffer();
In GetBuffer specify a size wich is enough for your problem.
Hope it help.
|
|
|
|
|
1. initialize your variables !!!!!!
CString userInput<font color=red> = ""</font>;
CString device<font color=red> = ""</font>,
cmd<font color=red> = ""</font>,
input1<font color=red> = ""</font>,
input2<font color=red> = ""</font>;
int i<font color=red> = ""</font>;
2. use the CString::GetBuffer() method to allow a C method to write in your CString...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: CString userInput = "";
CString device = "",
Doing that with CString objects does nothing but waste time. They initialize to an empty state and do not normally need to be initialized like that. If you do, you end up basically doing a copy operation to copy an empty string.
When you use GetBuffer(...) to write into CString object, remember to use ReleaseBuffer(...) to ensure that the CString correctly handles the changes.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: Doing that with CString objects does nothing but waste time
waste time ????
hey, this calls the copy constructor, so how can it loose time.
moreover, it's much clean to do so, ,even is the default constructor already creates an empty CString.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: waste time ????
Yes.
The default constuctor (in VC++ 6.0 at least, the ATL version does stuff a bit different) does something more than just creating an empty CString . There is something called afxEmptyString that is used as the shared reference to an empty string.
Normal construction basically just sets the internal data object pointer to the shared empty string's data object.
toxcct wrote: moreover, it's much clean to do so, ,even is the default constructor already creates an empty CString.
Initializing the object as in your example does not call the copy constructor, and would even be more ineffecient if it did because it would have to create a temporary CString object, initialize it, count the characters in the target string (zero in this case), and if a non-zero count, dynamically allocate memory for the string for it and assign it into the temporary object, and then call the copy constructor using the temporary object. Again, it would "waste time".
Initialzing with a static string causes a overloaded constructor to be used (an optimization to eliminate the need to create a temporary as shown above). This constructor initializes just like the default constructor, counts characters in the target string, and allocates memory and copies the string over if necessary.
All of these things take time. When time is taken for no good reason, it is wasted time. Hence, it wastes time. I hope this clears things up for you.
I understand why some people are ignorant about this - not everyone actually takes a look at what goes on behind the scenes. Having a look at the implementation of CString::CString(LPCTSTR lpsz) and CString::CString() will clear things up.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Just FYI:
Initialization (or lack thereof) had nothing to do with the original poster's problem.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
FYMNI, my post was a reply to someone other than the OP. My post addressed the incorrect parts of the provided solutions, as well as provided some background that will hopefully get others to learn more and thus become better developers.
You see, some of us are capable of learning something even if provided out of context, and some... as recently demonstrated... simply are not.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
James R. Twine wrote: FYMNI, my post was a reply to someone other than the OP. My post addressed the incorrect parts of the provided solutions, as well as provided some background that will hopefully get others to learn more and thus become better developers.
You see, some of us are capable of learning something even if provided out of context, and some... as recently demonstrated... simply are not.
I wasn't trying to discount what you were saying (nor the discussion) ... just mentioning that the original problem had nothing to do with this line of discussion.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Zac Howland wrote: I wasn't trying to discount what you were saying (nor the discussion) ... just mentioning that the original problem had nothing to do with this line of discussion.
Understood... Tone lowered...
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
You are going to have a couple problems here:
1) When using scanf-derivatives, the %s directive doesn't work quite like you would expect it to. You will probably want to use the %[] directives with the appropriate delimiter, or better yet, write a simple split method that takes a delimiter and splits a CString into a CStringArray. This part of the error will just give you incorrect data.
2) The scanf-methods (as with all C methods) were not designed to work with class objects. You have to give it a pointer to a character buffer. You can either declare a set of char[]'s, or use the GetBuffer/ReleaseBuffer calls appropriately:
CString userInput = _T("");<br />
char device[20] = {0};<br />
char cmd[20] = {0};<br />
char input1[20] = {0};<br />
char input2[20] = {0};<br />
int i = 0;<br />
<br />
i = _stscanf(userInput,_T("%[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9]"), device, cmd, input1, input2);
or, alternatively:
CString userInput = _T("");<br />
CString device = _T("");<br />
CString cmd = _T("");<br />
CString input1 = _T("");<br />
CString input2 = _T("");<br />
int i = 0;<br />
<br />
i = _stscanf(userInput,_T("%[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9] %[a-zA-Z0-9]"), device.GetBuffer(256), cmd.GetBuffer(256), input1.GetBuffer(256), input2.GetBuffer(256));<br />
<br />
device.ReleaseBuffer();<br />
cmd.ReleaseBuffer();<br />
input1.ReleaseBuffer();<br />
input2.ReleaseBuffer();<br />
Passing in the address of a CString object will lead to a stack-corruption and hence your Access Violation.
The old-style C methods (printf, scanf, etc) have been deprecated in VS.Net 2005 (can't remember if they were also in 2003). There is a link in the MSDN documentation that describes their replacements. You can either use those replacements (and avoid the warnings), pragma out the deprecated warnings, or write a split function (which is what it looks like you really want in this case anyway).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
God I love you guys!
Kitty5
|
|
|
|
|
I created my own CPropertySheet which is CMyPropertySheet and I created my own CPropertyPage, CMyPropertyPage. CMyPropertyPage is a superclass for my pages. It provides access to CMyPropertySheet which is run from my App like a dialog program runs it's dialog. So CMyPropertySheet holds my state information for filling out the pages as they gather information from the user. I am misunderstanding my compiler because I have the #ifndef __mypage_h__ ... #endif markup so that my files are only included once and still if I add #include "MyPropertySheet.h" to MyPropertyPage.h my project recurses the includes until it gives up. Naturally (to me at least), the sheet knows of the pages so that the sheet can make itself, and the page knows of the sheet so it can store info.
I tried prototyping the classes but I must be doing something improper. An example of a working relationship like this would save me two Aspirins and a morning.
|
|
|
|
|
I ran in to a saomewhat similar situation and to resolve it I removed the include of "MyPropertySheet.h" from 'MyPropertyPage.h" and use a forward reference for CMyPropertySheet instead. Are you able to use a forward reference instead of including the file?
|
|
|
|
|
Thanks, you set me on the path to a buildable project. For anyone who runs into a similar situation, here's what I did.
In CMyPropertyPage's header file, I forward reference "class CMyPropertySheet;", and in CMyPropertyPage's source file, I include CMyPropertySheet (MyPropertySheet.h). Be careful, when forward referencing you cannot use any operations on CMyPropertySheet in the header file. That was throwing me off.
Case closed.
|
|
|
|
|
I'm trying to create a .dll using ATL, which I want use in C#.
Can anybody please guide me to create a simple method / function which takes string in input parameters and returns the same(string)..
I tried a lot but always faced problem of returing string back to calling function in C#.
I did the same with C++ class library successfully but when it comes to ATL its not the same.
As I'm using other SDK API's in creation of .dll and I can't go with Class Library its having problem with "CLR and MTd". Which can't go together. Finally I decided to stick to ATL.
Now please guide me to create the methods which I can use in C# to get the string value.
Arun
|
|
|
|
|
Write a C++ program to implement this pointer object
Define Class customer containing
Code , Name , Billtotal and member function
Accept()
print()
In the main function declare two or multiple object of the above class
and find out which object contains highest billtotal and print that object
at end , use function prototype given below
customer Highbilltotal( customer &)
Implement object passing as referene and returning object
|
|
|
|
|
|
joyalfernandes wrote: Write a C++ program to implement this pointer object
1. i never talked to you that bad, so change your language please.
2. here at codeproject, people aren't paid for their help, so no one owe you anything.
3. Homework are bad seen here. if you have some homework to do, it's because you have to learn how things are done, so don't ask people to do it for you, this is child thinking.
Think about your problem first, and only then, ask for specific questions if you can do it yourself.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
|