Please help me find error in the program

#include
#include
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);
}

Replies

  • pratik007
    pratik007
    samaira
    #include
    #include
    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
  • pradeep_agrawal
    pradeep_agrawal
    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

You are reading an archived discussion.

Related Posts

import java.io.*; public class new { public static void main(String args[]) { String name; BufferedReader reader; reader=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\n What is your name?"); try { name=reader.readLine(); System.out.println("hello,"+name+"!"); } catch...
hey me fellow CEans, I am doing enigneering from ip university, Delhi! The college is good and reputed. And i have managed to clear all the backlogs i have as...
Hi 4 all i m from afghanistan, i m hacker is any1 need help can contact me, may many member know by my user AK-47 king of hacker.... i have...
Hi Guys. I am Sudheer,a mechanical engineer and I am in 7 th semister now. I want to write GATE-2010 exam and i had written it in 2009 (got 89...
I have been looking for some Linux stuff out here. Very few people and only computer guys seem to be interested in it. What do you think of having a...