943,568 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 577
  • C RSS
Apr 19th, 2009
0

You'd think I would know how to use strings by now, but...

Expand Post »
I'm getting an error (unhandled exception writing address so and so) trying to modify a string. It gets weirder though. Let me show what i've got:

--Main.c--
  1. #include "stdafx.h"
  2. #include "commands.h"
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6. char word[256];
  7. int id;
  8.  
  9. strcpy(word, "move");
  10. strToUpper(word);
  11. printf("%s", word);
  12. id = getCommandId(word);
  13.  
  14. if(id == -1)
  15. perror("Error");
  16. else if(id == 0)
  17. printf("No such command.");
  18. else
  19. printf("ID: %d\n", getCommandId("move"));
  20.  
  21. getchar();
  22. return 0;
  23. }

--commands.h--
  1. #pragma once
  2.  
  3. #define COMMAND_MOVE 1
  4. #define COMMAND_TAKE 2
  5. #define COMMAND_LOOK 3
  6.  
  7. int getCommandId(char *word);
  8. void strToUpper(char *str);

And --commands.c--
  1. #include "stdafx.h"
  2. #include "commands.h"
  3.  
  4. void strToUpper(char *str)
  5. {
  6. int i = 0;
  7. while(str[i] != '\0')
  8. {
  9. str[i++] &= 0xDF; //reset bit 5
  10. }
  11. }
  12.  
  13. int getCommandId(char *word)
  14. {
  15. FILE *fp;
  16. char buf[256];
  17. char *tok;
  18. int i = 0;
  19.  
  20. fp = fopen("commands.dat", "r");
  21. if(fp == NULL)
  22. return -1;
  23.  
  24. strToUpper(word);
  25.  
  26. while(++i)
  27. {
  28. if(fgets(buf, 255, fp) == NULL)
  29. break;
  30.  
  31. tok = strtok(buf, " ");
  32. while(tok != NULL)
  33. {
  34. if(strcmp(tok, word) == 0)
  35. return i;
  36.  
  37. tok = strtok(NULL, " ");
  38. }
  39. }
  40.  
  41. return 0;
  42. }

Nothing real exciting happens with the call to strToUpper in main, other than the string being capitalized. Then getCommandId is called, and therefore strToUpper is too, with the same pointer word as before. Only this time, I get the nasty error described at the top of my post. Thoughts?
Last edited by death_oclock; Apr 19th, 2009 at 9:00 pm.
Similar Threads
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

>>str[i++] &= 0xDF; //reset bit 5
Programming is not about trying to be cute, but about writing code that you and everyone else in the world can easily comprehend.
str[i] = toupper(str[i]); ++i;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

"Being cute" was not my intention. I was thinking of efficiency at the time. And while I appreciate the suggestion, it doesn't help my immediate problem.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

Do you still get the error if you remove:
* the file stuff? (replace it with a single buf load to tokenize)
* the token stuff?
Post a single file minimal version that recreates the error.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

I put your program all in one file and here is the result (using VC ++ 2008 Express)
  1. #include <ctype.h>
  2. #pragma warning(disable: 4996)
  3.  
  4.  
  5. void strToUpper(char *str)
  6. {
  7. int i = 0;
  8. while(str[i] != '\0')
  9. {
  10. str[i] = toupper(str[i]); //reset bit 5
  11. ++i;
  12. }
  13. }
  14.  
  15. int getCommandId(char *word)
  16. {
  17. FILE *fp;
  18. char buf[256];
  19. char *tok;
  20. int i = 0;
  21.  
  22. fp = fopen("commands.txt", "r");
  23. if(fp == NULL)
  24. return -1;
  25.  
  26. strToUpper(word);
  27. i = 0;
  28. while(fgets(buf, 255, fp) != NULL)
  29. {
  30. strToUpper(buf);
  31. tok = strtok(buf, " ");
  32. ++i;
  33. while(tok != NULL)
  34. {
  35. if(strcmp(tok, word) == 0)
  36. return i;
  37.  
  38. tok = strtok(NULL, " ");
  39. }
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. int main(int argc, char* argv[])
  46. {
  47. char word[256];
  48. int id;
  49.  
  50. strcpy(word, "move");
  51. strToUpper(word);
  52. printf("%s", word);
  53. id = getCommandId(word);
  54.  
  55. if(id == -1)
  56. perror("Error");
  57. else if(id == 0)
  58. printf("No such command.");
  59. else
  60. printf("ID: %d\n", getCommandId("move"));
  61.  
  62. getchar();
  63. return 0;
  64. }

The main problem with the program is that main() calls getComandId() twice. In the printf() line, instead of calling getCommandId() again (line 60 of the code I posted) just use the return value from the previous call.

getCommandId() also needs to close the file before exiting, which I did NOT correct.
Last edited by Ancient Dragon; Apr 19th, 2009 at 10:16 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

Nice one AD!
I did not spot that call tucked away in there.
getCommandId is being called with a string literal (constant),
which it tries to modify in strToUpper:
getCommandId("move"));
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Apr 19th, 2009
1

Re: You'd think I would know how to use strings by now, but...

The real reason you're having problems with your code is you're trying to modify a string literal:
>printf("ID: %d\n", getCommandId("move"));

You never allocated space for "move", so who knows what strtok() and your strToUpper() function are trying to do with your computer's memory.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Apr 19th, 2009
0

Re: You'd think I would know how to use strings by now, but...

as an aside....

the plan to "reset bit 5" as a method to convert a string to uppercase will make for some fun times if the string ever contains a numeral or other non-alphabetical character.

It is indeed rather "cute".




.
Last edited by jephthah; Apr 19th, 2009 at 11:48 pm.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Apr 20th, 2009
0

Re: You'd think I would know how to use strings by now, but...

Oh wow, I forgot about the second call myself. Jepthah: point taken.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reading words from file
Next Thread in C Forum Timeline: Problems with array and sscanf





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC