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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

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

 
0
  #1
Apr 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,647
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Apr 19th, 2009
>>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;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

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

 
0
  #3
Apr 19th, 2009
"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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

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

 
0
  #4
Apr 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,647
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #5
Apr 19th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

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

 
0
  #6
Apr 19th, 2009
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"));
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
1
  #7
Apr 19th, 2009
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

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

 
0
  #8
Apr 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

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

 
0
  #9
Apr 20th, 2009
Oh wow, I forgot about the second call myself. Jepthah: point taken.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 364 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC