hai!

(1)I have problem in deleting a record from a file.

I was told that the only way to delete a record from a file is to copy the file to another temporary file and then copy the records to the same file again by skipping the record that u need to delete i don't have problem until the copying of the file to the temporary file but i don't know how to skip the record that needs to be delete.

(2)what's the command to rename a file?

Plz anybody help me!


Urs,
PriyaJaiGanesh.

read the old file, record for record.
Write all records to the tempfile, except the one(s) you want to delete.
Remove the old file.
Copy the tempfile completely to a new file with the same name as the old one.
Remove the tempfile.

(2)what's the command to rename a file?

int rename (const char *oldname, const char *newname)
The rename function renames the file oldname to newname. The file formerly accessible under the name oldname is afterwards accessible as newname instead. If rename fails, it returns -1.

(1)I have problem in deleting a record from a file.

Lets say you want to delete record 3 in a 10 record file:

int rec_to_delete = 2; // record 3 is index 2 

for(int i = 0; i < 9; i++)
{
    if(i != rec_to_delete)
    {
        // copy record
    }
}

use the if statement so that the record number you want to delete doesnt get copied.

syntax to rename a file is
int rename(char *old_filename,char *new_filename);

I Think this code will help you to solve your problem:

Add Some Student:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include<fcntl.h>

struct student
{
	char name[20];
	char semCode[10];
	int enrollNo;
	int assignSub;
	int marks;
	int attendence;
};
struct student var;
FILE *ptr;

void refresh()
{
	fclose(ptr);
	ptr = fopen("c:\\file.txt","a+");
}
void addStudentRecord()
{
	refresh();
	fflush(stdin);
	printf("Enter name:\n");
	gets(var.name);

	fflush(stdin);
	printf("Enter Semester Code:\n");
	gets(var.semCode);

	fflush(stdin);
	printf("Enter Enrollment Number:\n");
	scanf("%d",&var.enrollNo);
	
	var.assignSub=0;
	var.marks=-1;

	fwrite(&var,sizeof(struct student),1,ptr);

	printf("Data Entered Successfully\n");
	printf("Press any key..");
	refresh();
	getch();
}

When You want to delete the record:

void deleteStudentRecord()
{
	int enrollNo;
	int counter=0;
	

	FILE *ptr2 = fopen("c:\\file2.txt","a");

	int records = getNoOfRecords();

	refresh();
	fflush(stdin);
	printf("Enter Enrollment Number:\n");
	scanf("%d",&enrollNo);

	while(counter!=records)
	{
		fread(&var,sizeof(struct student),1,ptr);
		if(var.enrollNo==enrollNo)
		{

		}
		else
		{
			fwrite(&var,sizeof(struct student),1,ptr2);
		}
		counter++;
	}
	fcloseall();
	remove("c:\\file.txt");
	rename("c:\\file2.txt","c:\\file.txt");
	printf("Press any key..");
	getch();
}

void main()
{
	int a=0;

	ptr = fopen("c:\\file.txt","a+");

	while(a!=2)
	{
		system("cls");
		printf("Select an Option:\n");
		printf("1.Add Student Record\n");
		Printf("2.Delete Student Record\n");
		scanf("%d",&a);

		switch(a)
		{
				case 1:
				addStudentRecord();
				break;
			
			case 2:
				updateStudentRecord();
				break;

			case 3:
				deleteStudentRecord();
				break;
			default:
				printf("Invalid Option\n");
				break;
		}
}
}

2. rename file:

rename("c:\\file2.txt","c:\\file.txt");

Rather than doin this,it s better to read the data and store it in some array or linklist then perform that delete operation in that array and now write the new array in to your file....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.