4

我想在 C 中创建一个程序,它将任意数量的任意长度的行作为输入,然后打印到控制台输入的最后一行。例如:

输入:

hi
my name is
david

输出:david

我认为最好的方法是有一个循环,将每一行作为输入并将其存储在一个 char 数组中,所以在循环结束时,最后一行最终是存储在 char 数组中的内容,我们可以打印出来。

到目前为止,我只上过一堂 C 语言讲座,所以我认为我的 Java/C++ 思维方式一直是错误的,因为我在这些语言方面有更多经验。

这是我到目前为止所拥有的,但我知道它远非正确:

#include <stdio.h>

int main()
{
    printf("Enter some lines of strings: \n");

    char line[50];

    for(int i = 0; i < 10; i++){
        line = getline(); //I know this is inproper syntax but I want to do something like this
    }

    printf("%s",line);


}

我也有i < 10循环,因为我不知道如何找到输入中的总行数,这将是循环的适当次数。此外,输入是从

./program < test.txt 

Unix shell 中的命令,其中test.txt有输入。

4

3 回答 3

6

使用fgets()

while (fgets(line, sizeof line, stdin)) {
    // don't need to do anything here
}
printf("%s", line);

您不需要限制迭代次数。在文件的末尾,fgets()返回NULL并且不修改缓冲区,因此line仍将保留读取的最后一行。

于 2018-10-05T14:55:12.530 回答
2

我假设您知道输入行的最大长度。

这里的这个肯定会为您完成工作

static char *getLine( char * const b , size_t bsz ) {
            return fgets(b, bsz, stdin) );
}

但请记住fgets也会'\n'在缓冲区的末尾放置一个字符,所以可能是这样的

static char *getLine( char * const b , size_t bsz ) {

            if( fgets(b, bsz, stdin) ){
                    /* Optional code to strip NextLine */
                    size_t size = strlen(b);
                    if( size > 0 && b[size-1] == '\n' ) {
                            b[--size] = '\0';
                    }
                    /* End of Optional Code */
                    return b;
            }
            return NULL;
    }

并且在调用 getline 时需要对您的代码进行一些更改

#define BUF_SIZE 256
char line[BUF_SIZE];

for(int i = 0; i < 10; i++){
    if( getLine(line, BUF_SIZE ) ) {
            fprintf(stdout, "line : '%s'\n", line);
    }

}

现在它是多么有可能创建类似的功能

char *getLine();

但是随后需要定义该函数的行为,例如,如果该函数getLine()动态分配内存,那么您可能需要使用 afree来取消分配由返回的指针getLine()

在这种情况下,函数可能看起来像

char *getLine( size_t bsz ) {
        char *b = malloc( bsz );
        if( b && fgets(b, bsz, stdin) ){
           return b;
        }
        return NULL;
}

根据您的功能有多小,您可以考虑使其成为inline可能现在有点离题的想法。

于 2018-10-06T10:19:16.387 回答
0

为了获得动态长度的动态输入数量,当输入长度更大时,您必须继续重新分配缓冲区。为了存储最后一行,您必须使用另一个指针来跟踪它并停止来自终端的输入,您必须按 EOF 键(ctrl+k)。这应该做你的工作。

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

char *get_last_line(FILE* fp, size_t size){
//The size is extended by the input with the value of the provisional
    char *str, *last_str = NULL;
    int ch;
    size_t len = 0, last_len = 0;
    str = realloc(NULL, sizeof(char)*size);//size is start size
    if(!str)return str;
    while(ch=fgetc(fp)){
        if(ch == EOF){
            break;
        }
        if(ch == '\n'){
            str[len]='\0';
            last_len = len;
            last_str = realloc(last_str,sizeof(char)*last_len);
            last_str[last_len]='\0';
            //storing the last line
            memcpy(last_str,str,sizeof(char)*last_len);
            str = realloc(NULL, sizeof(char)*size);//size is start size
            len = 0;
        }
        else {
            str[len++]=ch;
            if(len==size){
                str = realloc(str, sizeof(char)*(size+=16));
                if(!str)return str;
            }
        }
    }
    free(str);
    return last_str;
}

int main(void){
    char *m;

    printf("input strings : ");
    m = get_last_line(stdin, 10);
    printf("last string :");
    printf("%s\n", m);
    free(m);
    return 0;
}
于 2018-10-06T13:27:32.427 回答