Home > database >  strcpy not working inside for loop? c programming
strcpy not working inside for loop? c programming

Time:10-09

I am writing a program that loops through a text file with two columns. the first values are strings and the second are ints. I am trying to put them in arrays based on their column.

Data example:

stephen 170

shane 150

jake 180

Im trying to do this:

["stephen", "shane", "jake"]

[170,150,180]

For some reason the strcpy function is not working. I am not getting an error message but when I use strcpy and attempt to print the first value of the string array, nothing happens.

#include<stdio.h>
#include <string.h>
int main (void)
{
FILE* dict;
char word[50];
int weight;
int weights[50000]; 
char *words[50000];


dict = fopen("dict.txt", "r");
for (int i = 0; i < 50000; i  ) {
    fscanf(dict,"%s  %d", &word, &weight);
    weights[i] = weight;
    strcpy(word, words[i]);
}
printf("%s", words[0]);
printf("%d", weights[0]);
return 0;

}

CodePudding user response:

First note that the parameters of strcpy are in the wrong order: the first is the destination and the second is the source, and I guess you want to copy the word string to word[i], so you need to swap the parameters order.

But this won't work either as word[i] points to garbage memory. You'll have to allocate some. You could use for example strdup instead:

words[i] = strdup(word);

Note that it allocates memory on the heap so don't forget to free it once you finished using it.

CodePudding user response:

You have swapped destination and source in your call to strcpy.

Checking man or using a good IDE showing lib function prototypes should help you to avoid that kind of stupid errors for good.

Also, it's mere luck that the program isn't segfaulting as you are reading from words[i] which is an uninitialized array of pointers to chars, you should add a malloc of words[i] before copying to it.

A minimaly fixed (there are still other problems to fix) version of the program could look like below

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void)
{
FILE* dict;
char word[50];
int weight;
int weights[50000]; 
char *words[50000];


dict = fopen("dict.txt", "r");
for (int i = 0; i < 50000; i  ) {
    fscanf(dict,"%s  %d", &word, &weight);
    weights[i] = weight;
    words[i] = malloc(strlen(word) 1);
    strcpy(words[i], word);
}
printf("%s", words[0]);
printf("%d", weights[0]);
return 0;
}

Another option would be to use strdup rather than the current code because it does both malloc and strcpy.

Other obvious problems are that your program will have troubles if any word is longer than 50 characters and it's not trivial to fix using scanf. You could use something like fscanf(dict,"Is %d", &word, &weight); to avoid overflowing word but if the word is too long that will break the parsing loop. (you will get a line with the beginning of the word and the previous value of weight).

And another issue will happen if your dictionary file has less than 50000 entries.

Let's say that the content of your dictionary file has expected format rather than fixing the code.

  • Related