473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

file position

When outputting or inputting to a file is there some easy way to move
to and overwrite a specific line? With seekg it seems you can only
move positions without knowing whether you've moved to a newline or
not.
Jun 30 '08 #1
2 2370
On Jun 30, 9:26 am, "Alf P. Steinbach" <al...@start.nowrote:
* nko...@gmail.com:
When outputting or inputting to a file is there some easy
way to move to and overwrite a specific line?
No, not if you're talking about an ordinary text file.
If you know the length of the line, there is. You have to save
the position prior to reading the line.
That hasn't anything to do with C++, it has to do with the
structure of an ordinary text file.
Not really. The C++ standard says very clearly what you can and
cannot do with regards to seeking in a text or a binary file.
(Actually, I think I'm lying about "clearly". The C++ standard
defines pretty much everything concerning IO by reference to the
C standard, which means that you usually have to look in two
different standards. And even when it doesn't, "clear" and "the
C++ Standard" seems to be a oxymoron.)
With seekg it seems you can only move positions without
knowing whether you've moved to a newline or not.
Yes.
The one argument form of seekg just calls seekpos on the
streambuf. If the streambuf is in fact a filebuf, "If sp has
not been obtained by a previous successful call to one of the
positioning functions (seekoff or seekpos) on the same file the
effects are undefined." The two argument form calls
streambuf::seekoff, which for a filebuf is defined "as if" you
called fseek, which (in the C standard) says: "For a text
stream, either offset shall be zero, or offset shall be a value
returned by an earlier successful call to the ftell function on
a stream associated with the same file and whence shall be
SEEK_SET."

If you want to move around in an arbitrary fashion in a text
file, seeking according to the line number, you have to first
read the entire file (using getline, for example), saving the
results of ftell in an array.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 30 '08 #2
On Jun 30, 12:49 pm, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Jun 30, 9:26 am, "Alf P. Steinbach" <al...@start.nowrote:
* nko...@gmail.com:
>When outputting or inputting to a file is there some easy
way to move to and overwrite a specific line?
No, not if you're talking about an ordinary text file.
If you know the length of the line, there is. You have to
save the position prior to reading the line.
You're talking about a special case where (1) you've been at
that line sometime earlier, (2) you saved the position then,
and (3) the new line is exactly the same length as the old.
There are often such rare corener special cases that are
possible for something that isn't possible in general.
It's quite misleading to put that up as if it contradicted
what I wrote.
Yes and no. I forget what you'd actually written, but it's not
important. Such cases are rare, unless you actively take steps
to create them.
That hasn't anything to do with C++, it has to do with the
structure of an ordinary text file.
Not really.
Really. ;-)
For the general problem, it's the same in any language.
Is it? Or maybe the question is: what is the general problem?
We regularly seek to arbitrary lines in files here, using the
Posix interface (which doesn't distinguish between text files
and binary files). Of course, you can't reliably do it under
Posix in an arbitrary file written with the editor; we also
write the files, and take care that all of the lines have the
same known length. I've done similar things in Fortran, in the
past. The general problem here is that the C++ language doesn't
provide an interface for this sort of things; other languages
do.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 30 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
2
by: rr | last post by:
Hi, i have to write a file composed of n packets of a given length. I need to write this file writing every packet when i get it, knowing its number and length. I mean: packet #0 -length 100...
2
by: Senthil | last post by:
Hi, The need is to read each line in a text file and based on the read data decision has to be taken whether to reposition the file pointer. The StreamReader.Position does not give current...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: Eric | last post by:
I am trying to save the "last read" position of a file using a StreamReader object. I am reading the lines of the file using StreamReader.ReadLine and then saving the current position in the...
10
by: olivier.scalbert | last post by:
Hello, the following constraints: source: <Source> <Item>AAA</Item> <Item>BBBBBBBBBBB</Item> <Item>CCCCCCCCC</Item> <Item>DDDDDDDDDDDDDDD</Item>
75
by: ume$h | last post by:
/* I wrote the following program to calculate no. of 'a' in the file c:/1.txt but it fails to give appropriate result. What is wrong with it? */ #include"stdio.h" int main(void) { FILE *f;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.