Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange character replacement where it makes no sense (to me)
    primarykey
    data
    text
    <p>Sorry for the long sections of code, but I am stumped and need a hand!</p> <p>My specific problem is that when I use my parse method in order to call a "del" event, I get that odd character replacement going on in a line that, as far as I can tell, isn't being touched. This does not happen when I directly call the function "del." I've spent far too long poring over this code to no avail, attempting to implement a different way of tokenizing strings; you name it. I included ALL my code because I don't know where the issue is, as I am starting to think it does not lie within the function "parse."</p> <p>I'll hang around and give as much information as is requested, for now I don't know what else to add.</p> <p>P.S. The compiler links against the gnu windows libraries, that's where strsep comes from.</p> <p><a href="http://gnuwin32.sourceforge.net/packages/libgw32c.htm" rel="nofollow" title="LibGW32C for Windows">LibGW32C for Windows</a></p> <p>P.P.S. Only compiler warning is it whining about me not using char* buf</p> <p>db.h</p> <pre><code>#define MAX_ITEMS 80 typedef struct item { long id; char* name; char* desc; float price; } item_t; extern char *strsep (char **restrict stringp, const char *restrict delim); int isLong (char* str); int isFloat (char* str); void add (item_t* item, long id, char* name, char* desc, float price); void del (item_t* item, long id); void modify (item_t* item, long id, char* name, char* desc, float price); void disp (item_t* item, long id); void itemCopy (item_t* from, item_t* to); void parse (item_t* item, char* buf); int findLastElement (item_t* item); </code></pre> <p>db.c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;ctype.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include "db.h" int isLong (char* str) { if (str == NULL) { return 0; } char* pEnd; strtol (str, &amp;pEnd, 10); if (isalpha (*pEnd) || *pEnd == ' ') { return 0; } else { return 1; } } int isFloat (char* str) { if (str == NULL) { return 0; } char* pEnd; strtod (str, &amp;pEnd); if (isalpha (*pEnd) || *pEnd == ' ') { return 0; } else { return 1; } } void add (item_t *item, long id, char* name, char* desc, float price) { int i = 0; while (1) { if (item[i].id == id) { printf ("Item \"%s\" with ID %lu already exists.\n", item[i].name, id); break; } else if (item[i].id == 0) { item[i].id = id; item[i].name = name; item[i].desc = desc; item[i].price = price; break; } else { i++; } } } void del (item_t* item, long id) { int i = 0; int end = findLastElement (item); while (1) { if (item[i].id == id) { item[i].id = 0; item[i].name = ""; item[i].desc = ""; item[i].price = 0; while (i &lt; end) { itemCopy (&amp;item [i + 1], &amp;item [i]); i++; } break; } else { if (i == MAX_ITEMS) { printf ("Item with ID %lu does not exist.\n", id); break; } i++; } } } void modify (item_t *item, long id, char* name, char* desc, float price) { int i = 0; while (1) { if (item[i].id == id) { item[i].name = name; item[i].desc = desc; item[i].price = price; break; } else { if (i == MAX_ITEMS) { printf ("Item with ID %lu does not exist.\n", id); break; } i++; } } } void disp (item_t* item, long id) { int end = findLastElement (item); printf ("\nID\tNAME\tDESCRIPTION\tPRICE\n--\t----\t-----------\t-----\n"); if (id == -1) { for (int i = 0; i &lt; end; i++) { printf ("%lu\t%s\t%s\t$%2.2f\n", item[i].id, item[i].name, item[i].desc, item[i].price); } } else { for (int i = 0; i &lt; end; i++) { if (item[i].id == id) { printf ("%lu\t%s\t%s\t$%2.2f\n", item[i].id, item[i].name, item[i].desc, item[i].price); break; } } } } void itemCopy (item_t* from, item_t* to) { to -&gt; id = from -&gt; id; to -&gt; name = from -&gt; name; to -&gt; desc = from -&gt; desc; to -&gt; price = from -&gt; price; } void parse (item_t* item, char* str) { char **ap, *argv[10], *inputstr = malloc (sizeof(str)), *ptr; strcpy (inputstr, str); memset (argv, 0, sizeof (argv)); for (ap = argv; (*ap = strsep (&amp;inputstr, ",\n")) != NULL;) { if (**ap != '\0') { if (++ap &gt;= &amp;argv[10]) { break; } } } if (strcmp (argv[0], "add\0") == 0) { if (!isLong (argv[1]) || argv[1] == NULL) { printf ("\nInvalid/Missing ID\n"); return; } else if (argv[2] == NULL) { printf ("\nInvalid/Missing Product Name.\n"); return; } else if (argv[3] == NULL) { printf ("\nInvalid/Missing Product Description.\n"); return; } else if (!isFloat (argv[4]) || argv[4] == NULL) { printf ("\nInvalid/Missing Price\n"); return; } else { add (item, strtol (argv[1], &amp;ptr, 10), argv[2], argv[3], strtod (argv[4], &amp;ptr)); } } else if (strcmp (argv[0], "del\0") == 0) { if (!isLong (argv[1]) || argv[1] == NULL) { printf ("\nInvalid/Missing ID\n"); return; } else { del (item, strtol (argv[1], &amp;ptr, 10)); } } } int findLastElement (item_t* item) { for (int i = 0; i &lt; MAX_ITEMS; i++) { if (item[i].id == 0) return i; } return -1; } </code></pre> <p>main.c</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include "db.h" int main (int argc, char* argv[]) { item_t item [MAX_ITEMS]; memset (item, 0, sizeof (item)); char* buf = malloc (255); int exit = 0; while (!exit) { printf ("Adding 3 test items.\n"); add (item, 1234, "Pizza", "Tasty Pizza", 9.99F); add (item, 5678, "Pasta", "Tasty Pasta", 19.99F); add (item, 9012, "Ribs", "Tasty Ribs", 29.99F); disp (item, -1); printf ("\nDeleting Item with ID 5678.\n"); del (item, 5678); disp (item, -1); printf ("\nModifying item with ID 1234.\n"); modify (item, 1234, "Soup", "Tasty Soup", 4.99F); disp (item, -1); printf ("\nAdding another item with id 5678."); add (item, 5678, "Pasta", "Tasty Pasta", 19.99F); printf ("\nAdding item \"Pizza\" with the same ID as \"Soup\".\n"); add (item, 1234, "Pizza", "Tasty Pizza", 9.99F); disp (item, -1); printf ("\nDeleting item \"Soup\" and re-adding \"Pizza\".\n"); del (item, 1234); add (item, 1234, "Pizza", "Tasty Pizza", 9.99F); disp (item, -1); printf ("\nDisplaying only the item with id 5678.\n"); disp (item, 5678); printf ("\nAdding item \"Wings\" using the parsing function.\n"); parse (item, "add,9898,Wings,Tasty Wings,14.99\n"); disp (item, -1); printf ("\nAttempting to Delete non-existent item with ID 9999.\n"); //del (item, 9999); parse (item, "del,9999\n"); &lt;--- This is the problem line printf ("\nAttempting to Modify non-existent item with ID 9999.\n"); modify (item, 9999, "Test", "Test", 0.0F); disp (item, -1); exit = 1; } } </code></pre> <p>And finally, my output:</p> <pre><code>Adding 3 test items. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 1234 Pizza Tasty Pizza $9.99 5678 Pasta Tasty Pasta $19.99 9012 Ribs Tasty Ribs $29.99 Deleting Item with ID 5678. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 1234 Pizza Tasty Pizza $9.99 9012 Ribs Tasty Ribs $29.99 Modifying item with ID 1234. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 1234 Soup Tasty Soup $4.99 9012 Ribs Tasty Ribs $29.99 Adding another item with id 5678. Adding item "Pizza" with the same ID as "Soup". Item "Soup" with ID 1234 already exists. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 1234 Soup Tasty Soup $4.99 9012 Ribs Tasty Ribs $29.99 5678 Pasta Tasty Pasta $19.99 Deleting item "Soup" and re-adding "Pizza". ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 9012 Ribs Tasty Ribs $29.99 5678 Pasta Tasty Pasta $19.99 1234 Pizza Tasty Pizza $9.99 Displaying only the item with id 5678. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 5678 Pasta Tasty Pasta $19.99 Adding item "Wings" using the parsing function. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 9012 Ribs Tasty Ribs $29.99 5678 Pasta Tasty Pasta $19.99 1234 Pizza Tasty Pizza $9.99 9898 Wings Tasty Wings $14.99 Attempting to Delete non-existent item with ID 9999. Item with ID 9999 does not exist. Attempting to Modify non-existent item with ID 9999. Item with ID 9999 does not exist. ID NAME DESCRIPTION PRICE -- ---- ----------- ----- 9012 Ribs Tasty Ribs $29.99 5678 Pasta Tasty Pasta $19.99 1234 Pizza Tasty Pizza $9.99 9898 Win♦ Çdel $14.99 &lt;--- THIS IS WHAT'S DOING MY HEAD IN </code></pre> <p>Thanks for your help in advance!</p> <p>I really hope this doesn't end up being some trivially stupid rookie error.</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.
 

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