Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中读取包含空格的字符串_C_String - Fatal编程技术网

如何在C语言中读取包含空格的字符串

如何在C语言中读取包含空格的字符串,c,string,C,String,在C语言中,当字符串包含单词之间的空格时,从键盘读取字符串的最准确方法是什么?当我使用scanf时,它不会读取带空格的字符串。第二个选项是使用get,但它应该是有害的(我也想知道为什么?)。另一件事是,我不想使用任何文件处理概念,如fgets @user3623265 请查找使用fgets从标准输入读取字符串的示例程序 关于如何使用fgets从键盘获取字符串以及stdin的用途,请参考一些示例C文档 #include <stdio.h> #include <string.h&g

在C语言中,当字符串包含单词之间的空格时,从键盘读取字符串的最准确方法是什么?当我使用scanf时,它不会读取带空格的字符串。第二个选项是使用get,但它应该是有害的(我也想知道为什么?)。另一件事是,我不想使用任何文件处理概念,如fgets

@user3623265

请查找使用fgets从标准输入读取字符串的示例程序

关于如何使用fgets从键盘获取字符串以及stdin的用途,请参考一些示例C文档

#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

i = strlen(str) - 1;
if (str[i] == '\n')
    str[i] = '\0';

printf("This is your string: %s", str);
return 0;
}
#包括
#包括
内部主(空)
{
char-str[80];
int i;
printf(“输入字符串:”);
fgets(str,sizeof(str),stdin);
i=strlen(str)-1;
如果(str[i]='\n')
str[i]='\0';
printf(“这是您的字符串:%s”,str);
返回0;
}
@user3623265

请查找使用fgets从标准输入读取字符串的示例程序

关于如何使用fgets从键盘获取字符串以及stdin的用途,请参考一些示例C文档

#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
int i;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

i = strlen(str) - 1;
if (str[i] == '\n')
    str[i] = '\0';

printf("This is your string: %s", str);
return 0;
}
#包括
#包括
内部主(空)
{
char-str[80];
int i;
printf(“输入字符串:”);
fgets(str,sizeof(str),stdin);
i=strlen(str)-1;
如果(str[i]='\n')
str[i]='\0';
printf(“这是您的字符串:%s”,str);
返回0;
}

