Thread: Reading from file into memory

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Reading from file into memory

    I have a problem reading from a file into a memory. In the file I have in the same line "float variable, char variable". Now, I want this to be read into a memory.

    How to do it??
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fscanf
    fread

    Both are useful. If you're using a text file, use fscanf. If you're using binary, use fread.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Exclamation

    ok, but I want this with pointers.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And? Did you look at either function? They both take pointers as arguments. Post your attempt, we'll help fix your problems.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    struct Top10
    {
    char Name[80]
    struct Top10 *next;
    }

    void READ(struct Top10 **first, char file[])
    {
    FILE *fin;
    float temp[100];
    struct Top10 *new;

    *first = NULL;
    fin = fopen(file, "rt+");
    while (fgets(temp, 100, fin) != NULL)
    {
    new = (struct Top10*) malloc(sizeof(struct Top10));
    sscanf(temp, "%s", new->Name);
    new->next = *zac;
    *first = new;
    }
    fclose(fin);
    }

    This function reads name from the "fin". I want this to change that this function will read a float and char variable. How to do it?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Sayeh
    Guest
    Well, how long is a float in bytes? How long is a char, in byes? Read that number of bytes in... sheesh.

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Float is 4 bytes and char is 1 byte long, but I still don't know how to do it. Please give me an example.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You will have to use either fgets or fscanf not to mention fread.

    fgets will only work with text-files.

    Not sure about fscanf. Maybe only text files, check it out.
    fprintf to write a formated/non-formated character input.
    ssprintf lets you print data directly on a string of chars.
    fread it if you wrote it to a non-text file, and fwrite it to the non-text file. fscanf allows you to copy file data directly into your structures, as does fread for non-text files.

  9. #9
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Can anyone write me an example?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Read 'Smurfs' post.
    Last edited by Sebastiani; 12-13-2001 at 06:46 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Sayeh
    Guest
    Okay. Stop wasting time with little iddy-biddy routines. Use your drive like a block device like it was meant to be. This isn't rocket science--

    Code:
    // whether you read or write, you need the following things:
    //
    // - ram buffer to read/write from/to
    // - size of smallest unit you are reading/writing
    // - number of units you are reading or writing
    // - file you are reading/writing from/to
    //
    
    #define  MAXRECORDS     50
    
    typedef struct                         // format of smallest unit of data in db
       {
       int      data1;
       long   data3;
       char   letter1;
       }obj1;
    
    char myArrayMAXRECORDS];                  // array of arbitrary size
    obj1 myDataBase[MAXRECORDS];           // array of "records" for db
    size_t  count;                         // bytes read or written */
    
    FILE  *fp;                                // my file pointer */
    
    void foo(void)
       {
       obj1  myRecord;                 //  record-sized "buffer"
       
       // assume file has already been opened and 'fp' is valid
       // further assume that the position has already been set
       // with fpos()
    
       count = sizeof(obj1);
       count = fwrite(&myRecord,count,1,fp);
       if(count != sizeof(obj1))
          error(WRITE_FAILED);     // bytes written didn't match, bail
       else
          {
          // reposition fp to start using fpos()
    
          count = (sizeof(obj1) * MAXRECORDS);
          count = fread(&myDataBase[0],count,1,fp);
    
          //  or "count = fread(&myDataBase[0],sizeof(obj1),count,fp);"
    
          if(count != (sizeof(obj1) * MAXRECORDS))
             error(READ_FAILED);    // whole db couldn't be read in
          };
    
       // reposition fp at start using fpos()
    
       count = MAXRECORDS;
       fread(&myArray[0],1,count,fp);   // show reading an array
       }
    The above isn't complete, and isn't expected to run. It is just enough to show you how you can use the actual calls.

    Everything is bytes. Just bytes. Doesn't matter if it's an array, or a struct, or a union, or a piece of object code-- it is just "x number of bytes" to the computer and to RAM. In other words just a "block" of RAM or "block" of disk space.

    Also remember, the format you view everything through, whether you look at memory as an array, or a string, or a malloc()'d pointer, or whatever-- is all the same. Just consecutive bytes. It is _you_ who is change how you look at the same information based on what language constructs you choose to use.

    Truthfully, all file read functions eventually call fread(). whether you are reading 1 byte, or a million. Same thing with fwrite(), whether writing 1 byte or a million.

    enjoy, and I hope that helps.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just to nit pick...

    count = sizeof(obj1);
    count = fwrite(&myRecord,count,1,fp);

    There is no need to set 'count' the first time.

    count = fwrite(&myRecord,sizeof(obj1),1,fp);



    Additionally:
    fwrite
    fread and fwrite return the number of items successfully
    read or written (i.e., not the number of characters). If
    an error occurs, or the end-of-file is reached, the return
    value is a short item count (or zero).
    And as such:

    if(count != sizeof(obj1))

    This is incorrect I think. It would instead be '1', not 'sizeof(obj1)'. Right?

    Still, all in all, a nice way to reuse 'count' throughout.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    >Truthfully, all file read functions eventually call fread().

    Okay, then if I wanted to could I use fread to get all of my data? I thought fread was only for binary files, which is why I've never used it before. I'm just a bit confused as to when fread and fwrite should be used as opposed to fgets and fgetc.
    pointer = NULL

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fgetc is a very useful function if you need to read a single character at a time. If you need a single character from a file, there is no reason to use fread.

    fread is very useful for reading blocks of data at a time.

    fgets is also a very nice function. It shines when reading text files. It will read upto N-1 characters, or until it encounters a newline character, whatever comes first.

    In a nutshell, they're all useful.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Please download this file and analize the code. PLEASE.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM