【发布时间】:2021-11-28 19:12:45
【问题描述】:
我正在编写一个循环遍历具有两列的文本文件的程序。第一个值是字符串,第二个是整数。我正在尝试根据它们的列将它们放入数组中。
数据示例:
斯蒂芬 170
谢恩 150
杰克 180
我正在尝试这样做:
[“斯蒂芬”、“肖恩”、“杰克”]
[170,150,180]
由于某种原因,strcpy 函数不起作用。我没有收到错误消息,但是当我使用 strcpy 并尝试打印字符串数组的第一个值时,没有任何反应。
#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;
}
【问题讨论】:
-
你读到
word,然后将strcpy(destination, source)读到word- 投票结束是一个错字。