January 21

On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

void toV(string a,ref vector!string b){
    auto f=File(a,"r");
    while(!f.eof()){
        string l=strip(f.readln());push(b,l);
    }
    Qwk(b);
}//

There is an issue with the vector here, I don't know why the subsequent functions will affect the content of the vector here!

January 21

On Sunday, 21 January 2024 at 03:54:35 UTC, zjh wrote:

>

On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

void toV(string a,ref vector!string b){
    auto f=File(a,"r");
    while(!f.eof()){
        string l=strip(f.readln());push(b,l);
    }
    Qwk(b);
}//

There is an issue with the vector here, I don't know why the subsequent functions will affect the content of the vector here!

Hello,

some thoughts on your issue:

  1. you said that it fails in the foreach loop.
void gg(string a){
    Vector!string d=[];toV(a,d);//File to Vector
    print(d);//3,OK
    foreach(e;d){
        string b=e;//.1
        string m=readText(b);ff(m,b);
    }
}

I was thinking that the foreach loop is essentially utilizing an iterator.
When a print function accesses the value it invokes the mechanics of the iterator to get the value. But if you assign the value it might be a pointer to the iterator instead of the value.

Can you try to iterate in a different manner without the iterator?

  1. You perform push and pop functions of the vector to remove trailing empty lines that might change the memory layout. if you do not copy the values you might have outdated pointers.

What if you add more lines to the file. Does it crash then later?

January 22

On Sunday, 21 January 2024 at 18:38:42 UTC, atzensepp wrote:

> >

What if you add more lines to the file. Does it crash then later?

It's a problem with vectors. vector!string has a problem , I just use 'string []', and it's OK.
The vector is unstable, possibly because the later data overwrites the previous data.

1 2
Next ›   Last »