Thread: Why does this not read in correctly?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Why does this not read in correctly?

    The data is written correctly to the external file. In the correct order and format. But when read back in, the format is changed, and the data is not displayed correctly. Is their something wrong with this code? It should be in this order-
    MemberNumber MemberName MemberAddress MemberVisits.


    while (!feof(memberRead))
    {
    fscanf (memberRead, "\n%s", MemberData[counter1].membernumber);
    fgets(&MemberData[counter1].membername, 30, memberRead);
    fgets(&MemberData[counter1].memberaddress, 100, memberRead);
    fscanf (memberRead, "%d", &MemberData[counter1].membervisits);
    counter1++;
    }
    m = counter1;
    m--;

    BlueBob

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you add your data file as an attachment?

    Then someone can show you how to read the file properly, without all this feof() nonsense.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    Here ya go, an example of a typical members file.
    Whats wrong with using feof? i was told on this board to use it!

    BlueBoB

    mem1
    Scott Harkins
    South Cobberville
    0
    mem2
    Brian Hope
    North Queensferry
    0
    mem3
    Allister McIntosh
    New Jersey
    0

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    tsk tsk...mixing functions...you should stick with either scanf or fgets, not both. Using both together is known to wreak havoc sometimes.

    But it also looks like you're scanf'ing a number as a string, if in fact MemberData[counter1].membernumber is a number...
    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;
    }

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Slap me if I'm wrong (quite likely), but is it not easier to read and write the entire MemberData structure to the file using fwrite() and fread() in binary mode?
    e.g.

    filePtr = fopen(f_name, "wb");
    fwrite(&MemberData, sizeof(MD_STRUCT), 1, filePtr);
    fclose(filePtr);


    dt

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by DominicTrix
    Slap me if I'm wrong (quite likely), but is it not easier to read and write the entire MemberData structure to the file using fwrite() and fread() in binary mode?
    e.g.

    filePtr = fopen(f_name, "wb");
    fwrite(&MemberData, sizeof(MD_STRUCT), 1, filePtr);
    fclose(filePtr);


    dt
    Of course it would. But that would mean that the homework people always post here would be something practical and useful, rather than some screwed up worthless example of C...

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

  7. #7
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    lol

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    If i use a memory dump i will lose marks! Thats why i didnt use it.

    I will try to use the one function to read from now on though.

    BlueBob

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Slap me if I'm wrong (quite likely), but is it not easier to read
    >and write the entire MemberData structure to the file using
    >fwrite() and fread() in binary mode?
    In many cases it's easier to write blocks of binary data to files instead of text, but there's a little problem of portability when it comes to binary files. Binary representation may be different on another system whereas text is converted to the proper character set and represented correctly on just about any system.

    >Whats wrong with using feof? i was told on this board to use it!
    You weren't told not to use it on this board, you were told not to use it as the condition for a loop like you were on this board. There's nothing wrong with feof, it's how people use it that causes problems. When feof is used in a loop like that, the loop performs one iteration past EOF because EOF is only indicated after the loop has tried to read from the stream.

    -Prelude
    My best code is written with the delete key.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try this
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    #define MAX_MEMBERS 10
    #define MEM_NAME_LEN    20
    #define MEM_ADDR_LEN    20
    
    typedef struct {
        int  membernumber;
        char membername[MEM_NAME_LEN];
        char memberaddress[MEM_ADDR_LEN];
        int  membervisits;
    } mem_st;
    mem_st MemberData[MAX_MEMBERS];
    
    int main() { 
        char    buff[BUFSIZ];
        int     i, counter1 = 0;
        FILE    *fp;
    
        /* open the file, check for success */
        fp = fopen( "sample.txt", "r" );
        if ( fp == NULL ) {
            perror( "Cant open" );
            exit( 1 );
        }
    
        /* read all members, or until the array is full */
        while ( counter1 < MAX_MEMBERS &&
                fgets( buff, BUFSIZ, fp ) != NULL ) {
            sscanf( buff, "Mem%d", &MemberData[counter1].membernumber );
    
            /* read name and address directly into the array */
            /* if you need to remove the newline, add the code */
            /* if the fgets fails, we've reached the end of file unexpectedly */
            if ( fgets( MemberData[counter1].membername, MEM_NAME_LEN, fp ) == NULL ) break;
            if ( fgets( MemberData[counter1].memberaddress, MEM_ADDR_LEN, fp ) == NULL ) break;
    
            /* read and scan the number of visits */
            if ( fgets( buff, BUFSIZ, fp ) == NULL ) break;
            sscanf( buff, "%d", &MemberData[counter1].membervisits );
    
            /* successfully read a complete record */
            counter1++;
        }
        fclose( fp );
    
        for ( i = 0 ; i < counter1 ; i++ ) {
            printf( "%d %s %s %d\n",
                MemberData[i].membernumber,
                MemberData[i].membername,
                MemberData[i].memberaddress,
                MemberData[i].membervisits );
        }
    
        return 0; 
    }
    > Whats wrong with using feof? i was told on this board to use it!
    Nothings wrong, it just doesn't tell you what you want to know, so its the wrong function to use in the context in which you were using it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to i correct this code to read the file correctly?
    By funit49 in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2008, 07:56 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I am lost on how to read from file and output to file?
    By vicvic2477 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 11:52 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM