Thread: file I/O, writing to the file.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    19

    file I/O, writing to the file.

    I have to write user given data to a .txt file. I did with fprintf but only string did not write in the file? here is my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    int main()
    {
        struct student {                // student record field
            int st_id;
            char st_name[20];
            int st_age;
            char st_dep[6];
            float st_grade;
        }line;
        char name[20];
        printf("Please Enter 6 digit student ID\n");
        scanf("%d",&line.st_id);
        printf("please Enter student name\n");
        scanf("%s",name);
        printf("%s",name);
        printf("Please Enter student age\n");
        scanf("%d", &line.st_age);
        printf("Please Enter student Department(MATHS,CHEM,CSE,BME)\n");
        scanf("%s",&line.st_dep[6]);
        printf("Please Enter student grade(0-100)\n");
        scanf("%f",&line.st_grade);
        FILE * fp;                                                          // file open from here.
        if ((fp = fopen ("file.txt", "a")) != NULL){
            printf("%s",line.st_name);
            fprintf(fp, "%d, %s, %d, %s,%f \n", line.st_id, line.st_name, line.st_age,line.st_dep,line.st_grade);
            fclose(fp);
            printf("data recorded\n");
        }
        else printf("Writing Error.\n");
        return 0;
    }

    file.txt file is here
    111111, , 23232, ,33.000000 // here string is missing?
    Last edited by programinproces; 10-25-2013 at 12:08 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you never fill line.st_name - instead you fill local var name. Why?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    I try that but it was error then I went this way. Is it shouldn't be this way?

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    Thanks vart This time it works. I don't know where I was lost

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should also fix your line 25 - it access the array out of bounds

    It should be

    Code:
    scanf("%5s",line.st_dep);
    same goes for reading name - %s without width specifier in scanf is really dangerous and is one of the security wholes exploitable by hackers. Width in %s should be one less than array length
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    19
    Thanks and great appreciation.






    You can't live a perfect day without doing something for someone who will never be able to repay you. ~John Wooden

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  2. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  3. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 02:17 PM
  4. Writing to a file
    By k10spades in forum C Programming
    Replies: 3
    Last Post: 11-19-2003, 07:56 PM
  5. Writing to a file
    By unanimous in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 07:10 PM