2

I wanna store an array of Strings and display it like this

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

int main () {

    int i = 0;
    char* array[200000];
    char prod [10]; 

    FILE * fp = fopen ("arrayValues.txt", "r");

    while (fgets(prod, 10, fp) != NULL) {

        array[i] = strtok(prod, "\n\r");
        i++;

    }

    fclose(fp); 


    for (i = 0; array[i] ; i++) {

        printf("%s  %d\n", array[i], i);

    }



}

but the output is only the last line of the file im working with x times. Suggestions?

13
  • 2
    Note there is no conventional line-ending format that uses \n\r. Windows uses \r\n and *nix use \n Commented Feb 19, 2019 at 22:44
  • How does arrayvalues.txt look like? I assume you are trying to load each line in the array. Commented Feb 19, 2019 at 22:46
  • You're overwriting prod every time you read a line, not making a copy. And strtok() just returns a pointer into the string. So all your array elements point to that same string that's being overwritten. Commented Feb 19, 2019 at 22:47
  • 1
    strtok is going to return a pointer to prod and modify the contents of prod. you are always storing prod to array[i]. you are also modifying the contents of prod every time inside your while loop via fgets. thus all the prints will be the same. also, you don't terminate your last array value with NULL to indicate the end. Commented Feb 19, 2019 at 22:47
  • 1
    Try strdup(strtok(...)) And initialize the array like char* array[200000] = {0} Commented Feb 19, 2019 at 22:55

1 Answer 1

1

Using your coding style try this (please adjust the hard coded values to your needs)

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

int main () {

    int i = 0, j = 0;
    char array[200000][11];
    char prod [10]; 
    char *ptr;

    memset(array, 0, sizeof(array));

    FILE * fp = fopen ("arrayValues.txt", "r");

    while (fgets(prod, 10, fp) != NULL) {

        ptr = strtok(prod, "\n\r");
        snprintf(array[i], sizeof(array[i]) , ptr);
        printf("%s\n", array[i]);
        i++;

    }

    fclose(fp); 

    printf("============\n");

    for (j = 0; j < i; j++){ 
        printf("%s  %d\n", array[j], j);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

man page says bzero is deprecated. The bzero() function is deprecated (marked as LEGACY in POSIX.1-2001); use memset(3) in new programs.
Is using the 2D array more efficient then using an 1D pointer array? Your version is very good, but I wanted to escape the 2D array version.
char array[200000][11]; may blow the stack on some OS's and memory models.
@DavidC.Rankin True, but so can char* array[200000]

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.