strcpy problems, among others

This is a discussion on strcpy problems, among others within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program to act as a spellchecker. I have dictionary file (attached) which i'm trying to ...

  1. #1
    Registered User
    Join Date
    Mar 2004

    strcpy problems, among others

    I'm trying to write a program to act as a spellchecker. I have dictionary file (attached) which i'm trying to scan into an array to be stored, and then scan through another file and compare these words to the array. At the moment, i'm stuck on copying the buffer array i have with the dictionary words in into the dictionary array. I get segmentation faults (using unix). The only thing i can think is wrong is taht i think the program is scanning in \n at teh end of each word, but i'm not sure. The code is below...
    Any ideas on how to get this working would be great, and quick responses if pos as this is in tomorrow! (i got held up on another prog involving matrices!).

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*void de_allocate(char* dict_store, int dict_words)
            int i;
            for (i=0; i<=dict_words; i++)
    int main(void)
            char buffer[100];
            char* dict_store;
            int dict_words=0, word_length,i;
            FILE* in_file;
            printf("File open failed\n");
            if(fgets(buffer,100,in_file)==NULL) break;
                    printf("test 1\n");
                    word_length=strlen(buffer);printf("test 2\n");
                    dict_store = (char*)malloc(word_length*sizeof(char));printf("test 3\n");
                    for(i=0;i<=dict_words; i++)printf("test 4, dict_words=%d,i=%d\n",dict_words,i);
                            strcpy(dict_store[dict_words],buffer);printf("test 5\n");
                            printf("%s\n",dict_store[dict_words]);printf("test 6\n");
            return 0;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Don't forget that fgets stores the trailing newline (if present) in the buffer.

    >dict_store = (char*)malloc(word_length*sizeof(char));
    Too complex, and it hides a problem if someone isn't reading carefully:
    dict_store = malloc ( word_length + 1 );
    sizeof ( char ) is guaranteed to be 1, so there's no point in including that part of the expression. An exception would be to silence a warning about mismatched signedness. In that case sizeof would force the expression to be size_t. The cast is not required, and can easily hide the error of not including stdlib.h. The problem you had (which will usually cause a seg fault) was to forget room for the trailing nul character. Remember that strlen returns the length of the string, minus the '\0' at the end.

    I think that by scrunching multiple statements onto a single line you're confusing yourself. You have what can very easily be seen as logic errors through bad formatting. Put each statement on its own line and see if the program still appears to do what you think it does.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Deja Vu
    Even down to the IP addresses of the poster's
    Code looks pretty similar as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2004
    yeah that was my friend, i gave him the problem cause i had my matrix program to work on, then it turned out i managed to do a little bit more of a working code that him i think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat_s problems
    By Aisthesis in forum C++ Programming
    Replies: 18
    Last Post: 06-02-2009, 09:59 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 11:34 AM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 10:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21