System calls (open, close, write)

This is a discussion on System calls (open, close, write) within the C Programming forums, part of the General Programming Boards category; The code below takes in program scores for an array of structures containing student information. It will correctly take in ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    System calls (open, close, write)

    The code below takes in program scores for an array of structures containing student information. It will correctly take in program scores for the first student in the array and write them to the file. For any other students, it will ask for the grades and accept them with no errors, but will not write them to the file.

    Any ideas why?

    Thanks

    Code:
    void addProg()
    {
            int fd,s;
            struct rec *stdnt;
    
            fd = open("stdnt.dat", O_RDONLY, 0666);
            s = lseek(fd,0,L_XTND);
            int n = s/sizeof(struct rec);
            lseek(fd,0,L_SET);
            stdnt = malloc(s);
            read(fd,stdnt,s);
    
            int progNum;
            printf("Enter Program Number: ");
            scanf("%d", &progNum);
            int i;
            int tempProgScore;
            close(fd);
    
            fd = open("stdnt.dat", O_WRONLY, 0777);
    
            printf("%d",n);
            for(i=0;i<n;i++)
            {
                    printf("Enter %s' score for program #%d:",stdnt[i].name, progNum);
                    scanf("%d",&tempProgScore);
                    stdnt[i].prog[progNum-1]=tempProgScore;
            }
            write(fd,stdnt,sizeof(struct rec));
            close(fd);
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    write(fd,stdnt,sizeof(struct rec));
    You are only writing one entry. Don't you want something more like:
    Code:
    write(fd,stdnt,sizeof(struct rec) * n);
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by bithub View Post
    Code:
    write(fd,stdnt,sizeof(struct rec));
    You are only writing one entry. Don't you want something more like:
    Code:
    write(fd,stdnt,sizeof(struct rec) * n);
    I know the standard I/O alternative is

    Code:
    fwrite(stdnt, sizeof(struct rec), n, input);
    which loops "n" times.

    To try and reduplicate that, I tried putting my write statement in the above for loop but it didn't work.

    I see what you mean though

    Edit: "it didn't work" is the wrong way to put it. It worked, but it duplicated my first student to the second student

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    which loops "n" times.
    No, it doesn't "loop" at all.

    To try and reduplicate that, I tried putting my write statement in the above for loop but it didn't work.
    I bet that you did it wrong. If you simply did it the way I posted, that would be correct.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by bithub View Post
    No, it doesn't "loop" at all.

    I bet that you did it wrong. If you simply did it the way I posted, that would be correct.
    perhaps you could clear it up for me? i thought it simply wrote the stucture "n" number of times.

    I edited my program to what you suggested and it did work, thank you!

    Could you tell me why adding it into my for loop did not work?

    Code:
    for(i=0;i<n;i++)
            {
                    printf("Enter %s' score for program #%d:",stdnt[i].name, progNum);
                    scanf("%d",&tempProgScore);
                    stdnt[i].prog[progNum-1]=tempProgScore;
                    write(fd,stdnt,sizeof(struct rec));
            }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It doesn't work the way you have it because you are just writing the same thing "n" times. To do it your way, modify the for loop to:

    Code:
    for(i=0;i<n;i++)
            {
                    printf("Enter %s' score for program #%d:",stdnt[i].name, progNum);
                    scanf("%d",&tempProgScore);
                    stdnt[i].prog[progNum-1]=tempProgScore;
                    write(fd,&stdnt[n],sizeof(struct rec));
            }
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by bithub View Post
    It doesn't work the way you have it because you are just writing the same thing "n" times. To do it your way, modify the for loop to:

    Code:
    for(i=0;i<n;i++)
            {
                    printf("Enter %s' score for program #%d:",stdnt[i].name, progNum);
                    scanf("%d",&tempProgScore);
                    stdnt[i].prog[progNum-1]=tempProgScore;
                    write(fd,&stdnt[n],sizeof(struct rec));
            }
    Well that provided some interesting results. A bunch of gibberish

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by ollie88r View Post
    Well that provided some interesting results. A bunch of gibberish
    Of course it does, who said the way structures are arranged in memory isn't going to result in gibberish?

    Ideally you should be serialising the output, you can't expect to write a structure to disk and then read it and expect it to be in the same format in memory as it was before. Thanks to padding in the structure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: Unix System Calls ???
    By Paul22000 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 05:28 PM
  2. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 02:16 PM
  3. Open, search, act, close.
    By RoD in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2002, 04:48 PM
  4. System Calls
    By Jperensky in forum C Programming
    Replies: 6
    Last Post: 03-12-2002, 02:41 PM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM

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