WriteFile()

This is a discussion on WriteFile() within the Windows Programming forums, part of the Platform Specific Boards category; I'm just starting to learn file I/O in Windows Programming, and I'm not sure how to use the WriteFile() function. ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    14

    WriteFile()

    I'm just starting to learn file I/O in Windows Programming, and I'm not sure how to use the WriteFile() function. For example, I have a 8x8 square matrix that I want to output to a file, but am unsure of how to do it. One of the main problems that I have run across is that I don't know how to output a 'return' or newline to the file.

    Any help would be greatly appreciated.

    Thanks!

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    How is the data organized in your program? Ie, is it in an 2 dimensional array? In that case you should be able to do something like this:

    Code:
    HANDLE hFile=CreateFile("data.dat",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    
    int data[8][8];
    
    //fill array
    
    WriteFile(hFile,(LPVOID)&data,sizeof(data),NULL,NULL);
    To read it in again, you pretty much just do the reverse using ReadFile.

    Note: I've never actually used these functions, I'm just reading a programming reference. It should work though.

    One of the main problems that I have run across is that I don't know how to output a 'return' or newline to the file.
    You shouldn't have to do this, unless you want to store different data objects in the one file. If that's the case, a newline or return character is probably not the best separator, because a string may contain that character, therefore disrupting the way the data is read back in.

    But if you really want to, then the characters are ASCII codes 13 (CR) and 10 (newline).
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    14
    Alright, I think I understand a little better - how do I output just an integer from the array. Do I always need to make it into a string before I can output it?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    how do I output just an integer from the array
    If you just want to do that, it may be easier to use fprintf.
    Code:
    FILE *f;
    f=fopen("data.dat","w");
    fprintf(f,"%d",data[0][0]);
    That will simply output the first element of the array to the file.

    Do I always need to make it into a string before I can output it?
    Nope, you can do it how you want.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    bennyandthejets, there are a great many people who would debate the virtues of portable code vs OS dependant code. I for one, choose CreateFile,WriteFile,ReadFile, etc. Windows API for file access. The reason? Speed. Windows caches files and reads them much faster than the C runtime. But never mind the caching, even on the very first read of the file, the WinAPI blows away the "portable" code in time trials.

    Just my two cents.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Thanks for the tip. I have no idea about file writing, I've never really needing to use it. So you think the API functions are better?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "better" is an opinion thing. Faster, certainly. But I have to be careful not to say better because people around here are very much obsessed with portability.

    you can always do inline wrapper classes/functions or macros for OS dependant stuff though. Then when you want to port, all you have to do is fill in the proper functions in your wrappers. Portability can be pretty easily achieved in this way. Obviously I'm not a C/C++ purist. I would rather see the job get done in the most appropriate way for the platform.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 04:33 AM
  2. WriteFile issue
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-26-2007, 08:46 AM
  3. WriteFile question???
    By NewGuy100 in forum C Programming
    Replies: 27
    Last Post: 08-16-2005, 07:29 AM
  4. Whats wrong with my Writefile?
    By rEtard in forum Windows Programming
    Replies: 11
    Last Post: 06-10-2005, 02:46 PM

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