Change the file stream object's read position in C++.
Can I change a file stream object's read position in C++? I mean I have a file with a series of integers separated by a newline character. Can I read the first five integers into some variables and then move the read position back to read the fifth integer one more time?
You can move the position (using seekg) either by an offset, or to a previous point (that was saved by tellg). Unless your file is very strictly formatted, you probably want the latter. So in your case, call tellg to get the file position before reading the fifth line, then use seekg to return to that position.
Though if you're saving those integers into variables, why do you need to read them again? They're already in memory for you. :icon_rolleyes:
Well, actually i was trying to read two integers at a time, perform some comparison and then write them to another file! :-/ It was kinda like performing bubble sort without using array.
Anyway, thanks a lot for help! :)
can anyone plz take a look at my code? I think it should work, but it didn't.
Here's my code:
using namespace std;
for ( int n = 0; n <= 2; n++)
int min = 0, min2,a;
cout << "position before read= " << IN.tellg() << endl;
IN >> min;
IN >> a;
if (a < min) min = a;
cout << "position after reset = " << IN.tellg() << endl;
Basically, what my code would do is to move the reading position in a file back to the beginning after it reach eof. And it should do that three times. But actually, it seems to work ONLY once~ Can anyone tell me why? :-/
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:
Runtime run = Runtime.getRuntime();
Process pr = ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.