serge
serge

Reputation: 366

pass string in array

I am trying to pass strings (lines of text file) into arrays (array for f1 and array2 for f2). When I just print the buffer buffer2, the lines come up just fine. When I try to pass them using strcpy the program crashes with no apparent reason. I have tried the following:

I am using windows 7 x64, with DEV-C++.

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

int main(int argc, char *argv[]) 
{
    char *arrayF1[20] ;
    char *arrayF2[20] ;
    int i = 0; 
    int size = 1024, pos;
    int c;
    int lineCount = 0;
    char *buffer = (char *)malloc(size);
    char *buffer2 = (char *)malloc(size);
    char *array[100];
    char *array2[100];


    if (argc!=3)
    {
       printf("\nCommand Usage %s filename.txt filename.txt\n", argv[0]); 
    }
    else
    {        
        FILE *f1 = fopen(argv[1], "r");
        FILE *f2 = fopen(argv[2], "r");

        if(f1) 
         {
          do { // read all lines in file
            pos = 0;
            do{ // read one line
              c = fgetc(f1);
              if(c != EOF) buffer[pos++] = (char)c;
              if(pos >= size - 1) { // increase buffer length - leave room for 0
                size *=2;
                buffer = (char*)realloc(buffer, size);
              }
            }while(c != EOF && c != '\n');
            lineCount++;
            buffer[pos] = 0;
            // line is now in buffer 
            strcpy(array[i], buffer);
            printf("%s", array[i]);
            //printf("%s", buffer);
            i++;
          } while(c != EOF); 
          printf("\n");
          fclose(f1);           
        }
        printf("%d\n",lineCount);
        free(buffer);    

        lineCount=0;
        i=0;
        if (f2)
        {
          do { // read all lines in file
            pos = 0;
            do{ // read one line
              c = fgetc(f2);
              if(c != EOF) buffer2[pos++] = (char)c;
              if(pos >= size - 1) { // increase buffer length - leave room for 0
                size *=2;
                buffer2 = (char*)realloc(buffer, size);
              }
            }while(c != EOF && c != '\n');
            lineCount++;
            buffer2[pos] = 0;
            // line is now in buffer 
            strcpy(array2[i], buffer);
            //printf("%s", buffer2);
            printf("%s", array2[i]);
            i++;
          } while(c != EOF); 
          printf("\n");
          fclose(f2);           
        }
        printf("%d\n",lineCount);
        free(buffer2);    
        }//end first else
    return 0;
}

Upvotes: 1

Views: 1162

Answers (4)

Mike
Mike

Reputation: 49463

the program crashes with no apparent reason

There is always a reason :)

This line:

char *array[100];

Creates an array of 100 pointers to characters.

Then this line:

strcpy(array[i], buffer);

Tries to copy your buffer to the ith pointer. The problem is that you never allocated any memory to those pointers, so strcpy() crashes. Just this:

array[i] = malloc(strlen(buffer)+1);
strcpy(array[i], buffer);

will resolve that error.

Upvotes: 1

Kyle Strand
Kyle Strand

Reputation: 16499

Looks to me like you allocated the arrays on the stack but failed to ensure that they'd be big enough, since each has size exactly 100. Since you don't know how big they'll be, you can either allocate them dynamically (using @JohnKugelman's solution) or wait to declare them until after you know what their sizes need to be (i.e., how long the strings are that they need to hold).

Upvotes: 1

Parker Kemp
Parker Kemp

Reputation: 765

To strcpy() to a char*, you need to have already allocated memory for it. You can do this by making static char arrays:

char array[100][50];   //Strings can hold up to 50 chars

or you can use pointers and dynamically allocate them instead.

char *array[100];

for(int i = 0; i < 100; i++)
    array[i] = malloc(sizeof(char) * 50);    //Up to 50 chars

...

for(int i = 0; i < 100; i++)
   free(array[i]);          //Delete when you're finished

After allocating it with one of those methods, it's safe to write to it with strcpy().

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361849

You haven't allocated any memory for the arrays in array. You'll need to do that before you can copy the strings there.

array[i] = malloc(pos + 1);

if (array[i] == NULL) {
    // handle error
}

strcpy(array[i], buffer);
printf("%s", array[i]);

Upvotes: 2

Related Questions