这是两种读取包含不使用
get
fgets

  • 您可以使用
    getline
    (您的系统中可能不存在POSIX 2008),它可以方便地管理缓冲区的分配,并具有足够的大小来捕获整个行

    char *line = NULL;
    size_t bufsize = 0;
    size_t n_read; // number of characters read including delimiter
    while ((n_read = getline(&line, &bufsize, stdin)) > 1 && line != NULL) {
        // do something with line
    }
    
  • 如果您绝对想要
    scanf
    ,在本例中,它会一直读取到行尾,除非该行的字符数大于指定的分隔符字符数减1。在后一种情况下,行被截断,您将在下一次
    scanf
    调用中获得剩余的字符

    char line[1024];
    while (scanf("%1023[^\n]\n", line) == 1) {
        // do something with line
    }
    

  • 我还应该指出,例如,当您使用
    scanf
    从键盘读取字符串时,您实际上是在使用文件指针
    stdin
    从文件读取字符串。因此,您无法真正避免“任何文件处理概念”

    这是两种读取包含不使用
    get
    fgets
    的空格的字符串的方法

  • 您可以使用
    getline
    (您的系统中可能不存在POSIX 2008),它可以方便地管理缓冲区的分配,并具有足够的大小来捕获整个行

    char *line = NULL;
    size_t bufsize = 0;
    size_t n_read; // number of characters read including delimiter
    while ((n_read = getline(&line, &bufsize, stdin)) > 1 && line != NULL) {
        // do something with line
    }
    
  • 如果您绝对想要
    scanf
    ,在本例中,它会一直读取到行尾,除非该行的字符数大于指定的分隔符字符数减1。在后一种情况下,行被截断,您将在下一次
    scanf
    调用中获得剩余的字符

    char line[1024];
    while (scanf("%1023[^\n]\n", line) == 1) {
        // do something with line
    }
    

  • 我还应该指出,例如,当您使用
    scanf
    从键盘读取字符串时,您实际上是在使用文件指针
    stdin
    从文件读取字符串。因此,您无法真正避免“任何文件处理概念”

    如果您坚持在作用域中没有文件*,请使用getchar()

    charbuff[1024];
    int-ch;
    int i=0;
    而((ch=getchar())!='\n')
    如果(i<1023)
    buff[i++]=ch;
    buff[i]=0;
    /*现在将字符串移动到一个较小的缓冲区中*/
    

    然而,一般认为标准输出、标准输入和文件*是可用的。您的要求有点奇怪,因为您显然不是一个高级C程序员,不寻常地需要抑制FILE*符号,我怀疑您对C IO的理解是不可靠的。

    如果您坚持在作用域中没有FILE*,请使用getchar()

    charbuff[1024];
    int-ch;
    int i=0;
    而((ch=getchar())!='\n')
    如果(i<1023)
    buff[i++]=ch;
    buff[i]=0;
    /*现在将字符串移动到一个较小的缓冲区中*/
    

    然而,一般认为标准输出、标准输入和文件*是可用的。你的要求有点奇怪,因为你显然不是一个高级C程序员,特别需要抑制FILE*符号,我怀疑你对C IO的理解是不可靠的。

    还有第三种选择,你可以使用
    read()
    调用从
    stdin
    读取原始数据:

    #include <unistd.h>
    
    int main(void) {
        char buf[1024];
        ssize_t n_bytes_read;
    
        n_bytes_read = read(STDIN_FILENO, buf, sizeof(buf) - 1);
        if (n_bytes_read < 0) {
            // error occured
        }
        buf[n_bytes_read] = '\0'; // terminte string
    
        printf("\'%s\'", buf);
    
        return 0;
    }
    

    作为输出。试试。

    还有第三个选项,您可以使用
    read()
    调用从
    stdin
    读取原始数据:

    #include <unistd.h>
    
    int main(void) {
        char buf[1024];
        ssize_t n_bytes_read;
    
        n_bytes_read = read(STDIN_FILENO, buf, sizeof(buf) - 1);
        if (n_bytes_read < 0) {
            // error occured
        }
        buf[n_bytes_read] = '\0'; // terminte string
    
        printf("\'%s\'", buf);
    
        return 0;
    }
    

    作为输出。试试。

    字符串是如何输入系统的?由用户提供?从文件中?来自TTY?通过插座?这太宽泛了…您可能需要
    fgets
    。为什么不“使用任何类似fgets的文件处理概念”?你知道吗?关于
    获取
    :阅读1。如果您不想使用
    fgets
    ,您将不得不重新实现它,效果很差。2.C语言中没有键盘。字符串是如何输入系统的?由用户提供?从文件中?来自TTY?通过插座?这太宽泛了…您可能需要
    fgets
    。为什么不“使用任何类似fgets的文件处理概念”?你知道吗?关于
    获取
    :阅读1。如果您不想使用
    fgets
    ,您将不得不重新实现它,效果很差。2.C语言中没有键盘。如果我在您的提示下立即按
    ^D
    ,会发生什么?(我在一个类似Linux的系统上,所以“你得到一个包含
    ^D
    ”的字符串”不是一个有效的答案。)我给了他一个关于如何从键盘读取而不是使用scanf()和get函数的基本示例。基本上有多种方法可以避免显示EOF(Ctrl+D)。问题是关于使用scanf()或get或其他函数的有效性。因此,在这两个选项中,我的观点/我更喜欢fgets()。如果我所表达/解释的是错误的,那么请纠正我,因为我愿意在这里学习。这就是我加入这个社区的原因。我提到它是因为它将导致一个长度为0的输入字符串和
    strlen(str)-