Searching and Replacing in Files

Hello pro's,
I have a problem with this project I'm working on, the code involves searching for and replacing a structure in a binary file. I have written a similar (smaller) code that replicates the problem. This code will write 3 structures (each with only one int variable) to a binary file, It will then ask the user which number they want to search for and change, then display the new numbers. The problem with this code is the searching and changing part. I've tried so many different things and I'm at a stand still. If anyone has any advice to give it sure would be greatly appreciated. I've labeled what I'm certain is the problem code below.

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
#include <iostream>
#include <fstream>
using namespace std;

struct num
{
	int number;
};

int main()
{
    long pos;
    int change_num;
	num object;
	// write three simply structures to the file
	fstream open("number.dat", ios::out | ios::binary);
	if(!open)
    {
       cout << "error opening file\n";
        return 0;
    }
	for (int i = 0; i < 3; i++)
	{
		cout << "enter number for structure" << i+1 <<endl;
		cin >> object.number;
		open.write(reinterpret_cast<char *>(& object), sizeof(object));
	}
	open.close();

    
	// now display all four
	fstream look("number.dat", ios::in | ios::binary);
	if (!look)
    {
        cout << "error opening file\n";
        return 0;
    }
    
	look.read(reinterpret_cast<char *>(&object), sizeof (object));
	while (!look.eof())
	{
		cout << object.number << endl;
		look.read(reinterpret_cast<char *>(&object), sizeof(object));
	}
	look.close();
	
    /******************************* PROBLEM CODE ***************************/
    // now attempt to seach for a specific one and change it
    cout << "\nenter a number to search for and change\n";
    cin >> change_num;
    
    fstream search_replace("number.dat", ios::in| ios::out | ios::binary);
    if (!search_replace)
    {
        cout << "error opening file\n";
        return 0;
    }
    
    search_replace.read(reinterpret_cast<char *>(&object), sizeof (object));
	while (!search_replace.eof())
    {
        if (object.number == change_num)
        {
            pos = search_replace.tellp();
            cout << "\nenter a new number for this position in the file\n";
            cin >> object.number;
            search_replace.seekp(pos,ios::beg);
            search_replace.write(reinterpret_cast<char *>(&object), sizeof(object));
            break;
        }
    }
    search_replace.close();
    /**********************************************************************/
    
    

	// display the new list
	fstream lookie("number.dat", ios::in | ios::binary);
	if (!lookie)
    {
        cout << "error opening file\n";
        return 0;
    }
    
	lookie.read(reinterpret_cast<char *>(&object), sizeof (object));
	while (!lookie.eof())
	{
		cout << object.number << endl;
		lookie.read(reinterpret_cast<char *>(&object), sizeof(object));
	}
	lookie.close();
    
}

Last edited on
Topic archived. No new replies allowed.