954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Replace element in text file

Hi, I could use a little help if anyone would be willing.

I have some vectors which I am using to store input from a text file. Now I want to be able to store user input into a vector and output it to that file so that it replaces the old element in the file.

So for instance, if I have vector vecList; as my vector and I want to replace vecList[5] with an integer entered by a user, how exactly can I do that?

Thanks

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

You're asking two different things:

If you want to replace a element in a vector, ,you should pop until you've reached the element you want to change (storing all the popped values somewhere in a temp-var), then push the new value in and then push all the old values back from the temp-var.

If you want to change a number in a file, read in the file and store all the values somewhere in a var. Change the value you want to change and then delete the original file, create a new one and write everything back.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

The second route is the one I want to go with, but I am not sure how to do that. I really don't know how to store all the values in a variable as you say. And then I wouldn't know how to access to right value in that variable so that I could change it either.

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

What does your textfile look like and how many lines are in it? This is important to know to answer your question.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

My text file is just 1 line of 63 elements. Some of these are string type, some are int, and some are double.

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Please post it here, and explain which element you wish to change.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

1111 Hammers 125 125 0 12.50 20.00 2222
This is the first little strip of it......the rest of the file is exactly the same just different data. This is basically the data for "Hammers", and I want to change the element immediately after Hammers which is 125. I just want to change it from the integer that it already is, to a new integer.

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

You could use getline() and use the space as a delimiter.
Then use an if statement to see if the word from the file matches '125' and replace it. Then write everything back.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
So for instance, if I have vector vecList; as my vector and I want to replace vecList[5] with an integer entered by a user, how exactly can I do that?


Simply by vecList[5] = int_from_user;


To lookup a value to change, you can use std::find()

#include <algorithm>
...
vector<int> v;
...
const int value = 234;
vector<int>::iterator srch = find(v.begin(), v.end(), value);

if(srch != v.end())
{
    // increment the value by one
    *srch = value + 1;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

How exactly do I replace it though? I understand what you are saying with using getline() and then checking to see if the word compares...but I don't understand how to replace the 125 with another number.

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

You shouldn't look at 125 as a number, but as just another string.

ifstream file("yourfile.txt");
if (file)
{
    std::string word;
    vector <string> array;
    while ( getline(file, word, ' '))
    {
        if (word == "something") 
               word = "something else";
        array.push_back(word);
    }
}

Now you have all your words in a vector, and you've replaced 'something' with 'something else'

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

It is giving me errors since "word" is a string and I am trying to manipulate an integer. But getline is a string function isn't it? So there really is no way I can use that.

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Sure you can, you just have to convert your int to a string and compare it with the string that was read.

#include <sstream>

[...]

ifstream file("yourfile.txt");
if (file)
{
    std::stringstream ss;
    std::string str;
    std::string word;
    ss << 125;  // or any other number here
    ss >> str;
    vector <string> array;
    while ( getline(file, word, ' '))
    {
        if (word == str) 
               word = "something else";
        array.push_back(word);
    }
}
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Got it working. Thanks a bunch for your help!

still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You