CrazyEngineers
  • Please help me find error in the program

    samaira

    Member

    Updated: Oct 22, 2024
    Views: 901
    #include<stdio.h>
    #include<conio.h>
    struct student
    {
    int roll;char name;
    }s1;
    FILE *fp;
    void main()
    {
    create_file();
    read_file();
    getch();
    }
    void create_file()
    {
    char reply;
    fp=fopen("student.txt","w");
    if(fp==NULL)
    {
    printf("\n the file cannot be opened");
    exit(1);
    }
    do
    {
    printf("\n enter the student's roll no and name: ");
    scanf("%d%s",&s1.roll,s1.name);
    fprintf(fp,"\n the roll no is:%d \t the name is %s",s1.roll,s1.name);
    printf("\n do u wish to add new records(Y\N): ");
    reply=getch();
    }while(reply=='Y');
    fclose(fp);
    }
    void read_file()
    {
    fp=fopen("student.txt","w");
    if(fp==NULL)
    {
    printf("\n the file cannot be opened");
    exit(1);
    }
    while(!feof(fp))
    {
    fscanf(fp,"%d%s",&s1.roll,s1.name);
    printf("\n roll no=%d\tname=%s",s1.roll,s1.name);
    }
    fclose(fp);
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • pratik007

    MemberAug 1, 2009

    samaira
    #include<stdio.h>
    #include<conio.h>
    struct student
    {
    int roll;char name;
    }s1;
    FILE *fp;
    void main()
    {
    create_file();
    read_file();
    getch();
    }
    void create_file()
    {
    char reply;
    fp=fopen("student.txt","w");
    if(fp==NULL)
    {
    printf("\n the file cannot be opened");
    exit(1);
    }
    do
    {
    printf("\n enter the student's roll no and name: ");
    scanf("%d%s",&s1.roll,s1.name);
    fprintf(fp,"\n the roll no is:%d \t the name is %s",s1.roll,s1.name);
    printf("\n do u wish to add new records(Y\N): ");
    reply=getch();
    }while(reply=='Y');
    fclose(fp);
    }
    void read_file()
    {
    fp=fopen("student.txt","w");
    if(fp==NULL)
    {
    printf("\n the file cannot be opened");
    exit(1);
    }
    while(!feof(fp))
    {
    fscanf(fp,"%d%s",&s1.roll,s1.name);
    printf("\n roll no=%d\tname=%s",s1.roll,s1.name);
    }
    fclose(fp);
    }


    The error is of type mismatch in the two functions u have used i.e.
    1) create_file()
    2) read_file()
    The error when u call it outside main()
    I would suggest to declare both the functions separately above the main() function where u have declared the pointer
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberAug 1, 2009

    Reason why the code was not working is:
    1. While taking user input for adding more record "(Y\N)" is used, instead '\\' should be used as "(Y\\N)" otherwise it gives error in compilation.

    2. You have declared 'name' in structure student as 'char', whereas you are trying to read string using that, 'name' should be declared as char array.

    3. While writing to file you used statement
    fprintf(fp,"\n the roll no is:%d \t the name is %s",s1.roll,s1.name);
    which writes some additional data apart from roll number and name, whereas while reading data you use statement
    fscanf(fp,"%d%s",&s1.roll,s1.name);
    which try to read only roll number and name. You should read what you write to file, e.g.,
    fprintf(fp,"%d%s\n",s1.roll,s1.name);
    fscanf(fp,"%d%s\n",&s1.roll,s1.name);

    4. While reading file you were opening it in write mode instead of read mode.

    Fixing above issues makes the program working.

    Here is the sample code with above fixes.

    #include "stdio.h"
    
    #define MAX_NAME_LEN 20
    #define SUCCESS 0
    #define FAILURE 1
    
    typedef struct {
      int roll;
      char name[MAX_NAME_LEN];
    } student_t;
    
    
    int write_data(char* filename) {
      FILE* fp = NULL;
      student_t s = { 0 };
      char choice = 0;
    
      fp = fopen(filename,"w");
      if(fp == NULL) {
        printf("Error in opening file\n");
        return FAILURE;
      }
    
      do {
        printf("Enter student's roll no and name: ");
        scanf("%d%s",&s.roll, s.name);
        getchar();
        fprintf(fp,"%d%s\n", s.roll, s.name);
        printf("Add new record (Y\\N): ");
        choice = getchar();
      } while(choice == 'Y');
    
      fclose(fp);
    
      return SUCCESS;
    }
    
    
    int read_data(char* filename) {
      FILE* fp = NULL;
      student_t s = { 0 };
    
      fp = fopen(filename, "r");
      if(fp == NULL) {
        printf("Error in opening file\n");
        return FAILURE;
      }
    
      while(!feof(fp)) {
        fscanf(fp,"%d%s\n", &s.roll, s.name);
        printf("Roll no: %d\tName: %s\n", s.roll, s.name);
      }
    
      fclose(fp);
    
      return SUCCESS;
    }
    
    
    int main() {
      int ret_code = FAILURE;
      char* filename = "student.txt";
    
      ret_code = write_data(filename);
      if(ret_code == SUCCESS) {
        ret_code = read_data(filename);
      }
    
      return ret_code;
    }
    
    Few other suggestions:
    1. Avoid using global variables unless necessary.

    2. Instead of keeping return type of main as 'void' keep it 'int'. Some compilers give warning on keeping return type as 'void'. You may return the execution status of program as return value.

    3. If a function is not declared/defined before it use, by default C compilers assumes that the function does not take any parameter and it's return type is 'int' and gives warning if later there is any mismatch with above assumption. So declare/define the function before it's use.

    4. Its good to have single exit point for a program, so avaoid use of exit().

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register