简体   繁体   中英

C: How to store strings into a “char *myArray[100]” array?

I created this piece of code in C to read a text file line by line and store each line into a position of an array:

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

static const int MAX_NUMBER_OF_LINES = 100000;
static const char filename[] = "file.csv";

int main ( void )
{

  // Read file and fill the Array
  char *myArray[MAX_NUMBER_OF_LINES];
  int numberLine = 0;
  FILE *file = fopen (filename, "r");
  if (file != NULL)
  {
      char line [128];
      while (fgets (line, sizeof line, file) != NULL)
      {
          myArray[numberLine] = line;
          numberLine++;    
      }
      fclose (file);
  }

  // Print the Array
  for (int i = 0; i<numberLine; i++)
  {
    printf("%d|%s", i, myArray[i]);
  }
}

But when printing the array, it's empty. What am I doing wrong?

Because you'll need to copy the lines into buffers in your array.

You need to allocate space for a string in each element of the array, then use something like strncpy to move each line into each myArray slot.

In your current code, you're only copying the same reference - to your line buffer - into each array slot, so at the end, each slot of myArray should point to the same string in memory.

As per Shoaib's suggestion, strdup will save a step if it's available, so try:

myArray[i] = strdup(line);

No error handling there, see the docs for strncpy and strdup .

Alternately you could just add a dimension to myArray :

char myArray[MAX_NUMBER_OF_LINES][100];

You need to really allocate space for all strings but not only for pointers to nonexistent strings. In your example all of your allocated pointers point to variable, that is out of scope.

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

static const int MAX_NUMBER_OF_LINES = 100000;
static const int MAX_LINE_LENGTH = 127;
static const char filename[] = "file.csv";

int main ( void )
{

  // Read file and fill the Array
  char myArray[MAX_NUMBER_OF_LINES][MAX_LINE_LENGTH + 1 /* for 0 byte at end of string */];
  int numberLine = 0;
  FILE *file = fopen (filename, "r");
  if (file != NULL)
  {
      while (fgets (myArray[numberLine], MAX_LINE_LENGTH + 1, file) != NULL)
      {
          numberLine++;    
      }
      fclose (file);
  }

  // Print the Array
  for (int i = 0; i<numberLine; i++)
  {
    printf("%d|%s", i, myArray[i]);
  }
}

array of char* points to line chararray, but you should save each line in an array and point myArray[numberLine] to it:


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

static const int MAX_NUMBER_OF_LINES = 100000;
static const char filename[] = "file.csv";

int main ( void )
{

  // Read file and fill the Array
  char *myArray[MAX_NUMBER_OF_LINES];
  int numberLine = 0;
  FILE *file = fopen (filename, "r");
  if (file != NULL)
  {
      char line [128];
      while (fgets (line, sizeof line, file) != NULL)
      {
          //refinement is hre
          myArray[numberLine] = (char*)malloc(sizeof line);
          strcpy(myArray[numberLine], line);
          numberLine++;    
      }
      fclose (file);
  }

  // Print the Array
  for (int i = 0; i<numberLine; i++)
  {
    printf("%d|%s", i, myArray[i]);
    free(myArray[i])

  }
}

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