| | |
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 |
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--
--commands.h--
And --commands.c--
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
--Main.c--
C Syntax (Toggle Plain Text)
#include "stdafx.h" #include "commands.h" int main(int argc, char* argv[]) { char word[256]; int id; strcpy(word, "move"); strToUpper(word); printf("%s", word); id = getCommandId(word); if(id == -1) perror("Error"); else if(id == 0) printf("No such command."); else printf("ID: %d\n", getCommandId("move")); getchar(); return 0; }
--commands.h--
C Syntax (Toggle Plain Text)
#pragma once #define COMMAND_MOVE 1 #define COMMAND_TAKE 2 #define COMMAND_LOOK 3 int getCommandId(char *word); void strToUpper(char *str);
And --commands.c--
C Syntax (Toggle Plain Text)
#include "stdafx.h" #include "commands.h" void strToUpper(char *str) { int i = 0; while(str[i] != '\0') { str[i++] &= 0xDF; //reset bit 5 } } int getCommandId(char *word) { FILE *fp; char buf[256]; char *tok; int i = 0; fp = fopen("commands.dat", "r"); if(fp == NULL) return -1; strToUpper(word); while(++i) { if(fgets(buf, 255, fp) == NULL) break; tok = strtok(buf, " "); while(tok != NULL) { if(strcmp(tok, word) == 0) return i; tok = strtok(NULL, " "); } } return 0; }
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.
>>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.
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.
I put your program all in one file and here is the result (using VC ++ 2008 Express)
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.
C Syntax (Toggle Plain Text)
#include <ctype.h> #pragma warning(disable: 4996) void strToUpper(char *str) { int i = 0; while(str[i] != '\0') { str[i] = toupper(str[i]); //reset bit 5 ++i; } } int getCommandId(char *word) { FILE *fp; char buf[256]; char *tok; int i = 0; fp = fopen("commands.txt", "r"); if(fp == NULL) return -1; strToUpper(word); i = 0; while(fgets(buf, 255, fp) != NULL) { strToUpper(buf); tok = strtok(buf, " "); ++i; while(tok != NULL) { if(strcmp(tok, word) == 0) return i; tok = strtok(NULL, " "); } } return 0; } int main(int argc, char* argv[]) { char word[256]; int id; strcpy(word, "move"); strToUpper(word); printf("%s", word); id = getCommandId(word); if(id == -1) perror("Error"); else if(id == 0) printf("No such command."); else printf("ID: %d\n", getCommandId("move")); getchar(); return 0; }
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.
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.
>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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Comparing Strings in C# (C#)
- Please help me compare strings (Java)
- reversing two strings (C)
- C++ handling of strings in a boolean expression (C++)
- Compare strings... (C++)
- comparing two strings with linear search.. (Java)
- JSP and Oracle (JSP)
Other Threads in the C Forum
- Previous Thread: Reading words from file
- Next Thread: Problems with array and sscanf
Views: 366 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi







