简体   繁体   中英

How to read strings from stdin into a two-dimensional array in c programming using scanf() and while loop?

I'm writing ac code to read strings from stdin with scanf() and while loop (into a two-dimensional char array). My strategy is to use an input array to temporarily store each string and then assign it to a preword array (fixed sized). However, my strategy failed and all strings stored in my arrays are the same (the last string input). How to fix it?

I used a fgets() and it works find. However, I cannot use it to deal with a new line of strings (from stdin). My fgets() reads only the first line and that's why I turn to scanf and while loop.

#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
#define size 50


int main ()
{
  int count = 0;
  char input[size];
  char * preword[MAX];
  while (scanf("%s",input)!= EOF){
    preword[count] = input;
    printf("preword[%d] is %s\n",count,preword[count]);
    count++;

  }
  printf("the count is %d\n",count);

  for (int i = 0; i < count; i++){
    printf("preword[%d] is %s\n",i,preword[i]);
  }
  return 0;

}

I expect my input arrays from stdin will be stored in a two-dimensional char array. Below is the output in terminal after compilation. My input is a txt file, in which I have

hello world 
I am a hero

It turns out that all strings stored in the two-d array are the last word.

preword[0] is hello
preword[1] is world
preword[2] is I
preword[3] is am
preword[4] is a
preword[5] is hero
the count is 6
preword[0] is hero
preword[1] is hero
preword[2] is hero
preword[3] is hero
preword[4] is hero
preword[5] is hero

Firstly here

char * preword[MAX];

preword is array of character pointer ie each element is a char pointer & when you are doing like

preword[count] = input;

as @paddy pointed its copies input in every element of preword and it's the same pointer since you haven't allocated memory for preword[count] , correct way is to allocate memory for each pointer and then copy.

Also use fgets() instead of scanf() here. For eg

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
#define size 50
int main (void)
{
  int count = 0;
  char input[size] = {0};
  char * preword[MAX] = {0};
  size_t retStrCspn = 0;
  while (fgets(input, size, stdin) != NULL){
    /* remove trailing new line if its stored at end of buffer by fgets() */
    input[retStrCspn = strcspn(input, "\n")] = 0; /* remove the trailing & use the return value for allocating memory purpose \n */
    preword[count] = malloc(retStrCspn + 1); /* Allocate memory for each pointer elements */
    if(preword[count] != NULL) {
        memcpy (preword[count], input, retStrCspn + 1); /* copy input buffer into each different memory location */
        printf("preword[%d] is %s\n",count,preword[count]);
        count++;
    }
    else {
        /* @TODO malloc erro handling */
    }
  }
  printf("the count is %d\n",count);

  for (int i = 0; i < count && preword[i] != NULL; i++){
    printf("preword[%d] is %s\n",i,preword[i]);
    free(preword[count]); /* free dynamically allocated memory here*/
  }
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM