What is wrong in this program ?

Discussion in 'C Programming' started by pereges, Apr 11, 2008.

  1. pereges

    pereges Guest

    #include <stdio.h>
    #include <stdlib.h>

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    FILE *fp;
    int i, n;
    typedef struct student_record
    {
    char name[40];
    int sid;
    }student;

    student s;
    /* I presume this creates a new file if one already doesn't exist */
    fp = fopen("student.dat", "ab+");
    if(fp == NULL)
    {
    perror("file can't be opened\n!");
    exit(EXIT_FAILURE);
    }
    else
    {

    printf("How many records you want to write ?\n");
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
    printf("Enter record %d: student name student id\n", i);

    /* There seems to be some problem here and
    its obvious during run time */
    scanf("%s %d", s.name, &s.sid);
    fwrite(&s, sizeof(s),1, fp);
    }

    }

    /* doesn't print at all ? */
    while(fread(&s, sizeof(s), 1, fp) == 1)
    printf(" %s %d \n", s.name, &s.sid);

    return 0;

    }

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    The o/p I get :


    How many records you want to write ?
    3
    Enter record 0: student name student id
    Nathan Reed 5
    Enter record 1: student name student id
    Enter record 2: student name student id
     
    pereges, Apr 11, 2008
    #1
    1. Advertising

  2. pereges

    Martin Guest

    On Fri, 11 Apr 2008 07:43:40 +0100, pereges <> wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #include <stdio.h>
    > #include <stdlib.h>


    No need to include these twice.


    > /* There seems to be some problem here and
    > its obvious during run time */
    > scanf("%s %d", s.name, &s.sid);
    > fwrite(&s, sizeof(s),1, fp);
    > }


    %s scans for non-white space characters in the input. "Nathan Reed"
    contains whitespace. You're better off reading in a line at a time; then
    parse the read-in line for the data you want. scanf() is better suited to
    well-formed data, which user input isn't. Perhaps try fgets() (eschew
    gets() which is dangerous).


    > }
    >
    > /* doesn't print at all ? */
    > while(fread(&s, sizeof(s), 1, fp) == 1)
    > printf(" %s %d \n", s.name, &s.sid);


    The third argument to printf should be s.sid, not &s.sid. I get a few
    screenfuls of zeros (after I fix the third argument).

    --
    Martin
     
    Martin, Apr 11, 2008
    #2
    1. Advertising

  3. pereges

    pereges Guest

    On Apr 11, 12:58 pm, Martin <> wrote:


    > The third argument to printf should be s.sid, not &s.sid. I get a few
    > screenfuls of zeros (after I fix the third argument).


    I tried to create the file and wrote some records on it. Then I
    commented the bit where data is written and the program can display
    the records without any mistakes. guess you just can't do a read just
    after write.
     
    pereges, Apr 11, 2008
    #3
  4. pereges schrieb:
    > On Apr 11, 12:58 pm, Martin <> wrote:
    >
    >
    >> The third argument to printf should be s.sid, not &s.sid. I get a few
    >> screenfuls of zeros (after I fix the third argument).

    >
    > I tried to create the file and wrote some records on it. Then I
    > commented the bit where data is written and the program can display
    > the records without any mistakes. guess you just can't do a read just
    > after write.


    You can. Look up the fseek function in your favourite C standard
    library reference.

    --
    Irrwahn Grausewitz []
     
    Irrwahn Grausewitz, Apr 11, 2008
    #4
  5. pereges wrote:
    >
    > On Apr 11, 12:58 pm, Martin <> wrote:
    >
    > > The third argument to printf should be s.sid, not &s.sid. I get a few
    > > screenfuls of zeros (after I fix the third argument).

    >
    > I tried to create the file and wrote some records on it. Then I
    > commented the bit where data is written and the program can display
    > the records without any mistakes. guess you just can't do a read just
    > after write.


    Of course you can, as long as the file is open in read/write mode
    (which "ab+" does). You just have to remember that, after any
    read or write, the current position in the file in the character
    immediately after the last one read/written. In your case, you
    just wrote off the end-of-file, meaning there is nothing after
    it to be read.

    Look up the fseek() function, which lets you change the current
    position.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Apr 11, 2008
    #5
  6. pereges

    Guest

    Kenneth Brody <> wrote:
    > pereges wrote:
    >>
    >> I tried to create the file and wrote some records on it. Then I
    >> commented the bit where data is written and the program can display
    >> the records without any mistakes. guess you just can't do a read just
    >> after write.

    >
    > Of course you can, as long as the file is open in read/write mode
    > (which "ab+" does).


    And you call fflush() or a file positioning function like fseek() when
    switching from writing to reading or vice versa (7.19.5.4p6).

    -Larry Jones

    The real fun of living wisely is that you get to be smug about it. -- Hobbes
     
    , Apr 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    what's wrong in this program

    VisionSet, Sep 24, 2004, in forum: Java
    Replies:
    12
    Views:
    555
    Thomas G. Marshall
    Sep 28, 2004
  2. Replies:
    7
    Views:
    419
  3. Matthew
    Replies:
    7
    Views:
    675
    Priscilla Walmsley
    Jan 7, 2005
  4. jose luis fernandez diaz

    why this program is wrong on Tru64 OS ?

    jose luis fernandez diaz, Nov 25, 2003, in forum: C++
    Replies:
    2
    Views:
    427
    Stephan Br?nnimann
    Nov 26, 2003
  5. Replies:
    5
    Views:
    1,594
Loading...

Share This Page