c - 如何使用scanf()和while循环将字符串从stdin读入c编程中的二维数组?
问题描述
我正在编写 ac 代码以使用 scanf() 和 while 循环从 stdin 读取字符串(进入二维 char 数组)。我的策略是使用输入数组来临时存储每个字符串,然后将其分配给前字数组(固定大小)。但是,我的策略失败了,存储在我的数组中的所有字符串都是相同的(最后一个字符串输入)。如何解决?
我使用了 fgets() 并且它可以找到。但是,我不能用它来处理新的字符串行(来自标准输入)。我的 fgets() 只读取第一行,这就是我转向 scanf 和 while 循环的原因。
#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
#define size 50
int main ()
{
int count = 0;
char input[size];
char * preword[MAX];
while (scanf("%s",input)!= EOF){
preword[count] = input;
printf("preword[%d] is %s\n",count,preword[count]);
count++;
}
printf("the count is %d\n",count);
for (int i = 0; i < count; i++){
printf("preword[%d] is %s\n",i,preword[i]);
}
return 0;
}
我希望来自 stdin 的输入数组将存储在二维 char 数组中。以下是编译后终端中的输出。我的输入是一个 txt 文件,其中我有
hello world
I am a hero
事实证明,存储在二维数组中的所有字符串都是最后一个单词。
preword[0] is hello
preword[1] is world
preword[2] is I
preword[3] is am
preword[4] is a
preword[5] is hero
the count is 6
preword[0] is hero
preword[1] is hero
preword[2] is hero
preword[3] is hero
preword[4] is hero
preword[5] is hero
解决方案
首先在这里
char * preword[MAX];
preword
是字符指针数组,即每个元素都是一个字符指针&当你这样做时
preword[count] = input;
正如@paddyinput
在的每个元素中指出它的副本preword
并且它是同一个指针,因为您没有为 分配内存preword[count]
,正确的方法是为每个指针分配内存然后复制。
也用这里fgets()
代替scanf()
。例如
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
#define size 50
int main (void)
{
int count = 0;
char input[size] = {0};
char * preword[MAX] = {0};
size_t retStrCspn = 0;
while (fgets(input, size, stdin) != NULL){
/* remove trailing new line if its stored at end of buffer by fgets() */
input[retStrCspn = strcspn(input, "\n")] = 0; /* remove the trailing & use the return value for allocating memory purpose \n */
preword[count] = malloc(retStrCspn + 1); /* Allocate memory for each pointer elements */
if(preword[count] != NULL) {
memcpy (preword[count], input, retStrCspn + 1); /* copy input buffer into each different memory location */
printf("preword[%d] is %s\n",count,preword[count]);
count++;
}
else {
/* @TODO malloc erro handling */
}
}
printf("the count is %d\n",count);
for (int i = 0; i < count && preword[i] != NULL; i++){
printf("preword[%d] is %s\n",i,preword[i]);
free(preword[count]); /* free dynamically allocated memory here*/
}
return 0;
}
推荐阅读
- javascript - 如何使用javascript动态更改导航栏中的活动类
- oracle - 等待 Oracle DB 服务器通过 SSL 的响应时未调用 Netty channelRead
- java - AEM Asset API:根据需要创建中间目录
- node.js - 在 AWS EC2 上设置 socket.io 服务器
- ios - 无法在 Mac OS Catalina 上安装 fastlane
- html - 什么是在 mongodb 中正确存储 html 文件
- linear-programming - 这种行为是否表明我有记忆问题?
- jsf - 如何修复“UT010050:过滤器映射 SERVLET 中使用的过滤器 Richfaces - 未找到 Faces Servlet”
- c++ - 与碰撞检测作斗争。如何正确检测碰撞?
- c# - TPL和TAP之间的区别