For some reason, my write position sets itself to -1

I've looked this over and I just cannot figure it out. Towards the end of this class member function, I typed cout << recordsFile.tellp()and it keeps telling me it's at position -1 before it write to the file. Before seek performs, position is the correct number.

When I compile the code,it runs through as normal, but does not update the entry in the file. This code worked before I separated it into a class member function, but the snippet of code is all the same.. so I'm guessing I'm forgetting an intialization somewhere or... who knows what.. I just can't figure out where it's failing.

Now, before you look at the code, please keep in mind that I DO NOT intend to keep the cout statements in the member function. I'm currently in the process of separating the functions and i/o statements into the main program, but I'm just making sure everything works first.

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
  void Records::editInfo(Info &entries, char *fName, int f_size)
{
	long posNum;
	string input;

	fstream recordsFile("records.dat", ios::in | ios::out | ios::binary);

	recordsFile.read(reinterpret_cast<char *>(&entries), sizeof(entries));

	while (!recordsFile.eof())
	{
		position = recordsFile.tellg();

		if(strcmp(entries.name, fName) == 0 || (strstr(entries.name, fName) != NULL))
		{	
			cout << endl;
			cout << position / sizeof(entries) << ". ";
			cout << "Name found at position " << position << "!\n\n";
			cout << entries.name << endl;
			cout << entries.addr << endl;
			cout << entries.city << endl;
			cout << entries.state << endl;
			cout << entries.zip << endl;
			cout << entries.phone << endl;
			cout << entries.acctBal << endl;
			cout << entries.dateLP << endl;
			cout << endl;

			recordsFile.read(reinterpret_cast<char *>(&entries), sizeof(entries));
		}
		else
		{
			cout << "Name not found. \n";
			recordsFile.read(reinterpret_cast<char *>(&entries), sizeof(entries));			
		}
	}

	cout << "Type the record number of the entry you would like to edit : ";
	cin >> posNum;
	cin.ignore();
	cout << endl;

	
	position = posNum * sizeof(entries) - sizeof(entries);
	
	recordsFile.seekg(position, ios::beg);
	recordsFile.read(reinterpret_cast<char *>(&entries), sizeof(entries));

	cout << "\nWriting data to position " << position << " of file.\n";

	//Get the new data from user.
	cout << "NAME: ";
	getline(cin, input);
	strcpy_s(entries.name, input.c_str());
	
	cout << "ADDRESS: ";
	getline(cin, input);
	strcpy_s(entries.addr, input.c_str());
	cout << "CITY: ";
	getline(cin, input);
	strcpy_s(entries.city, input.c_str());

	cout << "STATE: ";
	getline(cin, input);
	strcpy_s(entries.state, input.c_str());
		
	cout << "ZIP: ";
	getline(cin, input);
	strcpy_s(entries.zip, input.c_str());
		
	cout << "PHONE: ";
	getline(cin, input);
	strcpy_s(entries.phone, input.c_str());
		
	cout << "ACCOUNT BALANCE: $";
	cin >> entries.acctBal;
	cin.ignore();

	cout << "DATE OF LAST PAYMENT: ";
	getline(cin, input);
	strcpy_s(entries.dateLP, input.c_str());

	recordsFile.seekp(position, ios::beg);
	recordsFile.write(reinterpret_cast<char *>(&entries), sizeof(entries));

	cout << "Record updated. ";
	system("pause");

	recordsFile.close();
}
Solved! I had cleared the end of file marker in my last code.. Don't know what happened to it. Anyways... problem solved.
Topic archived. No new replies allowed.