struct / fread ?

This is a discussion on struct / fread ? within the C Programming forums, part of the General Programming Boards category; Hello Board, I am almost in tears as I am having a problem reading in data from a file into ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    struct / fread ?

    Hello Board,

    I am almost in tears as I am having a problem reading in data from a file into an array of structs.

    My stuct is like this:

    struct {
    int stocknum;
    float pricea;
    float price b;
    } items;

    Then my file data is arranged in this way (there is a newline char at the end of each line)

    322 10.0 11.3
    111 2.45 3.40
    433 3.22 4.55
    987 19.8 20.0

    Then I start to write my program as usual:

    main()

    int fd;
    items itemarray[1000];

    // I am woking with unix and use file descriptors. I can use pointers to for those that don't know unix descriptors. Ill take anything at this point

    fd = open(filename, O_RDWR, 0777);

    // then my problem begins. I have tried FREAD and FGETS, and still cant get my list into the array of structs. Any help appreciated with this.

    -optimus

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Use fopen() if you want to use fgets() to read the file. Once you've read a line, use sscanf() to extract information from that line
    Code:
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
        // sscanf buff
    }
    You don't have a binary file, so using fread() would be of limited usefulness in this example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Thanks!

    Would you mind going into slightly more detail? What if I wanted to use OPEN ?

    V/r

    optimus

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    it means you have to use read(), and construct \n terminated lines yourself
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    Smile

    tHANKS Again!

    Should I use a while loop to just read it all in line by line?

    Something like:

    while(!EOF)


    How does it get into the struct properly? By using a loop also?

    -optimus

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Should I use a while loop to just read it all in line by line?
    That is exactly what I showed you in the first reply
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    I got most of it to work, and input my data into my struct, however, for some reason I can not read past the first line. I am using READ, but can not see why it can not read past 10 bytes. I set my buffer to 500, and still it will not get past the first line. I am guessing it is because of the newline character. I read the man on READ and still can not get the answer I am looking for. Any ideas about this?


  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you trying to read text, or in binary? If you're just reading text, consider something like fgets instead, and then pull what you need from the buffer you read into.

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

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Man, this sucks. I guess there is no way to do it with READ? I will have to change all my file pointers I guess. Perhaps this is the best course of action.

    -optimus

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FILE pointers work well, because there are a multitude of functions to easily get what you need. Is there a reason you need to use open and pure descriptors rather than file streams?
    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    int main( void )
    {
            char buf[BUFSIZ] = {0};
            int fd = -1, bytes = 0, linelength = 11;
            fd = open( "opentry.txt", O_RDONLY );
            printf("fd is %d ", fd );
    
            bytes = read( fd, buf, linelength );
            printf("Read %d bytes, buf contains '%s'.\n", bytes, bytes > 0 ? buf : "" );
            close( fd );
            return 0;
    }
    
    Opentry.txt:
    1234567890
    1234445566
    3423423423
    Try that on for size. Then consider using things like sscanf to pull what you need from the buffer.

    Quzah.
    Last edited by quzah; 04-02-2004 at 08:37 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    Question

    tHANKS Quzah,

    That is pretty helpfull!

    If the items were like this though:


    111 222 333 444
    222 333 444 333

    and I needed to get each element stored into their proper variable how would that be done? I keep having a problem with being able to read only the first four items, then needing to get the next 4, etc..

    V/r

    optimus

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Easy way? Read four bytes into a buffer. Turn the fourth character into '\0'. Then use atoi or atof to change it into whatever it is supposed to be. Lather, rinse, repeat.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

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