There is something wromg with my code, I can't find the error.

This is a discussion on There is something wromg with my code, I can't find the error. within the C Programming forums, part of the General Programming Boards category; Hello, There is something wrong with my code, I can't seem to find my error. The program works just fine, ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    There is something wromg with my code, I can't find the error.

    Hello,

    There is something wrong with my code, I can't seem to find my error. The program works just fine, there is something wrong in the function 'read_from_file'

    Code:
    #include<stdio.h>
    
    struct student
    {
        int roll_no;
        char name[ 50 ];
        float CGPA;
    };
    
    void write_to_file( struct student [] );
    void read_from_file( struct student [] );
    
    int main()
    {
        struct student s1[ 5 ];
    
        write_to_file( s1 );
        printf( "\n\n" );
        read_from_file( s1 );
    }
    
    void write_to_file( struct student s1[] )
    {
        int i = 0;
    
        FILE *Ptr;
        Ptr = fopen( "Students.txt", "w" );
    
        fprintf( Ptr, "%-14s%-11s%5s\n", "Roll No.:", "Name:", "CGPA:" );
    
        printf( "Student %0d: ", ( i + 1 ) );
        scanf( "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
    
        while( !feof( stdin ) )
        {
            fprintf( Ptr, "%-14d%-10s%4.1f\n", s1[ i ].roll_no, s1[ i ].name, s1[ i ].CGPA );
    
            i++;
    
            printf( "Student %0d: ", ( i + 1 ) );
            scanf( "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
        }
    
        fclose( Ptr );
    
        printf( "\n-> Writing on file \"Students.txt\"." );
    }
    
    void read_from_file( struct student s1[] )
    {
        int i = 0;
        FILE *Ptr;
    
        Ptr = fopen( "Students.txt", "r" );
    
        printf( "\n-> Reading from file \"Students.txt\" and displaying on screen:\n\n" );
    
        printf( "%-14s%-11s%5s\n", "Roll No.:", "Name:", "CGPA:" );
    
        fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
    
        while( !feof( Ptr ) )
        {
            printf( "%-14d%-10s%4.1f\n", s1[ i ].roll_no, s1[ i ].name, s1[ i ].CGPA );
    
            i++;
    
            fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
        }
    
        fclose( Ptr );
    }
    sorry for the long program, please help me

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    If the program works just fine then how do you know there is something wrong?

    If your compiler is giving you warning messages, post them here.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    it works fine but gives an infinite loop in function 'read_from_file'

    please help me find it

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    74
    what was your aim in this program could you explain a little?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
    If this fails, then you do not make progress through the file, and you never reach a point where feof() can return true.

    Read the link deadplanet posted.

    Then do something like this.
    Code:
    while ( fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA ) == 3 ) {
      // do stuff
    }
    if ( !feof(Ptr) ) {
      // oops, loop exited before we got to eof - that's bad?
    }
    Though IMO, it's better to fgets() into a buffer first, then sscanf into your data structure.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple error i can't find /code
    By Alecroy in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2010, 01:00 PM
  2. Can not find error in this code
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 11:04 AM
  3. Can't Find Error in Code
    By DarkDot in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2007, 09:56 PM
  4. Can't find error in code
    By blackgingr in forum C Programming
    Replies: 10
    Last Post: 11-14-2002, 08:00 PM
  5. HELP! can't find that one error in my code
    By andyt2000 in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2001, 09:25 PM

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