Windows API functions to read and write files in C

Reply

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Windows API functions to read and write files in C

 
0
  #1
Aug 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Windows API functions to read and write files in C

 
0
  #2
Aug 27th, 2005
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:
  1. #include <windows.h>
  2.  
  3. int main ( void )
  4. {
  5. HANDLE out = CreateFile ( "test.txt", FILE_WRITE_DATA, 0, NULL,
  6. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  7. char s[] = "This is a test";
  8. int i, j;
  9. int n;
  10.  
  11. for ( i = 0, j = sizeof s - 1; i <= j; i++, j-- ) {
  12. s[i] ^= s[j] ^ 194837U;
  13. s[j] ^= s[i] ^ 3876U;
  14. }
  15.  
  16. WriteFile ( out, s, sizeof s, &n, NULL );
  17. CloseHandle ( out );
  18.  
  19. return 0;
  20. }
However, you can directly write numbers and other non-string data without the encryption, and it will be unrecognizable when opened with Notepad:
  1. #include <windows.h>
  2.  
  3. int main ( void )
  4. {
  5. HANDLE out = CreateFile ( "test.txt", FILE_WRITE_DATA, 0, NULL,
  6. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  7. int i = 12345;
  8. int n;
  9.  
  10. WriteFile ( out, &i, sizeof i, &n, NULL );
  11. CloseHandle ( out );
  12.  
  13. return 0;
  14. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Windows API functions to read and write files in C

 
0
  #3
Aug 28th, 2005
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
  1. hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT
  2. GENERIC_WRITE | GENERIC_READ,
  3. FILE_SHARE_READ, // share for reading
  4. NULL, // no security
  5. OPEN_ALWAYS, // existing file only
  6. FILE_ATTRIBUTE_NORMAL, // normal file
  7. NULL); // no attr
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:

  1. while(ReadFile(hFile, buf, sizeof buf, &dw_bytes_read, NULL) && dw_bytes_read != 0)
  2. {
  3. WriteFile(hStdOut, buf, dw_bytes_read, &dw_bytes_written, NULL);
  4. }

I had one nasty problem, At first I tried this:
  1. while(ReadFile(hFile, buf, sizeof buf, &dw_bytes_read, NULL)){...
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Windows API functions to read and write files in C

 
0
  #4
Aug 28th, 2005
Couldn't you just do something like this? Remeber if this sucks I am not good at the API
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6. char buf[30] = {'\0'}; //Null terminate
  7. LPVOID lpMsgBuf;
  8. DWORD byteWritten = 0;
  9. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  10. BOOL ReadFileReturn;
  11.  
  12. HANDLE hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT
  13. GENERIC_WRITE | GENERIC_READ,
  14. FILE_SHARE_READ, // share for reading
  15. NULL, // no security
  16. OPEN_EXISTING, // existing file only
  17. FILE_ATTRIBUTE_NORMAL, // normal file
  18. NULL); // no attr
  19.  
  20. ReadFileReturn = ReadFile(hFile,buf,30,&byteWritten,NULL);
  21. if(ReadFileReturn)
  22. {
  23. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  24. NULL,
  25. GetLastError(),
  26. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  27. (LPTSTR) &lpMsgBuf,
  28. 0,
  29. NULL);
  30. WriteFile(hStdOut,buf,sizeof buf,NULL,NULL);
  31.  
  32. }
  33. else
  34. {
  35. WriteFile(hStdOut,"It Failed",sizeof "It Failed",NULL,NULL);
  36.  
  37. }
  38. std::cin.get();
  39. return 0;
  40. }
;
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Windows API functions to read and write files in C

 
0
  #5
Aug 28th, 2005
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 28
Reputation: proghelper is an unknown quantity at this point 
Solved Threads: 0
proghelper proghelper is offline Offline
Light Poster

Re: Windows API functions to read and write files in C

 
0
  #6
Aug 28th, 2005
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.
I am not sure if writing strigs in binary mode is a good idea.
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Windows API functions to read and write files in C

 
0
  #7
Aug 28th, 2005
>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:
  1. char *lines[N];
  2.  
  3. ...
  4.  
  5. WriteFile ( h, lines, N * sizeof *lines, &n, NULL );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Windows API functions to read and write files in C

 
0
  #8
Aug 28th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Windows API functions to read and write files in C

 
0
  #9
Aug 28th, 2005
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().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC