Reading and writing to file using the same fstream

I have a file of data where each line consists of 4 parameters (degree, alpha, beta and x) and a value corresponding to these, organized in increasing order of the parameters. Here's the test file I'm using:

1
2
3
4
5
6
    0 1 1 0.5 3.5
    1 1 1 -0.5 -0.5
    1 2 0 0.2 4.5
    1 3 0 0.2 -4.5
    2 0 0 1.5 2.1
    2 1 0 0 5.6


My code is supposed to look for a certain set of parameters (currently set to 1 2 1 1.5, which is not contained in the file) and its value.

If it's there (i.e. the file contains the value for the right parameters) the program is done. If not it should calculate the value using a function JacobiPoly() and insert the wanted parameters and corresponding value in the appropriate place in the file.

I have a code in which I think everything works except the writing to file. I use an fstream so I can both read and write and open it with ios::out||ios::in. I'm also aware I need something similar to fseek in between input and output but according to http://stackoverflow.com/questions/17536570/reading-and-writing-to-the-same-file-using-the-same-fstream, seekp() should suffice. I'm using seekp(), but the output (in the form inout << (...)) doesn't work.

Here's the code:

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
	fstream inout; 
	inout.open("inouttest.dat", ios::out | ios::in);
	int degree=1, readDeg; 
	double alpha=2, beta=1, x=1.5, value, readAlpha, readBeta, readX, readVal; 
	bool foundVal=false;
	streampos beforeRead=(streampos)0;
	
	cout << "looking for " << degree << " " << alpha << " " << beta << " " << x << endl << endl;
	
	inout.seekg(0);
	inout >> readDeg >> readAlpha >> readBeta >> readX >> readVal;
	while(true) {
		cout << readDeg << " " << readAlpha << " " << readBeta << " " << readX << " " << readVal << endl;
		if(readDeg>degree) break;
		else if(readDeg==degree) {
			if(readAlpha>alpha) break;
			else if(readAlpha==alpha) {
				if(readBeta>beta) break;
				else if(readBeta==beta) {
					if(readX>x) break;
					else if(readX==x) {
						foundVal=true;
						cout << "found value in file" << endl;
						value=readVal;
						break;
					}
				}
			}
		}
		beforeRead=inout.tellg();
		inout >> readDeg >> readAlpha >> readBeta >> readX >> readVal;
	}
	
	if(!foundVal) {
		value=JacobiPoly(degree, x, alpha, beta);
		string restOfFile=get_file_contents(inout, beforeRead); 
		cout << endl << restOfFile << endl;
		inout.seekp(beforeRead);
		inout << degree << " " << alpha << " " << beta << " " << x << " " << value << endl << restOfFile;
	}
	
	cout << endl << value << endl;
	inout.close();


Any ideas?


EDIT: Oops, I forgot to include the method reading the rest of the file into a string, created by http://www.blogger.com/profile/06901386115570670209 in http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string get_file_contents(fstream& inout, streampos start) {
	if (inout) {
		string contents;
		inout.seekg(0, ios::end);
		contents.resize(inout.tellg()-start);
		inout.seekg(start, ios::beg);
		inout.read(&contents[0], contents.size());
		inout.close();
		return(contents);
	}
	else {
		cout << "ERROR: couldn't find file" << endl; return "";
	}
    }

Last edited on
closed account (Dy7SLyTq)
i think i read somewhere that its bad to have a file open for both reading and writing. as to your problem, i dont think its possible. however, you could read it all into a container which would allow you to do that
All writes which are not at the end of a file are destructive. There is no way to "insert" a line into a file with just a write to a file.

The usual method is to read the file in and write it back out to the file with the data you want to insert in the correct place.
Ok, thanks for the replies! I changed the code in the way you suggested and it works.
Topic archived. No new replies allowed.