Thread: seek data

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0

    seek data


    fseek (pFile, (fans.id - 1) * sizeof (struct fansInfo), SEEK_SET);

    the code above is to seek the fans file if the id is a INTEGER….am I correct?
    If I want to seek the fans file based on the ID which is NOT INTEGER but CHARACTER, what am I supposed to do?


    leader.id = idDetail;

    when I set leader id and idDetail to CHARACTER and NOT INTEGER, error will occur…
    how to convert the data in leader.id into idDetail when both of them are CHARACTER and NOT INTEGER?
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    >>fseek (pFile, (fans.id - 1) * sizeof (struct fansInfo), SEEK_SET);

    fseek() sets a file pointer to the required location.

    in the file opened as pFile

    written with a structure 'fansInfo'

    the pointer is set to id-1 records (ie structures into) the file. This would appear as if it is converting a one based index to a zero based index.

    How do you do it if the id field of the structure is a char?

    If you changed the structure elements type (ie int -> char), and so the struct size, the file will no longer be vaild and will not load.

    If it was a char then you would need some conversion. That ddepends on HOW the id index was defined.

    it could be

    if the ID is stored in the form fans.id="1";//the id is a number stored as a char
    this has a limited range of 0 - 9

    int iIndex=atoi(fans.id);//may have to convert to zero based


    if a ASCI code was used
    ie fans.id='N';//use the ASCI key code of 'N' = 78
    this only has a range of 0 - 255
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0

    if a ASCI code was used
    ie fans.id='N';//use the ASCI key code of 'N' = 78
    this only has a range of 0 - 255
    at first, i set the structure to be char ^^;
    Erm...if the ID is combination of character and number?
    not only one character...for example
    AAZZ001123 like this?
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    >.if the ID is combination of character and number?

    For our purposes that is not possible.

    We are talking about the data type and size.

    >>AAZZ001123

    This is a char array. Not an numerical data type.


    You can not use this as an array index. In your code the ID element of your structure MUST contain a one based index. If not you will not be able to read the correct record from the file.

    You could convert using atoi() and isdigit(). That is while the element of the char array is not a digit move thru the array, then try an atoi() (ASCII to int)
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    [QUOTE
    You could convert using atoi() and isdigit(). That is while the element of the char array is not a digit move thru the array, then try an atoi() (ASCII to int) [/QUOTE]

    you mean if my ID is AAZZ001123 then....
    for the AAZZ I have to convert to ASCII then the result and 001123 combine together?
    for example the ASCII number for AAZZ is equal to 500
    then after the conversion, the ID is 500001123 ??
    Only with this number I can do compare or matching...am i correct?
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    No

    What is the ID of the first record in the file?
    It must be one (1) after you have parsed the string (removed the unwanted bits).


    if your ID is "AABB000001".

    test each char in turn.
    if it is not a digit , move to the next char
    else use atoi() to convert from a char (ASCI character) to an int

    Code:
    int iVar=0;
    char sString[16]={"00001"};
    
    iVar = atoi(sString);
    //iVar will now equal 1
    use a pointer
    char *pString=NULL;
    char sString[16]={"AABB00001"};

    pString=sString;//set the pointer to the begining of the string
    then you can increment the string with

    pString++;

    and will always have a pointer to the correct (current) char in the string as you move thru it looking for the end of the letters.

    Try it.
    Post code if you have problems and I will correct.
    NOTE: the reading off file will NOT work unless the records ID is also the location in the file ie the first record in the file has ID 1, the 100th record in the file has ID 100.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    thanks a lot for the answer...

    oh yea..about pointer...
    the pointer you in your example is a pointer to string right?
    then...if i want to put a pointer to file...
    for example i have 2 files...
    A and B
    then , i want to open a particular file..

    FILE *ptrFile = NULL;

    if the file i want to read file A,
    the code is something like
    fopen ( (ptrFile) "A.txt", "r");
    right?

    then if the file i want to read is not decided at the first time,
    fopen ( (ptrFile) "filename", "r")
    how to write if i want to read any file i want?

    thanks :)
  14. #8
  15. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    >>the pointer you in your example is a pointer to string right?

    Yes, it was a char*. Just as int* is a pointer to an int. This tells the complier how much to 'move' if you pVar++ (increment) the pointer.

    >>fopen ( (ptrFile) "filename", "r")
    Code:
    char         sFileName[MAX_PATH],sError[255];
    FILE         *pFile=NULL;
    
    sprintf(sFileName,".\\Folder\\MyFile.txt");
    if( (pFile  = fopen( sFileName, "r" )) == NULL )
    {
          sprintf( sError,"The file %s was not opened.",sFileName );
          MessageBox(NULL,sError,"MyApps Error",MB_ICONERROR|MB_OK); // show error message on main screen
    }
    else//file opened
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    thanks a lot one more time :)

    when we insert into file, we can insert it like how we insert into database right?
    it will be displayed in columns and we can retrieve them just like we retrieve from database..
    what is the advantage then if we use database or if we use files?
    using database is slower?
    for files...can we take data from A file and some data from B file and some data from C file, and then combine together, and process them?
    thanks for your explanation
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    Clone,

    Based on what you're writing, I'm thinking you're more than a lot lost as to how to store this data and how fseek works. fseek only sets the file input stream to a certain offset in the file. The file doesn't have any kind of inherent key/value pairing.

    If you're looking to build that sort of pairing into a data file, you can either implement a b-tree, or more sensibly, use a library like the Berkeley DB library from Sleepycat Software. I advise following the second route, because they have a very nice tutorial online.

    Do you have an existing data file that you need to read from, or are you trying to figure out how to store some data? If you want, contact me offline and I will try to help you.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    hi again....i tried to run the below codes...
    when I compile, there is no error, no warning..
    but when i run...
    when i type in the file i want to search....for example I search "Hello"
    a message suddenly appear saying that
    "test.exe (my program name) has encountered a problem and needs to close. We are sorry for the inconvenience."
    then after that it will print out "the file Hello was not opened" and press any key to continue...
    Why does this happen when actually there is a Hello file in my folder?

    Thanks a lot..


    printf("Please enter the file:\n");
    scanf("%s", sFileName);
    if( (pFile = fopen( sFileName, "r+" )) == NULL )
    {
    printf( "The file %s was not opened.",sFileName );
    }
    else
    {
    printf("File is found\n");
    }
    fclose (pFile);
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Originally posted by ClayDowling
    Clone,

    Based on what you're writing, I'm thinking you're more than a lot lost as to how to store this data and how fseek works. fseek only sets the file input stream to a certain offset in the file. The file doesn't have any kind of inherent key/value pairing.
    yeah..acutally i am kinda lost ^^;

    Do you have an existing data file that you need to read from, or are you trying to figure out how to store some data? If you want, contact me offline and I will try to help you.
    erm...actually I dun have any file to read from..the file is the one i created myself...
    i just simply created it from notepad...
    i separate into three columns
    the first one is characters
    the second one is decimal numbers
    the third one is integers...
    then, try to read the file...but, cannot :(
    anyway, thanks a lot for your help :)
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    Thanks a lot everyone...
    I finally have succeeded in doing it...
    I can find the file already :)
    thanks a lot..

    Oh yea, now another problem...
    for example inside the file (created using notepad)
    there are three columns
    AAA 000 A0A0
    AAA 001 A0A0
    BBB 000 A0A2

    if i want to detect how many data the file has for a particular data,can I do that?
    for example...
    I can print out there are two AAA data in the file.Can I do that?

    thanks
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    You're going to need some variety of dynamic data structure to read in this data and process it. The type of structure that you need will depend on what you want to do with it. A sorted list might be your best bet given what you've said you want to do.

    That said, this is C. Anything is possible. Some things are just a little too tedious.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  28. #15
  29. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    14
    Rep Power
    0
    how about if inside the file
    i put the format as three columns
    but at the most top, there is characters..
    for example

    AAA
    ZZZ OO01 20
    ZZZ OO02 30


    for
    ZZZ OO01 20
    ZZZ OO02 30

    i am using struct...

    as for the top one AAA,
    if once I open the file and check the most top value...
    how to do this?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo