[英]Getting unlimited input in C?
所以我正在尝试编写一个函数,允许用户输入无限数量的字符。 例如这个:
char string[100]
将输入限制为 100 个字符。
我到目前为止的代码是:
#include<stdio.h>
char* uinput(){
char *string, *current;
int counter = 0;
string = (char *) malloc(10 * sizeof(char));
do{
realloc(string, counter * sizeof(char));
current = string + counter;
*current = getchar();
counter++;
}while(*current != '\n');
return string;
}
int main(){
char *s;
s = uinput();
printf("\nYou entered: %s", *s);
return 0;
}
我是指针的新手,所以我不确定为什么这不起作用(程序崩溃)。 我想要做的是继续读取一个字符并继续重新定位字符串指针,以便字节数不断增加,直到用户按下 Enter ('\\n')。
谢谢~拉夫
好的,我认为这是问题所在
你正在重新分配
realloc(string, counter * sizeof(char));
第一次迭代中字符串的大小是多少? 它将是0
。
现在您正在写入一个指针,该指针分配了 0 个字节,因此出现了段错误。
将其更改为while loop
可以帮助修复它。 您还可以更改计数器的初始值来修复它
这种方法是理智的,但有一些小细节是错误的。 如果您在启用警告的情况下进行编译,您会注意到您缺少<stdlib.h>
; 您还将第一个字符提供给printf
而不是指向缓冲区的指针。
然后有一个明显的错误,您的大小被重置为 0,并且您正在转换 malloc 的返回值,使用char
来存储getchar()
的结果,这也是错误的,因为您无法检查EOF
。 您没有保存重新realloc
指针; 并且您没有正确终止字符串。 在次要细节上,您希望将每个realloc
中缓冲区的大小加倍,因为realloc
可能需要复制整行,因此随着行长度的增加,它会随着时间的推移变得越来越慢。
因此我们得到:
#include <stdio.h>
#include <stdlib.h>
char* uinput() {
char *string;
// number of characters in the buffer
size_t counter = 0;
// size of allocated buffer
size_t allocated = 16;
int c;
string = malloc(allocated); // sizeof(char) is 1
do {
c = getchar();
if (c == EOF) {
break;
}
// if our buffer is too small, double the allocation
if (counter + 2 <= allocated) {
size_t new_size = allocated * 2;
char *new_buffer = realloc(string, new_size);
if (! new_buffer) {
// out of memory? try smaller increment
new_size = allocated + 16;
new_buffer = realloc(string, new_size);
if (! new_buffer) {
// really out of memory: free old block
free(string);
return NULL;
}
}
allocated = new_size;
string = new_buffer;
}
// store the character
string[counter++] = c;
} while (c != '\n');
// terminate the buffer properly
string[counter] = 0;
return string;
}
int main() {
char *s = uinput();
if (!s) {
// possibly out of memory in uinput
perror("Error reading input");
exit(EXIT_FAILURE);
}
printf("\nYou entered: %s", s);
free(s);
return EXIT_SUCCESS;
}
使用getchar
、 malloc
和realloc
读取无限输入字符串
声明String类型,也可以使用char *
// String type
typedef char *String;
我写了这个函数来加入字符串末尾的字符
/**
* Join the Char into end of String
*
* @param string - String
* @param c - joined char
*/
void String_joinChar(String *string, const char c)
{
const size_t length = strlen(*string);
(*string) = (String)realloc((*string), sizeof(char) * (length + 2));
(*string)[length] = c;
(*string)[length + 1] = '\0';
}
该函数用于输入字符串,使用getchar
从键盘读取字符并将其连接到当前字符串的末尾。
/**
* Input String
*
* @return Inputed String
*/
String String_input()
{
String string = (String)malloc(sizeof(char));
strcpy(string, "");
char cursor;
fflush(stdin);
while ((cursor = getchar()) != '\n' && cursor != EOF)
{
String_joinChar(&string, cursor);
}
return string;
}
使用char *
, malloc
和realloc
,我们必须释放它
/**
* Destroy String
*
* @param string - Destroyed String
*/
void String_destroy(String string)
{
free(string);
}
现在我们只是使用它!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
String string = String_input();
printf("\n%s\n", string);
String_destroy(string);
return 0;
}
希望对你有用!
您可以尝试以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person{
char *name;
}pers;
void addMem(void);
int main(void){
addMem();
printf("\nYour name is:> %s\n",pers.name);
free(pers.name);
pers.name = NULL;
return 0;
}
void addMem(void){
unsigned int length = 6;
size_t newLength = 0;
unsigned int newSize = 0;
unsigned int i =0;
char *name;
int c;
name = malloc(length);
if(name == NULL){
exit(1);
}
newSize = length;
printf("Enter your name:> ");
while ((c = getchar()) != '\n' && c!=EOF){
name[i++]=(char)c;
if(i == newSize){
newSize = i+length;
name = realloc(name, newSize);
}
}
name[i] = '\0';
newLength = strlen(name)+1;
pers.name = malloc(newLength);
memcpy(pers.name, name, newLength);
free(name);
name = NULL;
}
另一种方法是使用fgets()
,它从输入流中获取一个字符串到一个大小合适的缓冲区中; 如果它有完整的输入,则字符串以\\n
结尾; 如果没有,那么就没有。 所以你可以循环调用 fgets 直到最后有一个 EOL 字符,然后根据你的程序对输入的处理,你可以决定是保持重新分配还是一次处理一点输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.