Home > Back-end >  Assigning string variable to type char * in while loop in c
Assigning string variable to type char * in while loop in c

Time:08-25

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

#define MAX_SIZE 1024
int line_count = 0;

void readFile(FILE *fptr, int *line);

int main(void) {
    FILE *fptr;
    readFile(fptr, &line_count);
    return 0;
}

void readFile(FILE *fptr, int *line) {
    fptr = fopen("C:\\Users\\xxx\\Desktop\\english_words.txt", "r");
    char wordList[MAX_SIZE];
    char *englishWords[MAX_SIZE];
    
    while(fgets(wordList, MAX_SIZE, fptr) != NULL){
        char *token = strtok(wordList, "=");
        englishWords[*line] = token;
        *line  = 1;
    }
    for(int i = 0; i < line_count; i  ){
        //printf("%s",englishWords[i]);
    }
}

I need to insert the data in the token variable one by one into the englishWords array

for example:

englishWords[0] = redudancy

englishWords[1] = overlap

While doing this, I noticed a problem while debugging. The address of the token variable I put in EnglishWords never changes, so I guess all indexes are filled with the last word of the text file.

a screenshot from the debug moment

CodePudding user response:

In the line

englishWords[*line] = token;

you are making englishWords[*line] point inside the array wordList. However, in the next loop iteration, you overwrite the content of wordList. Therefore, you are probably also overwriting what the pointer from the previous loop iteration is pointing to. This is not what you want.

Therefore, you should instead make a copy of the token, which will not be overwritten, and make englishWords[*line] point to that copy.

In order to make a copy of a string, you can use malloc to allocate memory for the copy of the string, and then use strcpy to actually copy it. Some platforms provide the function strdup which combines both of these function calls in one function call.

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

#define MAX_SIZE 1024
int line_count = 0;

void readFile(FILE *fptr, int *line);

int main(void) {
    FILE *fptr;
    readFile(fptr, &line_count);
    return 0;
}

void readFile(FILE *fptr, int *line) {
    fptr = fopen("C:\\Users\\xxx\\Desktop\\english_words.txt", "r");
    char wordList[MAX_SIZE];
    char *englishWords[MAX_SIZE];
    
    while(fgets(wordList, MAX_SIZE, fptr) != NULL){
        char *token = strtok(wordList, "=");

        //allocate memory for the copy of the string
        englishWords[*line] = malloc( strlen(token)   1 );
        if ( englishWords[*line] == NULL )
        {
            fprintf( stderr, "Memory allocation error!\n" );
            exit( EXIT_FAILURE );
        }

        //actually copy the string
        strcpy( englishWords[*line], token );

        *line  = 1;
    }
    for(int i = 0; i < line_count; i  ){
        //printf("%s",englishWords[i]);
    }

    //cleanup

    //free allocated memory
    for ( int i = 0; i < *line; i   )
    {
        free( englishWords[i] );
    }

    //close the file
    fclose( fptr );
}
  •  Tags:  
  • c
  • Related