| | |
Windows API functions to read and write files in C
![]() |
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Hello, I'm playing with win API functions readFile and WriteFile.
Is it possible to open file using CreateFile in binary mode just like using fopen standard C function.
I tried with function WriteFile to write string to a file but it seems that file is opened in textual mode. 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.
Thank you very much
Is it possible to open file using CreateFile in binary mode just like using fopen standard C function.
I tried with function WriteFile to write string to a file but it seems that file is opened in textual mode. 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.
Thank you very much
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:
However, you can directly write numbers and other non-string data without the encryption, and it will be unrecognizable when opened with Notepad:
>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:
C Syntax (Toggle Plain Text)
#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; }
C Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Thanks Narue, I understand now
please, can you check if this is a correct way of reading file:
first, file is open for both writing and reading
second I want to read contents of a file and display it on screen (I know that file contents are sentences (strings) like
"This is first sentence"
"This is second sentence"
so this is code for reading:
I had one nasty problem, At first I tried this:
but program ended up in an infinite loop (not like fwrite in C) and that way with dw_bytes_read it appears to work fine.
Can you suggest me other, better, way of doing this if this is not OK
Thanks
please, can you check if this is a correct way of reading file:
first, file is open for both writing and reading
C Syntax (Toggle Plain Text)
hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, // share for reading NULL, // no security OPEN_ALWAYS, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr
"This is first sentence"
"This is second sentence"
so this is code for reading:
C Syntax (Toggle Plain Text)
while(ReadFile(hFile, buf, sizeof buf, &dw_bytes_read, NULL) && dw_bytes_read != 0) { WriteFile(hStdOut, buf, dw_bytes_read, &dw_bytes_written, NULL); }
I had one nasty problem, At first I tried this:
C Syntax (Toggle Plain Text)
while(ReadFile(hFile, buf, sizeof buf, &dw_bytes_read, NULL)){...
Can you suggest me other, better, way of doing this if this is not OK
Thanks
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Couldn't you just do something like this? Remeber if this sucks I am not good at the API 
;

C Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> int main() { char buf[30] = {'\0'}; //Null terminate LPVOID lpMsgBuf; DWORD byteWritten = 0; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); BOOL ReadFileReturn; HANDLE hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, // share for reading NULL, // no security OPEN_EXISTING, // existing file only FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr ReadFileReturn = ReadFile(hFile,buf,30,&byteWritten,NULL); if(ReadFileReturn) { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL); WriteFile(hStdOut,buf,sizeof buf,NULL,NULL); } else { WriteFile(hStdOut,"It Failed",sizeof "It Failed",NULL,NULL); } std::cin.get(); return 0; }
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
>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?
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?
I'm here to prove you wrong.
•
•
Join Date: Jun 2005
Posts: 28
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Micko
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.
Either you will end up writing the memory locations to the file, which would not be useful in the second time around when you open it or you will end up writng human readable stuff any way.
I think what you are looking for is some encrypting mechanism so that you could hide the strings from the others.
----------------------
Programming ( Assignment / Project ) Help
>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:
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:
C Syntax (Toggle Plain Text)
char *lines[N]; ... WriteFile ( h, lines, N * sizeof *lines, &n, NULL );
I'm here to prove you wrong.
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
Thank you all for replies. Usually I do all file manipulation stuff with fopen, fwrite, fread and etc, but I wanted to know how this is done with API. Also I've read somewhere that C function fopen end up on CreateFile (under windows os) and that "would be wise" to use it directly to gain better performance. I don't know if this is true, I doubt it.
Thanks again
Thanks again
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().
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().
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Can we allow the user to make errors
- Next Thread: How to display graphics in 256 color in C
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter changingto char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory fflush file fork forloop frequency givemetehcodez global grade gtkgcurlcompiling hacking highest histogram homework i/o inches infiniteloop input interest iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match meter microsoft mysql number open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix user variable voidmain() wab win32api windows.h windowsapi






