0

I am trying to create an Array of words from a text file. I am able to get it to print out the values correctly but I need an array that I can actually work with. After I have this array I have to do various things to the words I have stored, such as counting each ones length. For now I just need help making an array that I can actually work with.

Here is the code:

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


int main ( int argc, char* argv[]){
    // First Read in First novel File
    FILE *fp;
    char *ProgFile;

    // Variables for Parsing
    int i = 0;
    int j=0;
    char *cp;
    char *bp;
    char line[255];
    char *array[5000];
    int x;
    int wordCount=0;
    int wordCountPerNovel;


    // Adjusting the file name to include txt and corresponding number
    strcat(argv[1],"_1.txt");
    ProgFile = argv[1];

    // Open Each File
    fp=fopen(ProgFile,"r");
    if( fp==NULL )printf("error");
    else printf("bin file loaded: '%s'",ProgFile);
    // Now begin analysing
    // Part 1
    // Parse Entire Document into Array of Strings
    while (fgets(line, sizeof(line), fp) != NULL) {
            bp = line;
            while (1) {
                    cp = strtok(bp, ",.!?<97> \n");
                    bp = NULL;
                    if (cp == NULL)break;
                    array[i++] = cp;
                    printf("Check print - word %i:%s:\n",i-1, cp);
            }
    }
    // At this point i is the last word that was iterated, -1 since it breaks out after being added
    // This gets total words of all novels

    wordCount=wordCount+(i-1);
    printf("\nTotal words %i\n",wordCount);
    // Find Total number of letters
    //for (i=1;i<15;i++){
    //      printf("My value: %s \n",finalArrayWord[i]);
    //
    //}
6
  • You didn't actually ask a question. Commented Apr 7, 2017 at 14:59
  • What's an array that I can actually work with? Commented Apr 7, 2017 at 14:59
  • 1) strcat(argv[1],"_1.txt"); Can't this. Commented Apr 7, 2017 at 15:02
  • 1
    You have a problem, when you asign array[i++] = cp you are working with the same memory area. In the next loop you will overwrite when you do fget(line) becase strtok will return you a pointer to line. You have another mistake wen you call for seccond time to strtok, take a look to strtok man page. Commented Apr 7, 2017 at 15:03
  • and @BLUEPIXY is right, you can write after argv, that memory is not yours, you have to do something like snprintf(myNewString,maxlenofMyNewString,"%s_1.txt",argv[1]); I think that you have to learn a bit about using memory and pointers in C. Commented Apr 7, 2017 at 15:06

1 Answer 1

1

The code didn't compile. If you know the length you can use char array and you don't have to use malloc for reading from a file into an array.

file 1.txt

   Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatu

Code

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

void read_words (FILE *f) {
    char x[1024];
    char **a;
    a = malloc(1024 * sizeof(char*));
    for (int i = 0; i < 1024; i++)
        a[i] = malloc((1024+1) * sizeof(char));
    int i = 0;
    while (fscanf(f, " %1023s", x) == 1) {
        strcpy(a[i], x);
        i++;
    }
    for (int j = 0; j < i; j++) {
        printf("%d %s\n", j, a[j]);
    }
}
int main(void){
    read_words(fopen("1.txt", "r"));
    return 0;
}

Test (here is your array)

$ ./a.out 
0 Sed
1 ut
2 perspiciatis
3 unde
4 omnis
5 iste
6 natus
7 error
8 sit
9 voluptatem
10 accusantium
11 doloremque
12 laudantium,
13 totam
14 rem
15 aperiam,
16 eaque
17 ipsa
18 quae
19 ab
20 illo
21 inventore
22 veritatis
23 et
24 quasi
25 architecto
26 beatae
27 vitae
28 dicta
29 sunt
30 explicabo.
31 Nemo
32 enim
33 ipsam
34 voluptatem
35 quia
36 voluptas
37 sit...
Sign up to request clarification or add additional context in comments.

2 Comments

This seg faults.
Loomings Call me Ishmael. Some years ago�never mind how long precisely�having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find mys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.