I have to make a project, where the user can see the student's fee records. He can also modify, delete and add a new record. But i am getting problem while running it. Anyone can please help me :(
here is my program

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *A, *B;
char another; int choice;
struct student
{
char name[40],f_name[40];int age,clas;long int rnum, fee;};
struct student s;
char stdname[40];
long int recsize;
A=fopen("fee.txt","rb+");
recsize=sizeof(s);
printf("1-Add new record \n 2-Display record \n 3-Edit record \n 4-delete record \n now enter your choice:- ");
scanf("%d",&choice);
switch(choice)
{case '1':
fseek(A,0,SEEK_END);
another='y';
do
{
printf("enter name , father name, age, class, roll num, fee");
scanf("%s %s %d %d %ld %ld", &s.name, &s.f_name, &s.age, &s.clas, &s.rnum, &s.fee);
fwrite(&s,recsize,1,A);
printf("Add another ? y/n");
scanf("%c",&another);
}
while(another=='y');
break;
case '2':
rewind(A);
while(fread(&s,recsize,1,A))
printf("\n %s %s %d %ld %ld", s.name,s.f_name,s.age,s.clas,s.rnum,s.fee);
break;
case 3:
do{
printf("\nenter the name of student to be modifief:");
scanf("%s",&stdname);
rewind(A);
while(fread(&s,recsize,1,A)==1)
{
if(strcmp(s.name, stdname)==0)
{
printf("enter new name, father name, age, class, roll num, fee:");
scanf("%s %s %d %d %ld %ld", &s.name, &s.f_name, &s.age, &s.clas, &s.rnum, &s.fee);
fseek(A,-recsize,SEEK_CUR);
fwrite(&s,recsize,1,A);
break;
}
}
printf(" modify anothe?");
fflush(stdin);
another=getche();
}
while(another=='y');
break;
case '4':
another='y';
while(another=='y')
{
printf("\nenter the name name of student whom you want to delete");
scanf("%s",&stdname);
B=fopen("tfee.tst","wb");
rewind(A);
while(fread(&s,recsize,1,A)==1)
{ 
if(strcmp(s.name,stdname)!=0)
fwrite(&s,recsize,1,A);}
 fclose(A);
 fclose(B);
 remove("fee.txt");
 rename("tfee.txt","fee.txt");
 A=fopen("fee.txt","rb+");
 printf("delete anothe");
 fflush(stdin);
 another=getche();
 }
 break;
case '0':
fclose(A);
exit(0);
}
return 0;
getch();
}

Thank you!

Edited 3 Years Ago by Dani: Formatting fixed

Use the |CODE| blocks to keep your code lined up straight.
This is the code with indentation, spelling corrections, and consistency edits. The code is functionally the same.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main(){
	FILE *A, *B;
	char another; int choice;
	struct student{
		char name[40],f_name[40];
		int age,clas;
		long int rnum, fee;
	};
	struct student s;
	char stdname[40];
	long int recsize;
	A=fopen("fee.txt","rb+");	//Need to add check if A is NULL
	recsize=sizeof(s);
	printf("1-Add new record \n 2-Display record \n 3-Edit record \n 4-delete record \n now enter your choice:- ");
	scanf("%d",&choice);
	switch(choice){
	case '1':
		fseek(A,0,SEEK_END);
		another='y';	//Unnecessary
		do{
			printf("enter name , father name, age, class, roll num, fee");
			scanf("%s %s %d %d %ld %ld", &s.name, &s.f_name, &s.age, &s.clas, &s.rnum, &s.fee);
			fwrite(&s,recsize,1,A);
			printf("Add another ? y/n");
			scanf("%c",&another);
		}while(another=='y');
		break;
	case '2':
		rewind(A);
		while(fread(&s,recsize,1,A)){
			printf("\n %s %s %d %ld %ld", s.name,s.f_name,s.age,s.clas,s.rnum,s.fee);
		}
		break;
	case 3:
		do{
			printf("\nenter the name of student to be modified:");
			scanf("%s",&stdname);
			rewind(A);
			while(fread(&s,recsize,1,A)==1){
				if(strcmp(s.name, stdname)==0){
					printf("enter new name, father name, age, class, roll num, fee:");
					scanf("%s %s %d %d %ld %ld", &s.name, &s.f_name, &s.age, &s.clas, &s.rnum, &s.fee);
					fseek(A,-recsize,SEEK_CUR);
					fwrite(&s,recsize,1,A);
					break;
				}
			}
			printf(" modify another?");
			fflush(stdin);
			another=getche();
		}while(another=='y');
		break;
	case '4':
		do{
			printf("\nenter the name name of student whom you want to delete");
			scanf("%s",&stdname);
			B=fopen("tfee.tst","wb"); //Need to check if B returned NULL
			rewind(A);
			while(fread(&s,recsize,1,A)==1){
				if(strcmp(s.name,stdname)!=0){
					fwrite(&s,recsize,1,A);
				}
			}
			fclose(A);
			fclose(B);
			remove("fee.txt");
			rename("tfee.txt","fee.txt");
			A=fopen("fee.txt","rb+");
			printf("delete another");
			fflush(stdin);
			another=getche();
		}while(another=='y');
		break;
	case '0':
		fclose(A);
		exit(0);
	}
	return 0;
	getch();
}

