Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bit of help with strings??

Options
  • 19-05-2005 7:43pm
    #1
    Closed Accounts Posts: 139 ✭✭


    Hello all, new user to c programming and am having difficulty with a little program I am trying to write. Basically what I have is a file which contains a list of words i.e:

    tst.txt:

    customer
    customers
    data
    deactivate
    deactivated
    department
    department
    dept.

    etc....

    I want to read in each word from the file Individually, and assign each individual word to an individual element in an array.
    the code I have so far is:

    #include <stdio.h>
    #include <string.h>

    main()
    {
    int c;
    FILE *in_file;
    FILE *out_file;
    FILE *out;

    in_file = fopen ("thes1.txt", "r");

    if( in_file == NULL )
    printf("Cannot open %s for reading.\n");
    else
    out_file = fopen ("tst.txt", "w");
    if( out_file == NULL )
    printf("Can't open %s for writing.\n");
    else {
    while( (c = getc( in_file)) != EOF )
    {
    putc (c, out_file);
    }

    putc (c, out_file); /* copy EOF */
    printf("File has been copied.\n");

    }
    fclose (in_file);
    fclose (out_file);

    out = fopen("tst.txt","r");

    int zz=0;
    char key2[20];

    while(!feof(out))
    {
    fgets(key2,20,out);
    printf("%s\n",key2);

    }

    }

    this code will get each word Individually, but so far I have not been able to assign words to elements in an array, continiously getting errors about invalid conversions from char*. Any help would be greatly appreciated.
    Kind Regards


Comments

  • Registered Users Posts: 4,287 ✭✭✭NotMe


    What are you trying to convert to? Do you have an array of char*?


  • Registered Users Posts: 131 ✭✭theexis


    Basically you should store a copy of key2 into an array:
    char* words[maximum expected number of words];
    int index = 0;
    
    while(!feof(out))
    {
        fgets(key2,20,out);
        printf("%s\n",key2);
        words[index++] = strdup(key2);
    }
    

    This is likely too simplified though since you'd probably need to dynamically allocate the array assuming you don't know how many words there will be.

    Also, you'll need to clean up after you finish with the array to avoid memory leaks:
    for (; 0 <= index; --index)
    {
        free(words[index]);
    }
    

    Would you consider C++ though - STL would make this a lot cleaner.


  • Closed Accounts Posts: 139 ✭✭john_g83


    thanks for the replies.

    theexis that snippet of code you gave me did exactly what I wanted, thanks a million. May be back soon with more stupid questions.
    Regards
    John


Advertisement