如果这个问题已经得到解答,我很抱歉。如果是,请指导我解决。
我正在尝试浏览一个文本文件,遍历它以查找数值,将值传递给子/父进程,然后我将更新后的值返回到文本中。虽然它很简单,但我试图找到数字并将它们放入数组/字符串中,这样我就可以将它提供给管道,然后将其反馈到文本中。我已经为此工作了5天,所以我终于要求你们帮忙了。再次,如果这已经存在,请道歉。
我正在浏览文本并检查每个字符是否为整数。我能把它放入一个字符串中,但它只是一个很长的字符串。我不确定如何实现它。
这是我到目前为止所做的。
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 << " ";
}
}
}
我的挑战是试图弄清楚如何分离价值并将它们放回去。我会跟索引一起成长,但数字会增长,所以位置不会那么准确。
更新后的上下文
谢谢大家的帮助。样本文件是
In the b6765lue sky there are 59 clouds. The gra7ss is green 766 and it 837 has 7 a cow in i87t.
我需要通过流程/功能更改数字。所以就像你提到的那样,数字的大小自然会变大。测试文件可以是100行。
In the b6789lue sky there are 83 clouds. The gra31ss is green 790 and it 861 has 31 a cow in i111t.
值已更改为5个进程,增量值为5。
答案 0 :(得分:0)
如果您到达该位置,为什么不想保存起始位置,值和新值?所以,在我看来似乎:
struct replace_integer
{
int old_int_length;
int new_int_value;
int int_to_replace_text_position;
};
而且,当你打印它时,只需跳过旧的int并打印新的int。它会起作用吗?
要记住开始位置,您应该修改周期:
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 << " ";
}