Using API function ReadFile()

This is a discussion on Using API function ReadFile() within the Windows Programming forums, part of the Platform Specific Boards category; Using ReadFile() Hello, I would like to use a configuration file with my application, and use the ReadFile function (unless ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    Using API function ReadFile()

    Using ReadFile()

    Hello,

    I would like to use a configuration file with my application, and use the ReadFile function (unless there is an even better way) to retrieve data from it. How would I go about retrieving data from a specific area in a text file?

    For example:
    In a file with several values I want to retrieve "Address," which, in this case, is "Whatever?"

    Name: Whatever
    Age: 11
    Address: Whatever
    Telephone Number: 555-5555

    Can the SetFilePointer() function be used, in conjunction with ReadFile, of course, for this purpose? If so, how exactly?

    Thanks in advance

    Echidna

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As long as you know the offset within your file, you can do -

    SetFilePointer(hFileHandle,iOffset,0,FILE_BEGIN);
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Thanks for responding, Zen

    >>>As long as you know the offset within your file, you can do -
    SetFilePointer(hFileHandle,iOffset,0,FILE_BEGIN);> >>

    That is the problem. How do you keep track of the offset (I assume by offset you mean the number of bytes to move the file pointer) in a file that is constantly being appended?????

    Is there a way to set the pointer by line instead of by bytes?

    Thanks in advance

    Note: I have used GetPrivateProfileString(), and the related functions, in the past for configuration files, but I will eventually need to learn how to use the ReadFile function since it is much more versatile.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Use GetFileSize() on the HANDLE returned from CreateFile().
    GlobaAlloc() for the file size and read the lot with ReadFile().

    Use a pointer to the mem block and string matching to find what you are looking for.

    Use titles like

    [NAME]
    Your Name

    [AGE]
    12

    ect
    as these help to divide the file.

    >>Is there a way to set the pointer by line instead of by bytes?

    Not unless you make the lines all the same length and know which one you need or know each lines length.

    With sprintf() you can set the string length with
    Code:
    int   iLineLength=MAX_LINE_SIZE;
    
    sprintf(sBuffer,"%*s",iLineLength,sText);
    Last edited by novacain; 10-23-2001 at 01:46 AM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    39
    Ahhhhh.......I see, good idea. Thanks for the suggestions, "novacain." Still, it appears as if GetPrivateProfileString() would be more efficient, but I will need to tackle using the ReadFile function some day -- today seems like a good day to start :-)

    Thanks guys

    Echidna

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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