0

I'm trying to take data read from a TCP socket with the g_io_channel_read_chars method and convert it into a long integer. I've tried using strtol, atoi, not casting ScanLine as a gchar pointer, accessing the first variable of ScanLine with ScanLine[0], declaring FilterAmount in different ways and despite that, my app still crashes on that line. Any ideas?

static gchar ScanLine[9640];
long int FilterAmount;

g_io_channel_read_chars (source, (gchar *) ScanLine,1,&BytesRead,&GlibError);
if (BytesRead != 1){ 
    return TRUE;
} 

printf("This is my string: %s\n", ScanLine);
FilterAmount = strtol(ScanLine, NULL, 10); 

The output of the printf statement is "2"

Scott James Walter
  • 427
  • 1
  • 6
  • 20

1 Answers1

0

strtol() takes a C string argument: this means the character array must be NULL-terminated. Yours is quite probably not. You must either add a terminator after the last byte you read or parse the digits yourself (since you know when to stop parsing).

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54