Note that there are some explanatory texts on larger screens.

plurals
  1. POReading data from a file
    text
    copied!<p>I'm currently getting a run-time check failure #2 - stack around the variable city was corrupted. The input file that is read is formatted like so:</p> <pre> Betty, 12 Main Street, North Elmo, NC 29801, 2000.20 Joe, 16 Maple Blvd., Stumptown, GA, 33125, 4000.40 Frank, 100 Avent Ferry, Raleigh, NC 27606, -3000.30 </pre> <p>How can I fix this error?</p> <pre><code>int main(int argc, char *argv[]) { int c, i, zip; FILE *fp; char name[20], address[50], city[19], state[3]; float balance; for (i = 1; i &lt; argc; i++) { fp = fopen(argv[i], "r"); if (fp == NULL) { fprintf(stderr, "cat: can't open %s\n", argv[i]); continue; } while ((c = getc(fp)) != EOF) { fscanf(fp, "%s%s%s%s%d%f", &amp;name, &amp;address, &amp;city, &amp;state, &amp;zip, &amp;balance); } printf("%s%s%s%s%d%f\n", name, address, city, state, zip, balance); fclose(fp); } return 0; } </code></pre> <h3>Update:</h3> <p>Thanks for all of the help thus far. What I have done now is I have created a <code>struct</code> with each <code>person</code> in the struct containing a name, address, and balance. I just changed the previous statements to pass variables to a <code>struct person p</code> and then created an <code>insert()</code> method that inserts p into a <code>struct person list</code>.</p> <pre><code>while (fgets(line, sizeof(line), fp) != NULL) { p = malloc(sizeof(struct person)); sscanf(line, "%19[^,], %49[^,], %18[^,], %2[^ ] %d, %lf\n", p-&gt;name, p-&gt;address, p-&gt;city, p-&gt;state, &amp;p-&gt;zip, &amp;p-&gt;balance); printf("got [%s], [%s], [%s], [%s], [%d] and [%9.2f].\n", p-&gt;name, p-&gt;address, p-&gt;city, p-&gt;state, p-&gt;zip, p-&gt;balance); insert(p); } </code></pre> <p>I am trying to insert <code>person</code> into the list in alphabetical order and tried doing this by using strcmp. Upon running my program with this method, it hangs and stops execution at the first insert. Looking for help on where I went wrong on my <code>insert()</code> method.</p> <pre><code>void insert(struct person *p) { struct person *cur, *prev; int result = 0; for (cur = list, prev = NULL, result = strcmp(p-&gt;name, cur-&gt;name); cur != NULL &amp;&amp; result &gt; 0; prev = cur, cur = cur-&gt;next, result = strcmp(p-&gt;name, cur-&gt;name)); if (cur != NULL &amp;&amp; result == 0) { printf("-------------------------\n" "DUPLICATE RECORD: %s\n" "-------------------------\n", p-&gt;name); free(p); return; } p-&gt;next = cur; if (prev == NULL) { list = p; } else { prev-&gt;next = p; } } </code></pre> <p>I tested this method out without the <code>strcmp()</code>. I just compared with <code>p-&gt;name &gt; cur-&gt;name</code> and it was able to insert and print out the list just fine with my <code>print()</code> method. The only problem was the persons were not sorted alphabetically, so I went back and tried to implement <code>strcmp()</code>.</p>
 

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