none
Reading and Writing from a File

    Question

  • Hello,

    I'm trying to read and write from a file, but I need to be able to change lines and positioning in order to do that. I can write to a file just fine, but only to the first line, overwriting what's there. I looked online for tutorials about file reading and writing, but they have fallen short on how to change the position of reading and writing in the file.

    Any help is appreciated

    Thanks
    Sunday, July 13, 2008 7:31 PM

Answers

  • Those examples should give you the information you need:
    seekp
    seekg
    • Marked as answer by Lil_Ozzy Tuesday, July 15, 2008 4:40 AM
    Sunday, July 13, 2008 9:05 PM
  • Yup, a text file is singularly unsuitable for what you're trying to do.  Not only will you burn CPU cycles trying to locate the line that you want to modify, you'll also have a big problem when what you write is more or less characters than is already there.  The only real solution is to read every single line of text and write them back one at a time, modifying the one you need to edit.

    Use a smarter file format.  A database is ideal, XML is good second choice.

    Hans Passant.
    • Marked as answer by Lil_Ozzy Tuesday, July 15, 2008 4:40 AM
    Monday, July 14, 2008 1:05 PM
    Moderator

All replies

  • seekg and seekp could do that for you ;)
    Sunday, July 13, 2008 8:45 PM
  • I know of those functions, but not how to use them. I also don't know how to change lines when reading or writing to a file.

    Sunday, July 13, 2008 8:48 PM
  • Those examples should give you the information you need:
    seekp
    seekg
    • Marked as answer by Lil_Ozzy Tuesday, July 15, 2008 4:40 AM
    Sunday, July 13, 2008 9:05 PM
  • Hm. Thanks, I'll take a look.

    Sunday, July 13, 2008 9:09 PM

  • Quote>I also don't know how to change lines when reading or writing to a file.

    Define what "to change" means. Do you mean how to change the data in a "line"?
    Or how to position to another/specific line?

    Be sure you have a good grasp of how data is stored/recorded on media or in
    memory. "Lines" is a visual concept which applies to how data is displayed
    on visual output devices such as CRTs and printers. On recorded media there
    is no "infinite white space" after a line in which to expand a line. The data in
    the line is followed by one or two characters to signal a carriage return and/or
    a linefeed to the display device. This is followed immediately by the data for
    the next line. Accordingly, to change a "line" in a file to a different length
    requires rewriting the following contents of the file. If you are overwriting a
    line with a shorter line, the remaining data in the file has to be shifted forward
    by rewriting all of the rest of the file. If you are overwriting with a longer line,
    you have to shift all following data down and rewrite it in its new position.

    Various methods can be employed to minimize disc I/O, such as in-memory
    file creation/modification. As these operations can be onerous to code, many
    programmers opt to use database or file manipulation packages/libraries.
    Note that the Standard C and C++ languages do not provide any such
    comprehensive file manipulation libraries which automatically handle data
    shifting, packing or contraction. They only provide the basic I/O functions
    needed to implement such operations.

    - Wayne



    Sunday, July 13, 2008 9:20 PM
  • I meant changing position to a specific line so I can write to that line.
    Sunday, July 13, 2008 9:28 PM
  • If you know the position of the "line" relative to the start of the file, you can
    seek to that position. If you don't know where it is, you have to read the data
    from the file until you find it. There is no "silver bullet" which will take you
    directly to a line if you don't know its position, no C/C++ function which
    positions a file to a specific "line" number.

    - Wayne



    Sunday, July 13, 2008 9:39 PM
  • Yup, a text file is singularly unsuitable for what you're trying to do.  Not only will you burn CPU cycles trying to locate the line that you want to modify, you'll also have a big problem when what you write is more or less characters than is already there.  The only real solution is to read every single line of text and write them back one at a time, modifying the one you need to edit.

    Use a smarter file format.  A database is ideal, XML is good second choice.

    Hans Passant.
    • Marked as answer by Lil_Ozzy Tuesday, July 15, 2008 4:40 AM
    Monday, July 14, 2008 1:05 PM
    Moderator