Note that there are some explanatory texts on larger screens.

plurals
  1. POPunctuation causing errors when reading a string with whitespace using sscanf in C
    primarykey
    data
    text
    <p><strong>Update</strong>: Properly initialising string with <code>char string[sizeof buffer - 1]</code> has solved the crashing problem, but I'm still curious as to what having more than one punctuation mark had to do with it!</p> <p>I am trying to read a string from a file in the form "some text". Using <code>sscanf</code> with the pattern <code>\"%[^\"]\"</code> has worked perfectly for me so far, but as soon as I started adding punctuation to the string the program has started crashing.</p> <p>It seems that the error only occurs if more than one punctuation mark is used, regardless of what punctuation mark it is, or the mark's position. It also occurs regardless of the position of lines with punctuation in the file (ie. even if last line has no punctuation the error still occurs).</p> <p>Anyway, below is the code I have so far:</p> <pre><code>char* func(char* f_name); FILE* file = get_file(f_name,"r"); // a short function I wrote to get the // file pointer from the current // directory. The error is almost // certainly not here. if (file == 0) { print("Unable to load file\nExiting..."); exit(-1); } char* pattern = "\"%[^\"]\""; int read_args = -1; char* string; // string size is unknown until read char buffer[1200]; // i expect very long line of data in the file while ( fgets( buffer, sizeof(buffer), file ) != NULL ) { printf("found line: %s\n",buffer); read_args = sscanf(buffer, pattern, string); printf("num args: %d\n",read_args); printf("read value: %s\n", string); } fclose(file); return string; } </code></pre> <p>The following are some data I have tried. Where marked 'unsuccessful' the program compiles, runs everything, and the crashes just before exiting.</p> <pre><code>"test test test" // successful "test, test test" // successful "test test; test" // successful "test, test, test" // unsuccessful "test; test. test," // unsuccessful </code></pre> <p>I plan to use a more complex pattern if this problem is resolved, and was reading data with the pattern <code>%d \"%[^\"]\" \"%[^\"]\"</code> successfully before this error occurred. Thanks in advance for any answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload