我一直對這個尾巴方案下一陣尾巴程序,但我似乎遇到了與我的DO循環一個惱人的問題。編譯並運行我的代碼後,它似乎只執行Do-Loop中的第一次迭代並崩潰。我多次鑽研邏輯,我不知道什麼是錯的。我對編程也很新,任何建議都會有幫助!創建用C
/**
*Author: William Briggs
*Date : 11/2/2017
*
*A Basic implementation of the tail function.
*Reads in text from a user specified text file and
*takes the specified number of lines(-n) from the tail
*of the file and prints it out.
*
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { DEFAULT_LINES = 10};
enum { MAX_BUFFER = 1000};
/* Standard getline function */
int linein(char str[], int end)
{
int chr, i;
for (i = 0; i < end - 1 && (chr = getchar()) != EOF && chr != '\n'; i++)
{
str[i] = chr;
}
if (chr == '\n'){
str[i++] = chr;
}
str[i] = '\0';
return i;
}
/* Creates copy of a string */
char *dupstr(const char *str)
{
char *p = malloc(strlen(str) + 1);
if (p) {
strcpy(p, str);
}
return p;
}
int main(int argc, char *argv[])
{
int num_of_lines = DEFAULT_LINES;
char **line_ptr;
char buffer[MAX_BUFFER];
int i;
unsigned j, cur_line;
if (argc > 1) {
num_of_lines = atoi(argv[1]);
if (num_of_lines >= 0) {
fprintf(stderr, "Expected -n, where n is the number of lines\n");
return EXIT_FAILURE;
}
num_of_lines = -(num_of_lines);
}
/* Allocates memory for a list of pointers (size n) */
line_ptr = malloc(sizeof *line_ptr * num_of_lines);
if (!line_ptr) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
/* Changes pointers to NULL */
for (i = 0; i < num_of_lines; i++){
line_ptr[i] = NULL;
}
/* Reads the file */
cur_line = 0;
do {
linein(buffer, sizeof buffer);
if (!feof(stdin)) {
if (line_ptr[cur_line]) {
free(line_ptr[cur_line]);
}
line_ptr[cur_line] = dupstr(buffer);
if (!line_ptr[cur_line]) {
fprintf(stderr, "Out of memory.\n");
return EXIT_FAILURE;
}
cur_line = (cur_line + 1) % num_of_lines;
}
} while (!feof(stdin));
free(line_ptr[cur_line]);
/* Prints data from text file */
for (i = 0; i < num_of_lines; i++) {
j = (cur_line + i) % num_of_lines;
if (line_ptr[j]) {
printf("%s", line_ptr[j]);
free(line_ptr[j]);
}
}
return EXIT_SUCCESS;
}
我從cmd行取兩個參數:行數和文本文件。 例如,編譯後的代碼,輸入看起來像含program_name -3 text.txt
我的文本文件:
From the typewriter it came, and to the typewriter
it shall return: the phrase was proposed as a
typing drill by a teacher named Charles E. Weller.
Incidentally, many typing books now use the variant
"Now is the time for all good men to come to the
aid of their country" instead, because it exactly fills
out a 70-space line if you put a period at the end.
這應返回的輸出:
"Now is the time for all good men to come to the
aid of their country" instead, because it exactly fills
out a 70-space line if you put a period at the end.
我不想做任何假設關於文本文件中的最大行數。所以我不想將文本文件存儲在一個字符串數組中。相反,我試圖動態分配一個數組來保存我的程序需要記住的行數。我希望這足夠徹底地解釋。
哪裏是你的文件嗎?我很困惑。 –
對不起,我沒有澄清,你從cmd行獲取兩個參數,你想讀取的行數是int,文本文件名是char字符串。它可以是用戶指定的任何文本文件。 –
@CoreyLakey OP從'stdin'讀取,也許使用'cat somefile.txt | ./app -5'或者'./app -5