Formatting serial.write string to include variable

Hi I am using a GSM shield with my arduino which works by serial AT commands.
What I am having problems with is including a variable in a serial command to send the phonebook position (Sch) to store a number. the Sch is incremented each time a new number is found so I cannot pass it a constant
.the problem line is at the end of the code.

I know I could do this with serial.printf is there something simalar for serial.write?

char number[20];
int Sch=10;//to store amount of numbers in phonebook

void setup() 
{
........
  
};

void loop(){
..........
}

void storeNumbersOnSim(){
  //Chekcs status of call
  stat=gsm.CallStatusWithAuth(number,10,Sch);
  
  
  //if number isnt on sim
  if(stat != CALL_INCOM_VOICE_AUTH){
    Sch=Sch++;//increase phonebook position
    Serial.write("AT+CPBW=,%i,\r",Sch);
     }
}

Thanks very much and sorry if I havent explained this clearly just let me know and I will try and explain a bit clearer.

char temp[20];

sprintf(temp,"AT+CPBW=,%i,\r",Sch);
Serial.write(temp);

Make sure the temp variable is big enough to store the entire formatted string plus the terminating null character. 20 is a guess that should work unless your number are more than 8 characters.

Thank you very much I will give this a go this afternoon.

    Sch=Sch++;//increase phonebook position
Sch++;

is equivalent to

Sch = Sch + 1;

So, your code is equivalent to

Sch = Sch = Sch + 1;

Looks a little silly that way, doesn't it?

PaulS : yes it does, thanks for pointing that out still learning as you can tell.

majenko your way worked great but it still isn't doing what it should i.e. storing the number

when i type the command in serial monitor for testing purpose this does store the number fine

AT+CPBW=10,"07777777777",129,"10"

but when i use

char temp[50];

sprintf(temp,"AT+CPBW=%i,\"%s\",129,\"%s\"\r",Sch, number, number2);
Serial.write(temp);

it seems to print correctly in the serial monitor but it isn't storing the number on the sim (so obviously not being passed correctly) any ideas?

Is your line ending correct? \r is character 13. Another common line ending is \n (character 10), or both together - \r\n.

majenko:
Is your line ending correct? \r is character 13. Another common line ending is \n (character 10), or both together - \r\n.

I think so it works ok for all other commands like getting the gsm shield to send sms so it should be the same for this command.
I will try it with \n to see what happens though thanks again for your help.

Did you ever solve this problem or find a different solution?

To whoever wants to reply, please note that this thread is 7 years old.

The problem still seems relevant though.