-1

I'm so sorry if this question already is answered. If it is, please direct me to the solution.

I'm currently trying to go through a text file, iterate through it to find a numeric value, pipe the value into a child/parent process, where I then return the updated value into a text. As simple as it is, I'm trying to find the numbers and either put them into an array/string, so I can feed it to a pipe and then feed it back into the text. I have been working on this for 5 days, so I'm finally asking you guys for help. Again, apologies if this already exists.

I'm currently going through the text and checking each character if it's an integer. I'm able to put it into a string, but it's just one big long string. I'm not sure how else to implement it.

This is what I have so far.

while(!readText.eof())
    {
      while(getline(readText, item))
      {
      readText >> item;
      for(int i=0; i < item.length(); ++i){
        if(isdigit(item[i])) //checking if it is a digit
          newitem += item[i]; //puts into string
        if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I 
                              //should put into new variable?
           cout << item[i];
        else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe 
                                //put it into a variable?
            cout << " ";
        }
      }
 }

my challenge is trying to figure out how to separate the values and put them back. I would grow with the index, but the numbers will grow, so the position would not be as accurate.


Updated context

Thank you everyone for the help. A sample file would be

In the b6765lue sky there are 59 clouds. The gra7ss is green 766 and it 837 has 7 a cow in i87t.

I would need to change the numbers through a process/function. So like you mentioned the size of the numbers will naturally become larger. The test files can be 100s of lines.

In the b6789lue sky there are 83 clouds. The gra31ss is green 790 and it 861 has 31 a cow in i111t.

The values have been changed with 5 processes and and incremental value of 5.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
AGirlThatCodes
  • 575
  • 7
  • 21
  • 1
    `while(!readText.eof())` -- [Please read why this is considered wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – PaulMcKenzie Oct 12 '17 at 02:02
  • Could you include a small sample of an example file, and what your expected output is meant to be? – Tas Oct 12 '17 at 02:04
  • I still don't understand what the final output would be or what the numbers are that are supposed to come out of this, given the line of input you posted. You have numbers mixed with letters, ok, so what you supposed to do with that? You have numbers with no letters, what are you to do with that? – PaulMcKenzie Oct 12 '17 at 03:32
  • I'm so sorry! I will update. – AGirlThatCodes Oct 12 '17 at 03:34
  • thank you very much @PaulMcKenzie for the tip. I really appreciate it! – AGirlThatCodes Oct 13 '17 at 01:36

1 Answers1

0

Why don't you want to save a starting position, of a value and a new value, to print if you reach that position? So, it seems to me like:

struct replace_integer
{
  int old_int_length;
  int new_int_value;
  int int_to_replace_text_position;
};

and than, when you print it down, just skip old int and print new one. Will it work?

To remember a start position you should modify your cycle:

int item_length = item.length();
for(int i=0; i < item_length ; ++i)
{
    if(isdigit(item[i]))
    {
      int_to_replace_text_position = i;
      while(isdigit(item[i++]) && i < item_length )
         newitem += item[old_int_length_counter]; //puts into string
      old_int_length = i - int_to_replace_text_position; //check it, i may have a 1 position mistake, too sleepy, so I can lose "-1"
    }

    if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I 
                          //should put into new variable?
       cout << item[i];
    else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe 
                            //put it into a variable?
        cout << " ";
}
Alex
  • 94
  • 9
  • Thank you so much for your suggestions! I have a further question, if I have multiple values, would I make a for loop to feed them in and iterate through? – AGirlThatCodes Oct 12 '17 at 02:33
  • Just looked at your nickname, it is really funny and cool) Sorry. What do you mean by "to feed them"? Can you just use not an array but a vector of smth, and use an iterator to iterate? – Alex Oct 12 '17 at 02:36
  • Thanks! I'm using a pipe like this one (https://stackoverflow.com/questions/21664844/linux-pass-value-between-parent-process-and-child-process-using-pipe-in-c), so the value is going through a child process and is being changed through n number of processes. – AGirlThatCodes Oct 12 '17 at 02:47
  • Dumb question, how would I use a vector? I'm still very rusty on my c++ because I'm a ruby gal that changed to c++ and java. – AGirlThatCodes Oct 12 '17 at 02:48
  • 1
    Well, have you read this: http://en.cppreference.com/w/cpp/container/vector ? Just make a class from a struct I've wrote above. – Alex Oct 12 '17 at 02:52
  • Thank you! On your original suggestion, how would you save the starting position? – AGirlThatCodes Oct 12 '17 at 03:23
  • Hi Alex, I ended up approaching this differently. However, I am grateful for your suggestion and will give you the points. I will post my hacky way after. I say hacky because it's probably not as efficient. – AGirlThatCodes Oct 13 '17 at 01:38