我正在制作一个可以打印出用户键入内容的程序,它需要使用方法read_line()
(用于家庭作业)完成,所以我不能做太多改变。
我不明白为什么它没有打印出用户输入的内容。
#include <stdlib.h>
char *read_line(char *buf, size_t sz) {
char tempBuf[sz];
char c;
int pos = 0;
printf("> ");
while(1) {
c = getchar();
if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') {
buf = tempBuf;
return buf;
} else {
tempBuf[pos] = c;
}
pos++;
}
}
int main(int argc, char **argv) {
char *buf;
char *input = read_line(buf, 128);
printf("Here: %s", input);
}
我是C的新手,我感到非常困惑,所以请以简单的方式解释所有内容。任何帮助将不胜感激。
char *buf;
char *input = read_line(buf, 128);
您的第一行将创建一个名为buf
的指针变量,但不会为其分配任何特定值。您的第二行将buf
的值传递给read_line
-但您从未为其分配任何特定值。因此,您将垃圾传递给read_line
,并告诉它使用该垃圾作为缓冲区。
您可能想要char buf[128];
而不是char *buf;
,但这很难说。
[另外,请参阅我的评论,关于您的read_line
函数以某种方式被破坏,表明无论编写它的人都不懂如何使用getchar
。
程序有一些问题。
在main()
中,您初始化了buf
,但从未为其分配任何内存。您可以使用malloc
:
char *buf = malloc(128);
char *input = read_line(buf, 128);
在read_line()
中,您初始化了tempBuf
,但是在初始化元素之前先从中读取元素。 tempBuf
保存用于比较的垃圾值。您初始化元素after比较,这是不正确的。还有其他问题。
这里是修改后的版本:
char *read_line(char *buf, size_t sz) {
char *tempBuf = malloc(sz);
char c;
int pos = 0;
printf("> ");
while(1) {
c = getchar();
if (c == '\n') {
tempBuf[pos] = '\0';
buf = tempBuf;
return buf;
} else {
tempBuf[pos] = c;
}
pos++;
}
}
C中任何字符串的最后一个字符必须为空字符('\ 0')。
提示:您还应该添加pos
的支票,因此它不会超过sz
的值。
您有很多错误。范围外使用的局部变量,而不是终止的字符串等。在这里,您可以使用示例https://godbolt.org/z/_3ZHUJ
#include <stdlib.h>
#include <stdio.h>
char *read_line(size_t sz)
{
char *tempBuf = malloc(sz);
char c;
int pos = 0;
printf("> ");
while(1) {
c = getchar();
tempBuf[pos] = c;
if (tempBuf[pos] == EOF || tempBuf[pos] == '\n')
{
tempBuf[pos] = 0;
return tempBuf;
}
pos++;
}
}
int main(int argc, char **argv) {
char *buf;
char *input = read_line( 128);
printf("Here: %s", input);
}
尝试这个:
#include<stdio.h> //originally missing - needed for I/O
#include<string.h> //for memset
void read_line(char *buf, size_t sz)
{
char c;
int pos = 0;
printf("> ");
while(pos<sz)
{
c = getchar();
if (c == EOF || c == '\n') //originally tempBuf[pos] was used which contained garbage value
{
break;
}
else
{
buf[pos] = c;
}
pos++;
}
}
int main(int argc, char **argv)
{
char buf[128]; //buf is now an array instead of char*
memset(buf,'\0',128);
read_line(buf, 128);
printf("Here: %s", buf);
}
此代码中的错误已注释。
read_line
的当前实现非常有缺陷,原因很简单,因为它返回一个本地声明的缓冲区(该缓冲区在函数末尾被删除)。结果,您的指针指向了垃圾值(这很危险,我再说一遍,因为这将导致您的程序崩溃甚至更糟[即使使用它不拥有的内存,也要继续运行))]
[您应该要做的是在堆上动态创建缓冲区(这样可以安全地返回它-但必须手动将其删除)。
因此,您的函数(以及代码的其余部分)应更改为以下内容:
#include <stdlib.h>
char *read_line(size_t sz) {
// create a new buffer on the heap - so it can easily be returned (note this will have to be deleted after use using free()).
char *tempBuf = malloc(sz);
char c;
int pos = 0;
// set all elements of tempBuf to nulls
memset(arr, 0, sizeof(tempBuf));
printf("> ");
while(1) {
c = getchar();
if (tempBuf[pos] == EOF || tempBuf[pos] == '\n') {
return tempBuf;
} else {
tempBuf[pos] = c;
}
pos++;
}
}
int main(int argc, char **argv) {
char *input = read_line(128);
printf("Here: %s", input);
// free the memory since we are now done with it.
free(input);
}
知道要回答您的问题...