1 Strange Error: Finding an int from file containing mixed (int, char) data.

I'm removing a line from my file.
User enters record. And the line containing record in the beginning is replaced by SPACES.
(This is what I thought to delete a line).

Problem is is.tellp() in os.seekp(is.tellp()); at line 12 in code. When I write ios::cur in place of is.tellp() Error is removed. But I've to use is.tellp() here in os.seekp(is.tellp()); But this causes Error.

Error:
--------------------Configuration: b - Win32 Debug--------------------
Compiling...
b.cpp
D:\VC 6.0\b\b.cpp(77) : error C2664: 'class std::basic_ostream<char,struct std::char_traits<char> > &__thiscall std::basic_ostream<char,struct std::char_traits<char> >::seekp(long,enum std::ios_base::seekdir)' : cannot convert parameter 2 from 'clas
s std::fpos<int>' to 'enum std::ios_base::seekdir'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

b.obj - 1 error(s), 0 warning(s)


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void remove(){
	ifstream is("hardware.dat");
	cout << "Enter Record Number: ";	char record[50];		cin >> record;
	for (int i = 0; record[i]
	bool found = 0;
	char f[50];
	while (!is.eof()){
		is >> f;
		if (strcmp(f,record)){
			found = true;
			ofstream os("hardware.dat", ios::out | ios::app);
			os.seekp(is.tellp());
			os << "                                                                                                                                                                                                             ";
			break;
		}
	}
	if (found){
		cout << "Removed!" << endl;
	}
	else{
		cout << "Not found." << endl;
	}
}


Complete code isn't necessary.
To see complete code of my program: https://www.dropbox.com/s/t2iveb2tuyjrz13/hardware.txt?v=0mwng
Last edited on
input file streams does not have tellp() member function. You should use tellg().
Also seekp() does nothing on stream opened with ios::app : http://en.cppreference.com/w/cpp/io/ios_base/openmode
app seek to the end of stream before each write

And line 4 of your file should give compile error too
Last edited on
Thanks MiiNiPaa, but now another problem starts.

Suppose my file a.txt has "ABC" written in it. Now I want to write a small b before capital B in the file. How will I do it. I've tried to do it but i'm having problems.
1. When opened in app mode seekp doesn't work.
2. When opened normally previous written data is erased.
I do not reccomend to open one file in several streams. Close one before opening in other.
Several possibilities:
1) Write data to another temporary file then replace original with it. (Widely used)
2) copy data afrer "A" to temporary buffer, write "b" after "A", write temporary buffer (uses too much memory for large files)
3) Write last character after last character ("ABCC")
write previous-to-last char in place of last ("ABBC")
etc...
Write character you vant to insert ("AbBC")
(Slow)
How the 2) and 3) points are done?
2) How to copy data to temporary buffer temporarily and then write to appropriate location?
3) How to write previous-to-last char in place of last?
Topic archived. No new replies allowed.