嗨,所有 stackoverflow 用户。我正在尝试构建一个简单的(作为练习)代码,该代码将从文件中读取并将文件中的单词存储在动态分配的数组中。我想我分配错了。有谁看到我做错了什么?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define ARRSIZE 10
int main(){
char * myArray = malloc(ARRSIZE*sizeof(char*));
FILE * p1File;
char mystring1 [100];
char word [100];
int j = 0;
p1File = fopen ("my1file.txt","r");
if (p1File == NULL) perror ("Error opening file");
else{
while(fgets(mystring1, 100, p1File)){
int nuRead = sscanf(mystring1, "%s", word);\
printf("lepo ani magia\n\n");
if (nuRead > 0){
strncpy (*myArray[j], mystring1, 100);
//*myArray[j] = mystring1;
}
j += 1;
}
}
}
//////////////////////////////
my text file is
this
will
probably
work
but
I
am