File I/O woes.

This is a discussion on File I/O woes. within the C++ Programming forums, part of the General Programming Boards category; Let me get right to the point. I have a file that has alot of information in it. I want ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Unhappy File I/O woes.

    Let me get right to the point.

    I have a file that has alot of information in it. I want to change one line of that file and leave the rest alone. Here is how I have been doing it.

    1) I open the file.
    2) I load the whole file into a string (C++ String)
    3) I search the string for a certain piece of text and get the index.
    4) I use the string's replace methon to put my new string in place of the old string

    Here is the problem.

    The text I put into the file varies in size, anywhere from 16-27 charaters in length. What happens is the string ends up overwriting the begining of the next line. Unless I imagined it, it was working until I did a final test on it. Is there a better way to search a file and replace just one line of text?
    What is C++?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Post your code.

    What you want to do is use string delimeters. A value in the string that you want to edit up to and stop. Basically make a loop and start editting, if you input your changes and don't reach the delimeter, start moving the rest of the string forward. If you don't finish editing and reach the delimeter, then you have to move the rest of the array back.

    Ofcourse this would be nice and easy if you're using the string datatype.

    Just find the part you want to edit and put everything before it in a string. Then find everything after that and put it into another string. Then just add.

    StringTotal = String1 + StringToAdd + String2
    Last edited by SlyMaelstrom; 11-23-2005 at 09:19 PM.
    Sent from my iPadŽ

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Thanks sly. I didn't even think about that.

    I am too ahamed to post my code now. You wouldn't want to see how I was doing it.
    What is C++?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    2) I load the whole file into a string (C++ String)
    Ok, so apparently you have something like this:
    Code:
    string str = "line1\nline2\nline3";
    3) I search the string for a certain piece of text and get the index.
    int pos = str.find('n');

    4) I use the string's replace method to put my new string in place of the old string
    Code:
    if(pos != string::npos)
    {
    	str.replace(pos, 1, "hello world");
    }
    Then I assume your write the string back to the file. Where does the overwriting of the beginning of the next line happen?
    Last edited by 7stud; 11-23-2005 at 11:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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