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]
串被预定义,如果你能告诉我如何打印那些尺寸不同的线条,那将是非常棒的!
'的scanf( “%S”,姓名);'< - 缓冲区溢出。 –
你有什么问题要问吗?另外,你应该检查'malloc'的返回值以及打印和错误,例如,如果失败,则使用'perror'。 – Badda
我有麻烦试图打印完整的.txt文件的40行,并使用fseek()来管理打印方法,其余工作正常,因为已经在另一个程序中使用 – Midnight