FILES in WinAPI

This is a discussion on FILES in WinAPI within the Windows Programming forums, part of the Platform Specific Boards category; Okay, here is what I have to do. Work with files. But the thing is, should I use API functions ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    FILES in WinAPI

    Okay, here is what I have to do. Work with files. But the thing is, should I use API functions or just standard c functions for this?

    And also, a question about it (which ever way I decide to go): I need to either read a certain file (for example: file.txt) or if it is not existing, I need to create the file (file.txt). How would I approach this? What functions?

    Thanks!
    1978 Silver Anniversary Corvette

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The windows API file stuff don't *seem* to be as easy to use in the beginning. I'm still learning them all but they seem to be an overall better way of handling files the standard C

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    API functions are far superior. That is if speed is important to you.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    And also, a question about it (which ever way I decide to go): I need to either read a certain file (for example: file.txt) or if it is not existing, I need to create the file (file.txt). How would I approach this? What functions?
    CreateFile()
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by FillYourBrain
    API functions are far superior. That is if speed is important to you.
    What do you base this statement on? Have you compared the speed differences between the two? I am curious about this, because I would believe that they would both be the same, as they both make use of the same low level interrupts.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yes I have run speed trials on the C runtime vs system API. Window API on first access of the file takes about half the time to read. On following reads of the same file however, Windows API takes about 1/10th of the time to read. This is due to caching. Sorry to the "standards" purists. My heart goes out to you.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by FillYourBrain
    CreateFile()
    But how would I first check if the file already exists?
    1978 Silver Anniversary Corvette

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    You need to read the MSDN on that function.

    CreateFile(), despite it's misleading name, can open old files, check for existance, check for write access, create files, and even play with devices.

  9. #9
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by FillYourBrain
    ...Window API on first access of the file takes about half the time to read. On following reads of the same file however, Windows API takes about 1/10th of the time to read. This is due to caching...
    I'm sorry, but this boggles my mind... What form of caching does the Win32 API do that regular C functions do not do? Also, as evident by your wording, you are not speaking about reading in a single file in its entirety at once. Have you tested this? What are the conditions of your test when reading in the file segments at a time?

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I don't know about FillYourBrain, but in the compilers I've tested (GNU, Microsoft VC++, Borland C++) the C functions have simply been wrappers for the Windows API functions, and so using the API functions is marginally quicker than use the standard C library.

    By using sensible buffers (64Kb reads for example), you can hardly tell the difference between them. It's only when you are reading such small quantities like a single byte at a time does the extra overhead show through.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    There is also another advantage if you use advanced WinAPI functions. For example, you can use CreateFile(), CreateFileMapping(), and CreateViewOfFile() to memory-map a file. This allows the OS a lot of flexibility on when it actually performs disk access.

    The latency of a disk is high, because it takes time to physically position the head and wait for the platter to spin into the right position. However, once the head is positioned, you can read a lot of sequential data very fast, because as the platter spins, the head simply moves over the data sequentially. For example, if you were to read 64K worth of data, one byte at a time, it would be vastly slower than reading the whole 64K chunk at once.

    Reading byte-by-byte would look like:
    -Position- -Wait- -Read 1 byte- -Position- -Wait- -Read 1 byte- -Position- -Wait- -Read 1 byte-, etc. A lot of wasted time.

    Reading a block of data would look like:
    -Position- -Wait- -Read 64 kbytes-

    In general, by using the API functions, and by doing so in an intelligent way, you can read/write data in a much faster and more intelligent fashion. You can let the OS decide when to commit data to the disk, or when data needs to be read from the disk, instead of forcing reads or writes on every data access/change.

    (As an aside, memory-mapped files are a great way to share data between processes).
    Last edited by Cat; 09-22-2003 at 11:10 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't know about FillYourBrain, but in the compilers I've tested (GNU, Microsoft VC++, Borland C++) the C functions have simply been wrappers for the Windows API functions, and so using the API functions is marginally quicker than use the standard C library.
    Is the C runtime not its own DLL? Not sure about that. Regardless. My time trials were very extensive.

    large and small files, repetative opening and closing, repetative seeking, repetative small reads, repetative large reads. In all cases, the Windows function won out by substantial margins.

    edit: The C runtime is in fact its own dll. crtdll.dll to be precise. Therefore unless you disassembled it and found it to be calling the API function then I question the above statement.
    Last edited by FillYourBrain; 09-23-2003 at 08:04 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    There's no need to disassemble anything, the full source code to the crtdll.dll is installed on my harddisk and in this code I can clearly see it call the Windows API CreateFile, WriteFile, etc functions.

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    regardless, do the time test yourself. You'll see what I'm talking about. Major difference.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I have, and I've not just used Microsoft VC++ either, I've also tested GNU C++ & Borland C++ (which don't use crtdll.dll). I only see a small improvement in speed between the C library and the Windows API. I wouldn't say that this small improvement in speed was worth risking the portability of the code.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 08:53 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21