动态内存和fgets
所有 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
Hi to all stackoverflow users.
I am trying to build a simple (as an exercise) code that will read from a file and will store the words from a file in an dynamically allocated array. I think I am mallocing wrong. Does anyone see what I am doing wrong?
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您已经分配了一个位置来存储十个字符串指针。但您尚未分配任何空间来将字符复制到持久字符串中。
如果您想在开始时设置该存储,您可以这样做
,或者,可能更好,您可以根据需要分配每个字符串。使用
strdup
而不是strncpy
(这就像执行malloc
然后执行strcpy
):You have allocated a place to store ten string pointers. But you have not allocated any space to copy the characters into persistent strings.
If you want to set up that storage at the beginning, you could do
Or, probably better, you can allocate each string as needed. Instead of
strncpy
, usestrdup
(which is like doing amalloc
then astrcpy
):如果您只需要处理最多 10 行文本,那么我会这样做:
发生的情况是这段代码一次性分配和复制(使用 strdup,而不是 malloc 后跟 strcpy)。
If you only need to handle up to 10 lines of text then I'd do it more like this:
What's happening is that this code allocates and copies in one go (using strdup, rather than malloc followed by strcpy).
对于此任务,我将首先定义一个包含单词的数据结构,如下所示:
然后我将定义一些函数来对它们进行操作。这是为了保持
main
中的代码简短且可读。这些功能是:使用这些功能应该不难完成原来的任务。
For this task I would first define a data structure that holds the words, like this:
Then I would define some functions to operate on them. This is to keep the code in
main
short and readable. The functions are:Using these functions it shouldn't be difficult to complete the original task.
您没有为字符串分配空间,只是为字符串数组分配空间。
myArray[j]
只是一个未初始化的指针。相反,为myArray
中的每个字符串分配空间,如下所示:正如 user411313 指出的,sscanf 不返回匹配的字符数,而是返回匹配的输入项的数量。使用
strnlen
(如果没有strnlen
,则使用strlen
)来获取字符串的大小(并且不要忘记添加 1对于空终止符)。You're not allocating space for your strings, just the array of strings.
myArray[j]
is just an uninitialized pointer. Instead, allocate space for each string inmyArray
like so:As user411313 pointed out, sscanf doesn't return the number of characters matched, but the number of input items matched. Use
strnlen
(orstrlen
if you don't havestrnlen
) to get the size of the string (and don't forget to add 1 for the null terminator).