String problems response server

This subrutine read file and transfer to string.

The file is a gcode .

I want to read line by line without keep file open.

I want to use values of "X" and "Y" to build a line like:

G01 X123.123 Y123.123 for send to gcode subrutine to action motors

and same values to buid a svg file with lines like :

and send back to client the line with 100 continue response .

But response is only G01 not Xvalue and Yvalue

void costel(){
char gcode[]="";
 File f = SPIFFS.open("/test.nc", "r");
int i=0;
      while(f.available()) {
      gcode[i] = f.read();
      i++;
        }  
f.close();
int a=0;
String gcodex ="";
String gcodey ="";
String final ="";

 for (int j=1; j<i; j++){

  if(a==1)gcodex +="a";// but here not obtain response     G01 aaaaa bbbbb
  if(a==2)gcodey +="b";

    //if(a=1) gcodex +=char(gcode[j]);
    //if(a=2) gcodey +=char(gcode[j]);

  

  if(gcode[j]=='X'){a=1;gcodex ="";}//if here put gcodex ="same"
  if(gcode[j]=='Y'){a=2;gcodey ="";}//if here put gcodex ="same1"
                               //response is       G01 same same1
   if(gcode[j]='\n'){
    a=0;
   
    final="G01 "+gcodex+" "+gcodey;
    server.send(100, "text/html",final);
    gcodex="";
    gcodey="";
   
    }
    
 }
  server.send(200);
}

I attend to return G01 aaa bbb ..... but not!

i dont understand because this!
Thanks in advance!

When you do char gcode[]="";you have allocated 1 byte (the null character ‘\0’) in memory, pointed by gcode

When then you map your file into memory with

while(f.available()) {
      gcode[i] = f.read();
      i++;
        }

you basically write in random location and overwrite other stuff . From there on you are toast

I don’t know why you need to bring the file in memory since the weird stuff you do afterwards is just on checking one byte at a given position - so you could do the parsing while reading the file in SPIFFS

We would recommend not to use the String class in general and remember your SRAM is probably not huge

When you do this

char gcode[]="";

you should allocate enough space for the longest string that you will use.

IMHO it would also be better to allocate that as a global variable or as a static local variable so it won't continually be created and abandoned.

Also, there is probably no need to create the whole message as a single string if it just for the purpose of sending it somewhere. As far as the recipient is concerned there is no difference between

Serial.print("abc");

and

Serial.print('a');
Serial.print('b');
Serial.print('c');

...R

so it won't continually be created and abandoned.

As such it’s just on the stack so costs nothing to create and nothing to delete. Any reference to part of this buffer will just be an offset relative to the stack pointer, it’s done at compile time. If the file parsing is done only once it’s probably better that way (well the correct way) so as to not lock scarce SRAM for something that is essentially temporary.

But as said above, OP can probably do without a buffer

J-M-L:
so as to not lock scarce SRAM for something that is essentially temporary.

The Arduino is not a multi-program system so there is nothing that can use that SRAM for another purpose.

If the OP creates the array as a global variable the compiler will tell him/her how much SRAM is used.

...R

Well the compiler does not report on stack or heap usage - can't predict recursive calls or dynamic memory allocation if you use that.. so I still think it's a good idea - if you don't need something for the long run to not lock the memory and there is no performance penalty attached here

Now if you have to often read the file and need the buffer then it's q different story as a statically allocated buffer will guarantee the memory is there when you need it