Click here to Skip to main content
15,392,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
int main()
{
student s1;
FILE *fp, *temp;
int j, rno, found = 0;
fp = fopen("mystudents.txt", "r");
temp = fopen("temp.txt", "w");

printf("Enter rollno to delete : ");
scanf("%d", &rno);

while (fscanf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total))
{
	if (s1.rno == rno)
	{
		found = 1;
	}
	else
	{
		// fprintf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total);
		fprintf(fp, "%d %s %d %d %d %d", s1.rno, s1.name, s1.sub[0].mark, s1.sub[1].mark, s1.sub[2].mark, s1.total);
	}
}
fclose(fp);
fclose(temp);

if (found)
{
	temp = fopen("temp.txt", "r");
	fp = fopen("mystudents.txt", "w");

	while (fscanf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total))
	{
		// fprintf(fp, "%d %s %d %d %d %d", &s1.rno, s1.name, &s1.sub[0].mark, &s1.sub[1].mark, &s1.sub[2].mark, &s1.total);
		fprintf(fp, "%d %s %d %d %d %d", s1.rno, s1.name, s1.sub[0].mark, s1.sub[1].mark, s1.sub[2].mark, s1.total);
	}
	fclose(fp);
	fclose(temp);
}
else
{
	printf("not found");
}

return 0;
}


What I have tried:

I think the program is way too much buggy . It's taking input from the user but not compiling it. If u have any other way to do this , pls consider sharing it also.
If u have any other way to do this , pls consider sharing it also.
Posted
Updated 24-Apr-22 6:46am
v2
Comments
Rick York 23-Apr-22 16:40pm
   
Please consider learning how to spell the word "please."
merano99 24-Apr-22 6:53am
   
Code is incomplete. Missing struct student and substruct mark.

Even if that did compile - and I'm not sure it would - it won't work.
That looks like it was thrown together in the hope it would work, without any real thought being put into it.

Three things to start thinking about:
1) What do you think happens when you try to write to a file opened for read-only access?
2) What do you think would happen if you write to the same file you are reading from?
3) Are all the strings in a file the same length all the time?
   
Best is to begin a new approach, so best start with some Learn C++ tutorial and improve your skills in debugging.

Coding is somehow an art and science, but you need to have the skills and the knowledge to do it. Do some copy & paste and begging the neighbours isnt a success strategy at all.
   
It often makes sense to load everything into memory first.
C
fseek(fp, 0, SEEK_END);  // obtain file size
long lSize = ftell(fp);
rewind(fp);

// allocate memory for file
char * buffer = (char*)calloc((lSize+1)*sizeof(char), 1);

// read file into buffer
size_t result = fread(buffer, 1, lSize, fp);
fclose(fp);

You can then search for lines in memory and edit them if necessary. At the end, the entire buffer can be saved again. Functions like strchr(buffer, '\n') or strtok() could be helpful.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900