944,184 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 5796
  • C RSS
Dec 15th, 2004
0

Deleting a record from a file

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PriyaJaiGanesh is offline Offline
4 posts
since Dec 2004
Dec 15th, 2004
0

Re: Deleting a record from a file

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 15th, 2004
0

Re: Deleting a record from a file

Quote originally posted by PriyaJaiGanesh ...
(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.
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Dec 16th, 2004
0

Re: Deleting a record from a file

Quote ...
(1)I have problem in deleting a record from a file.
Lets say you want to delete record 3 in a 10 record file:

  1. int rec_to_delete = 2; // record 3 is index 2
  2.  
  3. for(int i = 0; i < 9; i++)
  4. {
  5. if(i != rec_to_delete)
  6. {
  7. // copy record
  8. }
  9. }

use the if statement so that the record number you want to delete doesnt get copied.
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Sep 6th, 2010
0
Re: Deleting a record from a file
syntax to rename a file is
int rename(char *old_filename,char *new_filename);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shridhark is offline Offline
1 posts
since Sep 2010
Sep 7th, 2010
0
Re: Deleting a record from a file
  1. I Think this code will help you to solve your problem:
  2.  
  3. Add Some Student:
  4.  
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<io.h>
  9. #include<fcntl.h>
  10.  
  11. struct student
  12. {
  13. char name[20];
  14. char semCode[10];
  15. int enrollNo;
  16. int assignSub;
  17. int marks;
  18. int attendence;
  19. };
  20. struct student var;
  21. FILE *ptr;
  22.  
  23. void refresh()
  24. {
  25. fclose(ptr);
  26. ptr = fopen("c:\\file.txt","a+");
  27. }
  28. void addStudentRecord()
  29. {
  30. refresh();
  31. fflush(stdin);
  32. printf("Enter name:\n");
  33. gets(var.name);
  34.  
  35. fflush(stdin);
  36. printf("Enter Semester Code:\n");
  37. gets(var.semCode);
  38.  
  39. fflush(stdin);
  40. printf("Enter Enrollment Number:\n");
  41. scanf("%d",&var.enrollNo);
  42.  
  43. var.assignSub=0;
  44. var.marks=-1;
  45.  
  46. fwrite(&var,sizeof(struct student),1,ptr);
  47.  
  48. printf("Data Entered Successfully\n");
  49. printf("Press any key..");
  50. refresh();
  51. getch();
  52. }
  53.  
  54. When You want to delete the record:
  55.  
  56. void deleteStudentRecord()
  57. {
  58. int enrollNo;
  59. int counter=0;
  60.  
  61.  
  62. FILE *ptr2 = fopen("c:\\file2.txt","a");
  63.  
  64. int records = getNoOfRecords();
  65.  
  66. refresh();
  67. fflush(stdin);
  68. printf("Enter Enrollment Number:\n");
  69. scanf("%d",&enrollNo);
  70.  
  71. while(counter!=records)
  72. {
  73. fread(&var,sizeof(struct student),1,ptr);
  74. if(var.enrollNo==enrollNo)
  75. {
  76.  
  77. }
  78. else
  79. {
  80. fwrite(&var,sizeof(struct student),1,ptr2);
  81. }
  82. counter++;
  83. }
  84. fcloseall();
  85. remove("c:\\file.txt");
  86. rename("c:\\file2.txt","c:\\file.txt");
  87. printf("Press any key..");
  88. getch();
  89. }
  90.  
  91. void main()
  92. {
  93. int a=0;
  94.  
  95. ptr = fopen("c:\\file.txt","a+");
  96.  
  97. while(a!=2)
  98. {
  99. system("cls");
  100. printf("Select an Option:\n");
  101. printf("1.Add Student Record\n");
  102. Printf("2.Delete Student Record\n");
  103. scanf("%d",&a);
  104.  
  105. switch(a)
  106. {
  107. case 1:
  108. addStudentRecord();
  109. break;
  110.  
  111. case 2:
  112. updateStudentRecord();
  113. break;
  114.  
  115. case 3:
  116. deleteStudentRecord();
  117. break;
  118. default:
  119. printf("Invalid Option\n");
  120. break;
  121. }
  122. }
  123. }
  124.  
  125. 2. rename file:
  126.  
  127. rename("c:\\file2.txt","c:\\file.txt");
Reputation Points: 10
Solved Threads: 2
Newbie Poster
1seo is offline Offline
22 posts
since Aug 2010
Sep 7th, 2010
0
Re: Deleting a record from a file
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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snivas519 is offline Offline
10 posts
since Aug 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 4 cups game in c
Next Thread in C Forum Timeline: Graphics.h is not running in turbo c++ 4.5?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC