Thread: Jump to a position in a file

  1. #1
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476

    Jump to a position in a file

    Hi, I wanted to make a small game (For learning the techniques) and I wanted to create a main info file, containing the tilemaps, image names etc. but how can I jump to a marked place in the file and read it until the next mark is reached? I know it can be solved in many ways, but which one would you suggest me?

  2. #2

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    That's a good idea (very usual) but how can I search for a specific string in a file for using it? Or make an index, such as LEVEL1 -> 8 (in lines)?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Neither of these works on a line basis, only on a byte basis. If you want to store a byte index, I suppose you could. If you know which line you're looking for, you have to read a line, throw it away, read a line, throw it away, ..., read a line, throw it away, and then read the line you want.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Well, ok, i'll try. But how can I scan for a line containing LEVEL x.y? I mean, scanf would try to get the values and save them in x and y, not compare them.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    scanf isn't supposed to compare them. You're supposed to compare them. Read something in; if it's what you want, then yay; otherwise, do it again.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    you mean like:

    Code:
    while(getline(line, SYMBOL_LENGTH, file) != NULL) {
            if (line == {'@', 'L', 'V', 'L'}) {
                    fscanf...
            }
    }
    Last edited by Brafil; 12-22-2008 at 10:56 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The idea looks reasonably correct, but the execution is all wrong. You can't compare strings with ==, and the rest of the line is in line too, not just the first bit, so you'll have to not read from the file, but read from line.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Sorry, i did it wrong the first time. Anyway now it crashes. How can I get the first lets say 5 chars from a string? with memcpy? And should I use strcmp instead of ==?

    PS: Why the hell does gdb report a sigsegv fault in ntdll!RtlInitializeHandleTable()
    Last edited by Brafil; 12-22-2008 at 11:10 AM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since == is amazingly wrong and strcmp is amazingly right, I would go with strcmp. If you only want to compare the beginning of your string, you could use strncmp instead.

  11. #11
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Sorry, now it goes. The problem was the file. Thanks for your help!
    Last edited by Brafil; 12-22-2008 at 01:33 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you ever get into the if statement? What does your line variable hold, other than what you expect?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably because fscanf() doesn't return 4 and doesn't pass over the stuff you wanted it to, so it gets stuck - which is a common scanf() scenario when the input and the expected input aren't a good match (e.g. text when numeric input is expected).

    By the way, if the file isn't enormous (as in, many megabytes) then it may actually work better to just read all of the file into memory at once into some suitable data structure, and then search inside the data structure as needed, rather than searching the file content.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM