2017-06-09 34 views
0

display_text.c 該程序不使用字符串作爲緩衝器的矩陣,而是直接讀出的文本字符串。 文本必須顯示爲由40行分別組成的頁面。 打印第一頁後,程序將顯示一個提示>>並等待用戶的命令。從文本文件中讀取行,打印其中的40行,並且「必須」使用系統調用lseek或fseek來更改偏移量?

命令是:

n: to show next page 
p: to show previous page 
q: to quit the program 

該程序使用系統調用lseek(fd, offset, mode)fseek()。 注意行數有不同長度的事實!

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

//GLOBAL VARS 
const char *filename = NULL;  //file name 
FILE *file;      //file stream 
int page_number=1;    //number of current page 
int line_count; 
int page_total=0; 

//PROTOTYPES METHODS 
int prompt(void); 
int print_page(int num_page_print); 

int main(int argc, char *argv[]) { 
    int i; //loop counter 
    char name[50]; //name of file 

    //read the name of the file from command line 
    if(argc!=2) { 
     printf("Specify input on command line.\n"); 
     scanf("%s", name); 
     filename = name; 
    } 
    else if(argv[1]!=NULL) 
      filename=argv[1]; 
     else 
      return 1; //error 

    //try to open the file 
    file=fopen(filename, "rt"); 
    if(file == NULL){ 
     printf("Cannot open the file! %s \n",filename); 
     return 1; //error 
    } 
    prompt();      //call for prompt 
    fclose(file);     //close file 
    return 0;      //everything has gone as supposed :-) 
} 

int prompt(void) { 
    char *cmd=NULL;     //cmd 
    cmd=malloc(2*sizeof(char*));   //allocate two bit for command 
    char line[100]; 

    while(fgets(line, sizeof(line), file)!=NULL) //count file lines 
     line_count++; 

    rewind(file); 

    //number of total pages 
    if(line_count%40==0) 
     page_total=line_count/40; 
    else 
     page_total=line_count/40+1; 

    //printf("\nTotal pages are %d\n",page_total); 
    //while cmd!=q, continue to show prompt >> 
    while(1){ 
     //VIEW ALL COMMANDS 
     printf("a: next page; i: previous page; q: quit\n"); 
     printf(">> "); 
     scanf("%s", cmd); 
     //next page 
     if(page_number!=page_total && strcmp(cmd,"n")==0){ 
      print_page(page_number+1);    //print next page 
      page_number++;       //update number of page 
     } 
     //prev page 
     if(page_number!=1 && strcmp(cmd,"p")==0){ 
      print_page(page_number-1);    //print prev page 
      page_number--;       //update number of page 
     } 
     //exit 
     if(strcmp(cmd, "q")==0){ 
      free(cmd);      //free memory 
      return 0;       //success, return zero 
     } 
    } 
} 
//My problems start here 
int print_page(int num_page_print) { 
    char line[100]; 
    int i; 

    if(num_page_print < page_number) 
     //fseek change offset to print prev page 
    else 
     //fseek change offset to print next page 

    for(i = 0; i < 40; i++) { 
     fread(line, sizeof(line),1, file); 
     //how do I print lines that have different lengths! 
     printf("[%d] %s\n",i+1, line); 
    } 
} 
//Any comments, reccomendations and critics are welcome :-)` 

我想:先打印給定file.txt的40線,但因爲我是直接從文件中讀取,輸出我得到的是,超過40行印刷,大概是因爲char line[100]串被預定義,如果你能告訴我如何打印那些尺寸不同的線條,那將是非常棒的!

+0

'的scanf( 「%S」,姓名);'< - 緩衝區溢出。 –

+3

你有什麼問題要問嗎?另外,你應該檢查'malloc'的返回值以及打印和錯誤,例如,如果失敗,則使用'perror'。 – Badda

+0

我有麻煩試圖打印完整的.txt文件的40行,並使用fseek()來管理打印方法,其餘工作正常,因爲已經在另一個程序中使用 – Midnight

回答

0

兩個功能,允許線在文件中的行輕鬆閱讀。

第一個是getline()我建議你使用。你可以閱讀關於它here。這個函數的優點是它根據你正在閱讀的行的長度自動地爲你的存儲變量分配正確的大小。不幸的是沒有C的版本實現getline()。它不是C標準的一部分,而是POSIX.1-2008的一部分,所以你只能在符合這個標準的系統上使用它。

另一個功能,不太友善的使用是fgets()哪些文檔是here

在這兩種情況下,你只需要做出一個循環迭代其40倍,例如:

char* storingVariable = NULL; 
size_t len = 0; 
for (int i = 0; i != 39; getline(&storingVariable, &len, inFile)) 
    printf("Line is : %s\n", storingVariable); 

使用getline()的常用方法如下:

int i = 0; 
char* storingVariable = NULL; 
size_t len = 0; 
while (getline(&storingVariable, &len, inFile) != -1) // This allows to read until the end of the file 
{ 
    printf("Line is : %s\n", storingVariable); 
    i++; 
    if (i == 39) 
     break; 
} 
// free the storing variable if you don't need it 

我最近問了一個​​,這可能會比我剛剛做得更詳細。

+0

謝謝!!!!!!!!!!!!! – Midnight

+0

@Midnight沒問題。如果它解決了你的問題,請接受答案,如果你需要更多的解釋/細節,請不要猶豫,評論一些問題。 – Badda

+1

由於'getline()'是POSIX,因此可能需要使用功能測試宏來使其在某些系統上可以使用;這個細節可能值得添加答案。此外,當達到文件結束時,getline()會返回「-1」,所以你的第二個例子不能按預期工作。 –

相關問題