Edited 4 Years Ago by DeanMSands3: n/a

First time compiling:

Records.c:36:1: warning: too many arguments for format [-Wformat-extra-args]

Have a look at that.


Edit: And yes, but make sure to exit out of the program.

Edited 4 Years Ago by DeanMSands3: n/a

Actully my problem is that when i run this program the output screen come. and the program ask the user to chose his choice.
like
"1-Add new record
2-Display record
3-Edit record
4-delete record

now enter your choice:-"

but when the user press any of them and then press "enter" , program exits :(

Oh! Well, your problem is simple. Take off the single quotes from the numbers in the case statements.
Number 1 has a numerical value of 1 where as '1' the character has a numerical (ASCII) value of 49 or (0x31 in hex).
Since choice is an integer, it's going off it's numerical value.

Edit: And also add a second "%d" for s.clas in line 35.

Edited 4 Years Ago by DeanMSands3: n/a

Oh it works :) thank you. The program'll automatically save all the data ??

Loll. Now program runs. But it doesn't ask "do you want to enter another" and when user enter 2 it come out of the program. Same with 3, 4

That's a good question. Now that I've had a chance to look at your code, I'm seeing what it's doing. I'm not thrilled with your way of doing things, but if it works, it works.
Each switch case modifies the file using different methods. Personally, I'd consolidate the bit where you write to the file into a single function or a set of function. And you really need more than one function.
But, like I said, if it works, it works.

actually i am newly learning C. so i am getting prob with basics :/ and i hope that i'll learn all this soon but till then i really need your help. so can you rewrite me the program? and tell me what's going wrong?

Well, I can't do that. (Or at least shouldn't.)
Here's a starting point. I put your first switch/case block into a function. I declared the function in the beginning and defined it later on. But to do this, I had to move your struct definition outside the main function to make it global. I also passed it two pointers. Notice that even this "rewrite" really isn't an example of good coding practices since the student s structure is open to use by any function instead of creating a local copy. But a local copy would mangle the computer's stack memory since local function variables are kept on the stack. But computers are resilient enough that's really not an issue I guess.

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

struct student{
	char name[40],f_name[40];
	int age,clas;
	long int rnum, fee;
};
void addNewStudent(FILE *A, struct student *s);

int main(){
	struct student s;
	char stdname[40];
	long int recsize;
	FILE *A, *B;
	char another;
	int choice;
	A=fopen("fee.txt","rb+");	//Need to add check if A is NULL (
	if(A==NULL){
		printf("Could not read input file.\n");
		exit(-1);
	}
	recsize=sizeof(s);
	printf("1-Add new record \n 2-Display record \n 3-Edit record \n 4-delete record \n now enter your choice:- ");
	scanf("%d",&choice);
	switch(choice){
	case 1:
		addNewStudent(A, &s);
		break;
	case 2:
		rewind(A);
		while(fread(&s,recsize,1,A)){
			printf("\n %s %s %d %d %ld %ld", s.name,s.f_name,s.age,s.clas,s.rnum,s.fee);
		}
		break;
	case 3:
		do{
			printf("\nenter the name of student to be modified:");
			scanf("%s",&stdname);
			rewind(A);
			while(fread(&s,recsize,1,A)==1){
				if(strcmp(s.name, stdname)==0){
					printf("enter new name, father name, age, class, roll num, fee:");
					scanf("%s %s %d %d %ld %ld", &s.name, &s.f_name, &s.age, &s.clas, &s.rnum, &s.fee);
					fseek(A,-recsize,SEEK_CUR);
					fwrite(&s,recsize,1,A);
					break;
				}
			}
			printf(" modify another?");
			fflush(stdin);
			another=getche();
		}while(another=='y');
		break;
	case 4:
		do{
			printf("\nenter the name name of student whom you want to delete");
			scanf("%s",&stdname);
			B=fopen("tfee.tst","wb"); //Need to check if B returned NULL
			if(B==NULL){
				printf("Could not read output file.\n");
				exit(-1);
			}
			rewind(A);
			while(fread(&s,recsize,1,A)==1){
				if(strcmp(s.name,stdname)!=0){
					fwrite(&s,recsize,1,A);
				}
			}
			fclose(A);
			fclose(B);
			remove("fee.txt");
			rename("tfee.txt","fee.txt");
			A=fopen("fee.txt","rb+");
			printf("delete another");
			fflush(stdin);
			another=getche();
		}while(another=='y');
		break;
	case 0:
		fclose(A);
		exit(0);
	}
	return 0;
	getch();
}
void addNewStudent(FILE *A, struct student *s){
	char another;
	fseek(A,0,SEEK_END);
	do{
		printf("enter name , father name, age, class, roll num, fee");
		scanf("%s %s %d %d %ld %ld", &s->name, &s->f_name, &s->age, &s->clas, &s->rnum, &s->fee);
		fwrite(&s,sizeof(struct student),1,A);
		printf("Add another ? y/n");
		scanf("%c",&another);
	}while(another=='y');
}
This article has been dead for over six months. Start a new discussion instead.