相关疑难解决方法(0)

为什么"while(!feof(file))"总是错的?

我看到人们最近在很多帖子中试图读取这样的文件.

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    char *path = argc > 1 ? argv[1] : "input.txt";

    FILE *fp = fopen(path, "r");
    if( fp == NULL ) {
        perror(path);
        return EXIT_FAILURE;
    }

    while( !feof(fp) ) {  /* THIS IS WRONG */
        /* Read and process data from file… */
    }
    if( fclose(fp) == 0 ) {
        return EXIT_SUCCESS;
    } else {
        perror(path);
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个__CODE__循环有什么问题?

c file while-loop feof

550
推荐指数
4
解决办法
21万
查看次数

如何读取用户在C中输入的字符串?

我想阅读用户使用C程序输入的名称.

为此,我写道:

char name[20];

printf("Enter name: ");
gets(name);
Run Code Online (Sandbox Code Playgroud)

但是使用gets不好,那么更好的方法是什么?

c stdin

47
推荐指数
3
解决办法
18万
查看次数

如何从stdin读取一行,阻塞直到找到换行符?

我试图从命令行的stdin一次读取任意长度的一行.我不确定我是否能够包含GNU readline并且更喜欢使用库函数.

我读过的文档表明它getline应该有效,但在我的实验中它并没有阻止.我的示例程序:

#include <stdio.h>
int main()
{
    char *line = NULL;
    if (getline(&line, NULL, stdin) == -1) {
        printf("No line\n");
    } else {
        printf("%s\n", line);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

生成No line,这使它不适合接受用户输入.

我该怎么做呢?我知道这应该是微不足道的,但我无法弄明白.

c io

30
推荐指数
2
解决办法
8万
查看次数

为什么getline的第一个参数指向指针"char**"而不是"char*"?

我用getline函数来读取一行STDIN.

原型getline是:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Run Code Online (Sandbox Code Playgroud)

我使用它作为测试程序,来自http://www.crasseux.com/books/ctutorial/getline.html#getline

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int atgc, char *argv[])
{
    int bytes_read = 1;
    int nbytes = 10;
    char *my_string;

    my_string = (char *)malloc(nbytes+1);

    puts("Please enter a line of text");

    bytes_read = getline(&my_string, &nbytes, stdin);

    if (bytes_read == -1)
    {
        puts ("ERROR!");
    }
    else
    {
        puts ("You typed:");
        puts (my_string);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这很好用.

我的怀疑是?

  1. 为什么使用char **lineptr而不是char …

c gcc function

27
推荐指数
3
解决办法
6万
查看次数

在C中获取用户输入的最简单方法是什么?

似乎有很多方法可以在C中获得用户输入.

什么是最简单的方法,需要很少的代码?

基本上我需要显示这个:

Enter a file name: apple.text
Run Code Online (Sandbox Code Playgroud)

基本上我需要询问用户文件名.所以我需要的东西只能得到用户输入的那个词.

c io input scanf

20
推荐指数
2
解决办法
8万
查看次数

如何从命令行定义文件名并将其传递给fopen()?

我有以下程序将用户的输入写入文件.目前要写入的文件是预定义的; 但是,我想让用户从命令提示符定义文件名.添加此功能的最佳方法是什么?我无法理解如何将用户输入的字符串作为fopen()函数中的参数.我应该使用scanf()还是创建另一个getchar(),同时循环将chars存储在一个数组中,然后创建一个变量作为fopen()的参数或什么?

#include <stdio.h>

int main()
{
    char c;
    FILE *fp;

    fp = fopen("file.txt", "w");

    while ((c = getchar()) != EOF)
    {
        putc(c, fp);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

5
推荐指数
1
解决办法
3万
查看次数

如何在C中的一行中获得多个输入?

我知道我可以使用

scanf("%d %d %d",&a,&b,&c): 
Run Code Online (Sandbox Code Playgroud)

但是,如果用户首先确定该行中有多少个输入呢?

c input

5
推荐指数
1
解决办法
2万
查看次数

从C中的命令行捕获可变长度字符串

我到处寻找我的问题的答案,但我还没有找到一个可靠的答案来解决我的问题.

我目前正在用C编写程序,专门针对UNIX命令行(我使用Linux作为我的开发环境,但我希望这个程序尽可能便携).现在,我有一个提示用户输入的基本shell.然后,用户将输入命令,并相应地处理该命令.这是我到目前为止的代码:

/* Main.c */
int main(int argc, char **argv)
{
    while (TRUE)
    {
        display_prompt();
        get_command();
    }

    return 0;
}

/* Main.h */
void get_command()
{
    /*
     * Reads in a command from the user, outputting the correct response
     */

    int buffer_size = 20;   
    char *command = (char*) malloc(sizeof(char) * buffer_size);

    if (command == NULL)
    {
       return_error("Error allocating memory");
    }

    fgets(command, buffer_size, stdin);
    if (command[strlen(command) - 1] == '\n')
    {
        puts("It's inside the buffer.");
    }
    else
    {
        puts("It's not inside …
Run Code Online (Sandbox Code Playgroud)

c stdin

1
推荐指数
1
解决办法
1210
查看次数

逐字逐句地读取文件

我有一个自定义存档结构如下:

%list% name1 name2 name3 %list%

%dirs% archive directories %dirs%

%content% name1 path1 content of file1 %content%
%content% name2 path2 content of file2 %content%
%content% name3 path3 content of file3 %content%
Run Code Online (Sandbox Code Playgroud)

%list%包含存档中文件的名称
%dirs%包含目录名称
%content%列出文件内容.

由于我需要打印指定文件的内容,我想逐字阅读此存档,以便识别%content%标签和文件名.
我知道它的存在fscanf(),但只有你知道存档模式它似乎才有效.

是否有C库或命令,如ifstreamC++,允许我逐字阅读?

谢谢

c file scanf

1
推荐指数
1
解决办法
5万
查看次数

fgets 有更好的替代品吗?

我只是一名年轻的计算机科学学生,目前我对从stdin读取字符串的最佳实践有点困惑。我知道有很多方法可以做到这一点,有些方法比其他方法更安全,等等...我目前需要一个函数来防止缓冲区溢出并在末尾附加一个空终止符(\0)字符串的。我发现fgets对此非常有用,但是......它会停止读取 \n 或 EOF!如果我希望用户一次输入多行怎么办?还有其他功能可以帮助我做到这一点吗?如果这个问题对你们中的一些人来说似乎很愚蠢,我很抱歉,但是请理解我!任何帮助,将不胜感激。

c string fgets

1
推荐指数
1
解决办法
1万
查看次数

区别btw gets,scanf('%s'),getline

我在quora上阅读了这个问题,并读到Google在其一次采访中问了这个问题,

函数之间有什么区别:scanf(“%s”),gets和getline

任何人都可以提供详尽的清单及其解释。

c c++ std

-2
推荐指数
1
解决办法
2030
查看次数

标签 统计

c ×11

file ×2

input ×2

io ×2

scanf ×2

stdin ×2

c++ ×1

feof ×1

fgets ×1

function ×1

gcc ×1

std ×1

string ×1

while-loop ×1