0

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<int> 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

3
Contributors
13
Replies
14
Views
9 Years
Discussion Span
Last Post by still_learning
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.

0

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.

0

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

0

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

0

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.

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.

0

So for instance, if I have vector<int> 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;
}
0

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.

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'

0

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.

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);
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.