1

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:

  • Using a two dimensional array with no avail
  • Working with methods and tried to return char and THEN pass it to the array, so I can avoid this sloppy code, but this will do for now.

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;
}

4 Answers 4

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

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().

Comments

1

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).

Comments

1

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.

Comments

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.