Writing to files on exact spots

This is a discussion on Writing to files on exact spots within the C++ Programming forums, part of the General Programming Boards category; Hello everybody, Yup, still working on my prog I am testing now to replace words in files. However, it doesn't ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Writing to files on exact spots

    Hello everybody,

    Yup, still working on my prog I am testing now to replace words in files. However, it doesn't excactly work as I want it to. I used tellg and seekp to find the spots in txt-files, and then I wrote the word to that spot. Some1 told me that it would only work good if the words are the same length (because it'll overwrite other parts if different), so that is okay, it changes 'y' to 'n'. However, it doesn't work good! it DOES write it at the good spot, but all of the text after it is erased. So for example, take this text from a textfile:

    "hello world
    testing
    n
    1234
    boo"

    And I made my program so that it'll change n into y, it makes the file like this:

    "hello world
    testing
    y"

    And that's it... I don't want that! How can I make sure it only overwrites the amount of bits that the word is long?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    read the file into memory.make the required changes and write it out to media again!
    someone add this to the FAQ its become more frequent than how to clear the screen.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > add this to the FAQ its become more frequent than how to clear the screen
    shhhh - the FAQ is meant to be a secret document, full of dark magic which only gurus are suppost to know about.

    On the whole, it's pretty safe - no one reads the damn thing anyway.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Originally posted by Salem
    >On the whole, it's pretty safe - no one reads the damn thing anyway.
    I read it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with writing to files
    By beanroaster in forum C++ Programming
    Replies: 10
    Last Post: 12-22-2007, 11:21 PM
  2. Writing files to a CD
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-16-2003, 04:43 PM
  3. *.COM Files? Writing them?
    By johnc in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-11-2002, 01:52 AM
  4. Making files, opening them, and writing to them
    By Unregistered in forum Game Programming
    Replies: 6
    Last Post: 06-18-2002, 09:57 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21