Deleting Text File Records

Hello,
I am trying to write a function that searches for, and deletes, a particular record in a text file. I am trying to achieve this by creating a temporary file, copy everything except the record i want to delete, delete the original file, and then rename the temp file using the original name. However, my code does not delete the record, and instead, sometimes copies the contents of the original file and appends.

Can anyone tell me where I went wrong, or show me a better way. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
void POS::deleteRecord(char fileName[], string itemNum) 
{                                            
 ofstream write;
 ifstream read;
 
 counter = 0;
   
 string num, name, manu, type, reso, cost, pric, quan;
   
 read.open(fileName); 

 write.open("temp.txt", ios::out);
 
 write << ""; // empty the file just in case;
  
   getline(read, num);
   getline(read, name);
   getline(read, manu);
   getline(read, type);
   getline(read, reso);
   getline(read, cost);
   getline(read, pric);
   getline(read, quan);
   getline(read, readSpace);
  
 while (!read.fail())
 {
                  
       if (itemNum.compare(num)== 0) // is item number == to our search item?
       {
         
          num = "";
          name = "";
          manu = "";
          type = "";
          reso = "";
          cost = "";
          pric = "";
          quan = "";
          readSpace = "";
          
         continue; // skip and move to the next record      
          
                
       }
       else
       {
           
            counter++;        
           // store only valid records (quantity > 0)
           write << counter << endl
                 << name << endl
                 << manu << endl
                 << type << endl
                 << reso << endl
                 << cost << endl
                 << pric << endl
                 << quan << endl
                 << endl; 
       }// end else
       
       getline(read, num);
   getline(read, name);
   getline(read, manu);
   getline(read, type);
   getline(read, reso);
   getline(read, cost);
   getline(read, pric);
   getline(read, quan);
   getline(read, readSpace);
             
 }// end while
  
 
 read.close();
 write.close();
 
 // delete the original file 
 remove (fileName);
 
 int result;
 
 // rename the temp file to original name
 result = rename("temp.txt", "Printers.txt");
  
 if (result == 0)
 cout << "  Successfully!";
 else
 cout << "  Unsuccessful!";
 
 remove("temp.txt");
  
 // reset counters
 counter = 0;
       
}
Last edited on
can you paste your code here?
Can't seam to figure this one out
I also tried vectors but now I am getting a "Permission Denied" error message and now every time I try to rerun the program, it just freezes! Any suggestions will be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# include <iostream>
# include <vector>
# include <fstream>
# include <algorithm>

using namespace std;

int main()
{
  ifstream infile;
  ofstream outfile;
  
  int counter = 0;
  
  string searchItem;
  
  cout << "Enter search Item: ";
  cin >> searchItem;
  
  infile.open("test.txt");
  outfile.open("outfile.txt");
  outfile << "";
  
  vector<string> info(9);  
   
  getline(infile, info[0]);
   getline(infile, info[1]);        
   getline(infile, info[2]);
   getline(infile, info[3]);
   getline(infile, info[4]);
   getline(infile, info[5]);        
   getline(infile, info[6]);
   getline(infile, info[7]);
   getline(infile, info[8]);// space
   
  while (!infile.fail())
  {
      
   if (info[0].compare(searchItem) == 0)
      info.erase(info.begin(), info.begin()+8);    
   else
   {
    counter++;
    
    outfile << counter << endl
            << info[1] << endl
            << info[2] << endl
            << info[3] << endl
            << info[4] << endl
            << info[5] << endl
            << info[6] << endl
            << info[7] << endl
            << info[8] << endl; // output a space   
   }
   
    getline(infile, info[0]);
   getline(infile, info[1]);        
   getline(infile, info[2]);
   getline(infile, info[3]);
   getline(infile, info[4]);
   getline(infile, info[5]);        
   getline(infile, info[6]);
   getline(infile, info[7]);
   getline(infile,info[8]);
   
  }// end while
   
   infile.clear();
  infile.seekg(0);  
  infile.close(); 
  outfile.close();
      
system ("pause");    
}
Topic archived. No new replies allowed.