TPoly
asked on
Search, Read and Write to file.
Question:
How do we Write to an external file and get the info on it using C++? The following source is seen below.
There is a problem more on the writing part as when after it is written to the external file and when its read, it only displays only the first letter of the word.
== Source Code ==
if (wcsstr(pspvEmail->Value.l pszW, L"98494778") != NULL)
{
MessageBeep(MB_ICONASTERIS K);
MessageBox(NULL, pspvSubject->Value.lpszW, pspvEmail->Value.lpszW, MB_OK);
HANDLE hFile;
const TCHAR* filename = TEXT("\\My Documents\\Personal\\jobs. txt"); // Name of file to be written
const TCHAR* file_contents = pspvSubject->Value.lpszW; // String to be written to file
TCHAR message[255]; // String to be displayed in message box
DWORD bytesWritten;
// Open existing file or create it if it does not exist
hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
MessageBox(NULL,TEXT("Coul dn't create the file!"), TEXT("Create File ERROR"), MB_OK);
else
{
// Write a string to the file
if (WriteFile(hFile, file_contents , wcslen(file_contents)*size of(TCHAR), &bytesWritten, NULL)!=0)
{
wsprintf(message, TEXT("Success - String %s written to file %s."), file_contents, filename);
}
else
{
wsprintf(message, TEXT("Error writing to file: %d"), GetLastError());
}
// Display the message indicating the result
MessageBox(NULL, message, TEXT("Result"), MB_OK);
// Close file handle
CloseHandle(hFile);
}
// Delete the message and mark it as handled so it won't show up in Inbox
hr = DeleteMessage(pMsgStore, pMsg, cbMsg, lpMsg, cbDestFolder, lpDestFolder, pulEventType, pHandled);
}
else
{
// a 'normal' message, pass it on
*pHandled = MRC_NOT_HANDLED;
}
== End of Source Code ==
How do we Write to an external file and get the info on it using C++? The following source is seen below.
There is a problem more on the writing part as when after it is written to the external file and when its read, it only displays only the first letter of the word.
== Source Code ==
if (wcsstr(pspvEmail->Value.l
{
MessageBeep(MB_ICONASTERIS
MessageBox(NULL, pspvSubject->Value.lpszW, pspvEmail->Value.lpszW, MB_OK);
HANDLE hFile;
const TCHAR* filename = TEXT("\\My Documents\\Personal\\jobs.
const TCHAR* file_contents = pspvSubject->Value.lpszW; // String to be written to file
TCHAR message[255]; // String to be displayed in message box
DWORD bytesWritten;
// Open existing file or create it if it does not exist
hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
MessageBox(NULL,TEXT("Coul
else
{
// Write a string to the file
if (WriteFile(hFile, file_contents , wcslen(file_contents)*size
{
wsprintf(message, TEXT("Success - String %s written to file %s."), file_contents, filename);
}
else
{
wsprintf(message, TEXT("Error writing to file: %d"), GetLastError());
}
// Display the message indicating the result
MessageBox(NULL, message, TEXT("Result"), MB_OK);
// Close file handle
CloseHandle(hFile);
}
// Delete the message and mark it as handled so it won't show up in Inbox
hr = DeleteMessage(pMsgStore, pMsg, cbMsg, lpMsg, cbDestFolder, lpDestFolder, pulEventType, pHandled);
}
else
{
// a 'normal' message, pass it on
*pHandled = MRC_NOT_HANDLED;
}
== End of Source Code ==
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want to leave the file as UNICODE, you need to write the byte markers at the start of the file so a text editor will know it is UNICODE. The bytes are 0xFFFE for 16-bit little-endian. See
http://www.unicode.org/unicode/faq/utf_bom.html#BOM
for more details.
http://www.unicode.org/unicode/faq/utf_bom.html#BOM
for more details.
ASKER
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_2bj9.asp
I've read on the above site and it indicate that its not safe to use the WideCharToMultiByte method due to security reasons.
== Quote from MSDN site ==
Remarks
Security Alert Using the WideCharToMultiByte function incorrectly can compromise the security of your application. Calling the WideCharToMultiByte function can easily cause a buffer overrun because the size of the In buffer equals the number of WCHARs in the string, while the size of the Out buffer equals the number of bytes.
== End of Quote ==
Actually I don't mind using any other code to write to a file. I can don't use the unicode. I need only any example that can show me on how to write to a file. Thanks.
I've read on the above site and it indicate that its not safe to use the WideCharToMultiByte method due to security reasons.
== Quote from MSDN site ==
Remarks
Security Alert Using the WideCharToMultiByte function incorrectly can compromise the security of your application. Calling the WideCharToMultiByte function can easily cause a buffer overrun because the size of the In buffer equals the number of WCHARs in the string, while the size of the Out buffer equals the number of bytes.
== End of Quote ==
Actually I don't mind using any other code to write to a file. I can don't use the unicode. I need only any example that can show me on how to write to a file. Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This are the errors that was generated from the source code.
We are using Embedded Visual C++
== Error Generated ==
c:\documents and settings\tp\desktop\projfi les\mapiru le 13-8\mapirule\mapirule.cpp (616) : error C2440: 'initializing' : cannot convert from 'char *' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\documents and settings\tp\desktop\projfi les\mapiru le 13-8\mapirule\mapirule.cpp (618) : error C2664: 'WideCharToMultiByte' : cannot convert parameter 5 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tp\desktop\projfi les\mapiru le 13-8\mapirule\mapirule.cpp (622) : error C2664: 'WriteFile' : cannot convert parameter 2 from 'char' to 'const void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
== End of Error ==
We are using Embedded Visual C++
== Error Generated ==
c:\documents and settings\tp\desktop\projfi
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\documents and settings\tp\desktop\projfi
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\tp\desktop\projfi
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
== End of Error ==
This looks that at some point you try assign a char pointer to a char and at another you try to do the reverse. Check whether both need to be char* and change the variable type accordingly.
Its in itsmeandnobodyelse's code:
char buf = new char[wlen * 2 +1]; // should be enough
should be
char* buf = new char[wlen * 2 +1]; // should be enough
and don't forget to delete[] that pointer after using it.
char buf = new char[wlen * 2 +1]; // should be enough
should be
char* buf = new char[wlen * 2 +1]; // should be enough
and don't forget to delete[] that pointer after using it.
ASKER
Thanks for your fast reply..
There isn't any error now but the problem is that the output to the file is blank.
Nothing is written inside the file.
There isn't any error now but the problem is that the output to the file is blank.
Nothing is written inside the file.
STeH,
thanks for correcting my bugs ;-)
TPoly,
could you debug the function? Is there any value in pspvSubject->Value.lpszW or file_contents? What about buf? Is there any contents before WriteFile?
Regards, Alex
thanks for correcting my bugs ;-)
TPoly,
could you debug the function? Is there any value in pspvSubject->Value.lpszW or file_contents? What about buf? Is there any contents before WriteFile?
Regards, Alex
ASKER
== The codes from my question ==
// Write a string to the file
if (WriteFile(hFile, file_contents , wcslen(file_contents)*size of(TCHAR), &bytesWritten, NULL)!=0)
{
wsprintf(message, TEXT("Success - String %s written to file %s."), file_contents, filename);
}
else
{
wsprintf(message, TEXT("Error writing to file: %d"), GetLastError());
}
== End ==
The messagebox contains the value of file_contents.
And the file_contents has the value that we want (its a string).
example of what is inside file_contents: 123, 18/8/2004 13:45, 123,
I'm not quite sure about the buf. I don't know if there is anything contained inside the bluf.
// Write a string to the file
if (WriteFile(hFile, file_contents , wcslen(file_contents)*size
{
wsprintf(message, TEXT("Success - String %s written to file %s."), file_contents, filename);
}
else
{
wsprintf(message, TEXT("Error writing to file: %d"), GetLastError());
}
== End ==
The messagebox contains the value of file_contents.
And the file_contents has the value that we want (its a string).
example of what is inside file_contents: 123, 18/8/2004 13:45, 123,
I'm not quite sure about the buf. I don't know if there is anything contained inside the bluf.
>>>> if (WriteFile(hFile, file_contents ,
writes the UNICODE string (buffer) to file. When opening that with any text editor you only see the first character as explained above...
So you _HAVE_ to convert the file_contents buffer (it's a wide string where any character is a 16 bit short integer) to buf (that's a 'normal' string where any character is an 8 bit char) _AND_ use that 'buf' variable when calling WriteFile
if (WriteFile(hFile, buf, strlen(buf) + 1, ...
Regards, Alex
>> if (WriteFile(hFile, buf, strlen(buf) + 1, ...
It's a text file so you should not write the string terminator - don't use +1 with strlen(buf).
It's a text file so you should not write the string terminator - don't use +1 with strlen(buf).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i tried using this method
=== alex's code===
int len = strlen(buf);
buf[len] = '\n';
buf[++len] = '\0';
if (WriteFile(hFile, buf, len, ... ))
{
...
===end===
now the result:
the contents of the is only a line feed...
=== alex's code===
int len = strlen(buf);
buf[len] = '\n';
buf[++len] = '\0';
if (WriteFile(hFile, buf, len, ... ))
{
...
===end===
now the result:
the contents of the is only a line feed...
ASKER
actually i had posted another question regarding passing a value from this compile dll(which is a dll trying to intercept the incoming SMS from the Inbox of a POCKET PC, so that the SMS is not handled by the Inbox) directly to an VB.NET application, however there is no reply for that question..
thus i try coding this dll and write the value into an external file (eg. jobs.txt) in a POCKET PC (the file format i tried are .pwi , .txt , .csv)..
jobs.txt act as a middleman between the dll and the VB.NET application...
thus at the VB.NET application side, it will reads the jobs.txt and gets the contents, use split() to break the string into parts and insert into a ListView object..
the above three components( dll, jobs.txt and VB.NET application) all resides in the POCKET PC...
thus i try coding this dll and write the value into an external file (eg. jobs.txt) in a POCKET PC (the file format i tried are .pwi , .txt , .csv)..
jobs.txt act as a middleman between the dll and the VB.NET application...
thus at the VB.NET application side, it will reads the jobs.txt and gets the contents, use split() to break the string into parts and insert into a ListView object..
the above three components( dll, jobs.txt and VB.NET application) all resides in the POCKET PC...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That sequence
USES_CONVERSION;
char* buf = W2A(pspvSubject->Value.lps zW);
int len = strlen(buf);
buf[len] = '\n';
buf[++len] = '\0';
if (WriteFile(hFile, buf, len, &bytesWritten, NULL)!=0)
...
....
CloseHandle(hFile);
should definitively give some valid output if
pspvSubject->Value.lpszW
contains a UNICODE string (with printable characters) that isn't empty.
Did you check the values with the Debugger?
Regards, Alex
USES_CONVERSION;
char* buf = W2A(pspvSubject->Value.lps
int len = strlen(buf);
buf[len] = '\n';
buf[++len] = '\0';
if (WriteFile(hFile, buf, len, &bytesWritten, NULL)!=0)
...
....
CloseHandle(hFile);
should definitively give some valid output if
pspvSubject->Value.lpszW
contains a UNICODE string (with printable characters) that isn't empty.
Did you check the values with the Debugger?
Regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for all your help...
learn alot man...
learn alot man...
ASKER
i had posted another project related problem at this question title at VB.NET..
Question Title: Sending SMS to a GSM phone that is connected to a PC COM port
hope u guys can further help...
your help will be greatly appreciated...
Question Title: Sending SMS to a GSM phone that is connected to a PC COM port
hope u guys can further help...
your help will be greatly appreciated...
if (WriteFile(hFile, buf, len, &bytesWritten, NULL)!=0)