c - 仅打印长度超过 10 个字符的输入行 (ANSI C)

标签 c string file-io input output

所以我正在用 C 编写一个练习程序,其目的是获取用户输入,然后在到达 EOF 后,它读回输入,但只读回长度超过 10 个字符的行。

我使用的是 Linux,所以 EOF 是 Ctrl + D,但是,如果输入行超过 10,它会在我按下 Enter 时打印,而不是等到到达 EOF。

这是我的代码:

#define MAXSIZE 1000
#define SIZE 10

int checklen(char line[], int index);

int main()
{
    char currentline[MAXSIZE];
    int i = 0;

        while ((currentline[i] = getchar()) != EOF){
            if (currentline[i] == '\n'){
                if (checklen(currentline, i) > SIZE){
                    printf("%s", currentline);
                }
            }
            ++i;
        }

    return 0;
}

int checklen(char line[], int index)
{
    int i;
        for (i=index; line[i] != '\n'; ++i){
            ;
        }
    return i;
}

编辑:我已经尝试弄清楚它有一段时间了,但没有运气。我不太明白你们在说什么以及一切,但我们最终会到达那里:)

我已经重写了代码,但它仍然无法工作。

#include <stdio.h>

#define MAX 1000
#define SIZE 10

void examine(char input[], int index);

int main()
{
    int i=0;
    char input[MAX];
    char output[MAX];

        //take user input and store it in our input string
        while ((input[i] = getchar()) != EOF){
            ++i;
        }

        //put a null byte at the end of input[]
        input[i+1] = '\0';

        //examine line by line until end of string (null byte)
        for (i=0; input[i] != '\0'; ++i){
            if (input[i] == '\n'){
                examine(input, i);
            }
        }

    return 0;
}

void examine(char input[], int index)
{
    //decrement through input[] until \n or start [0] is reached
    int i=0;

        for (i=0; ((input[index] != '\n') || (index > 0)); ++i){
            --index;
        }
    //if line is bigger than 10 chars, print it 
    if (i>SIZE){
        for (; input[index+1] != '\n'; ++index){
            putchar(input[index]);
        }
    }

    //otherwise, return

    return;
}

最佳答案

重写了它。其实很简单。这是代码:

/*this program takes keyboard input from the user until EOF
and prints out their input excluding lines less than or equal to LINESIZE*/

#include <stdio.h>

#define MAX 2000
#define LINESIZE 10

void checkprint(char line[]);

int main()
{
    char input[MAX];
    char line[MAX];
    int i, i2;
    i2 = 0;

        //take input until EOF (Ctrl + D)
        for (i=0; (input[i]=getchar()) != EOF; ++i){
            ;
        }

    //add null byte to end of string    
    input[i+1] = '\0';
    //basic formatting for aesthetics
    putchar('\n');


        //copy a line into line[] from input[] until NULL byte reached
        for (i=0; input[i] != '\0'; ++i){
            line[i2] = input[i];
            ++i2;
            //if end of line, call checkprint
            if (input[i] == '\n'){
                checkprint(line);
                i2=0;
            }
        }   

    return 0;
}

void checkprint(char line[])
{


    int i;
        //find the length of the line
        for (i=0; line[i] != '\n'; ++i){
            ;
        }

        //if longer than LINESIZE, print it
    if (i > LINESIZE){
        putchar('\n');
        for (i=0; line[i] != '\n'; ++i){
            putchar(line[i]);
        }
    }   
}

关于c - 仅打印长度超过 10 个字符的输入行 (ANSI C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942692/

相关文章:

C:逐行读取文件

c - 为什么用不同的方式修改字符串会有不同的行为?

c - 链表中的节点交换

c - 使用 memcpy 丢弃限定符

java - 将字符串数组从某个参数连接在一起形成一个字符串?

java - 如何向我的审查器添加空格?

python - 在用 Python 写入文件之前如何确保文件存在或可以创建?

c++ - 编译器代码生成错误或内存损坏?

c - while循环中的哪条语句会先执行?

c++ - 用于加载文件的 while 循环不起作用