Search through a file & return a pointer?

This is a discussion on Search through a file & return a pointer? within the C++ Programming forums, part of the General Programming Boards category; Hi, Are there any functions similar to strstr() that can search through a file and return a pointer to that ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    28

    Search through a file & return a pointer?

    Hi,

    Are there any functions similar to strstr() that can search through a file and return a pointer to that location, so i can then read in the data to a buffer?

    Example..

    Code:
    FILE* fFile;
    fFile = fopen("Test.dat");
    int iLocation = look_through_file(fFile, "XPR0");
    
    etc etc..

    If not how would one get the location of such a string in a file?

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To my knowledge you can't read contents of a file in it's original location. You have to load it to memory and read it from there. If you knew in advance the offset from the front of the file, then you could place the file pointer at that position and not have to read the contents of the file preceding the disired position. However, that is not the case for many programs.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: Search through a file & return a pointer?

    Originally posted by Marc
    Hi,

    Are there any functions similar to strstr() that can search through a file and return a pointer to that location, so i can then read in the data to a buffer?

    Example..

    Code:
    FILE* fFile;
    fFile = fopen("Test.dat");
    int iLocation = look_through_file(fFile, "XPR0");
    
    etc etc..

    If not how would one get the location of such a string in a file?

    Thanks
    The only way to search through a file is to read the file, so no. You'll have to read the file from the beginning and search thru the buffer for what you're looking for.

    But it IS possible to read the bytes directly at, say, 100 thru 125. You need lseek() or a related function.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Thanks for the replys guys.

    I never thought about doing it that way!

    Luckily all the files that the prog will use should be less than 10mb, so its fine to read it all into memory and sort from there.

    Thanks for the help.



    Out of curiosity if the files were say 500mb or more, what techniques could one use to sort through them?

    Thanks

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    If you can't get them in memory all at once, read them in say 100mb chuncks. Keep track of the chunk you've loaded to get the exact address.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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