problem with my data

This is a discussion on problem with my data within the C Programming forums, part of the General Programming Boards category; hi i know how to print text in a file like xyz.txt but there is one thing i am not ...

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    problem with my data

    hi
    i know how to print text in a file like xyz.txt
    but there is one thing i am not sure about:

    when i am going to print data like this in a file:

    STR = 10
    INT = 5
    DEX = 12
    ...

    how can i access this data again? i can read
    the file, sure, but how can i "jump around"
    in this file to find the data i am looking for ?
    Last edited by dune911; 07-07-2002 at 07:38 AM.

  2. #2
    Unregistered
    Guest
    use fsetpos from stdio.h
    thats all i know

    bennyandthejets

  3. #3
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    thanks. i just found this in stdio.h

    Code:
    int	fgetpos	(FILE* fileGetPosition, fpos_t* pfpos);
    int	fsetpos (FILE* fileSetPosition, fpos_t* pfpos);
    but how can i use them?

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, if the data being written to the file is formatted,
    ie. using fprintf(),
    eg. A name, a surname and an age in each line,

    Then i would suggest that you read in a whole line using
    fscanf().

  5. #5
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    no, i want to use it like this :

    for my game, i need random skills for the player.
    i will put them into an extra file (like xyz.txt) to
    be able to access this data when the program
    runs another time. (like a savegame, you know?)

    i have to access this data to use it for math functions
    like "data in file (adress 1)" + "data in file (adress2)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If the file contains struct's (read and written in binary mode), you can use the fsetpos() function to determine which struct you want to get. You know how many structs there are in the file in total, because you know the total file size, and the size of a single struct. Simple division tells you how many structs are in the file.

    Alternativley, load the complete file into memory (into an array, or list of some kind), then access that instead of the file on disc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Or, is if it's a plain text file somthing like the .INI, then you probably have entries into this file like:
    VAR1=val1
    VAR2=val2
    You can use fgets() to read each line and compare the first few characters with the 'VAR' you are looking for. If it is found, the value can be easily extracted which is on the right of this string.
    Code:
    fgets(line, sizeof(line), fp);
    if(strncmp(line, "PLAYER", 6) == 0)
    ...

  8. #8
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    @shaik786
    well, i think that's exactly what i am looking for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM

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