There's no way to specify the orientation of a file opened by CreateFile, it's always binary. ReadFile and WriteFile work with raw bytes.
>What I'm trying to do is to open file in binary mode and then to write
>strings so that cannot be opened and examined with Notepad.
Printable characters in the character set will always be readable by Notepad, whether you're using binary or text orientation. If you want your strings to be unreadable, you need to encrypt them somehow rather than just write them straight to the file:
#include <windows.h>
int main ( void )
{
HANDLE out = CreateFile ( "test.txt", FILE_WRITE_DATA, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
char s[] = "This is a test";
int i, j;
int n;
for ( i = 0, j = sizeof s - 1; i <= j; i++, j-- ) {
s[i] ^= s[j] ^ 194837U;
s[j] ^= s[i] ^ 3876U;
}
WriteFile ( out, s, sizeof s, &n, NULL );
CloseHandle ( out );
return 0;
}
However, you can directly write numbers and other non-string data without the encryption, and it will be unrecognizable when opened with Notepad:
#include <windows.h>
int main ( void )
{
HANDLE out = CreateFile ( "test.txt", FILE_WRITE_DATA, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
int i = 12345;
int n;
WriteFile ( out, &i, sizeof i, &n, NULL );
CloseHandle ( out );
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>OPEN_ALWAYS, // existing file only
OPEN_ALWAYS creates the file if it doesn't exist. If you want CreateFile to fail if the file doesn't exist, use OPEN_EXISTING.
>but program ended up in an infinite loop
That's because with the way you open the file, reaching end-of-file still returns success.
>Can you suggest me other, better, way of doing this if this is not OK
Well, there's nothing wrong with it, so it all boils down to style preference.
>Couldn't you just do something like this?
Sure, but where's the loop? ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I am not sure if writing strigs in binary mode is a good idea.
Only if you write the file with CreateFile and WriteFile, then read it using a FILE* opened with text orientation. Then it's possible to have conversion issues depending on the contents of the strings. But as long as the file is consistently read and written as binary, there's no problem.
>Either you will end up writing the memory locations to the file
No, you wouldn't. WriteFile takes a pointer and writes the contents of that pointer, so the only way to get WriteFile to write the addresses is if you had an array of pointers representing the strings and passed it like this:
char *lines[N];
...
WriteFile ( h, lines, N * sizeof *lines, &n, NULL );
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
yes, its true that with M$ compilers fopen and fstream eventually use win32 api functions. But there are a lot of reasons not to use win32 api for reading/writing text files and buffered i/o. Using either fstream or fgets() its really easy to read just one line of text (up to CR/LF), but win32 api its not so easy because you, the programmer, have to duplicate the functionality of fgets().
Bottom line: use win32 api directly only if speed is important to your program. Otherwise use either fstream or FILE and associated functions. Personally, I hate fstream because I think its ugly and very clumbsy. The only advantage is getline() used with std::string object. But for formatted i/o you can't beat the ease of fprintf() and fscanf().
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
the easiest way is that you save the file with the extension of (.bin) instead of (.txt)
The file extension has nothing to do about file contents. Changing the file extension from .txt to .bin will do nothing (other than renaming the file)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>extension (.bin) means that the file is in binary mode
No it doesn't, as the code you posted illustrates. The file extension has nothing to do with whether the file is in binary or text mode. Your own program proved it.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343