问题描述
- 求助各位大神,是LINUX下关于tail实现的
-
#include
#include
#include
#include
#define MAXLINE 50
int cur_rec;
int cur_nums;
int file_fd = -1;
char buf[MAXLINE ];
void file_open1(char *);
char *file_next(void);
void file_close1(int);
void swift(char *);
int main(int argc,char *argv[])
{
int n=0;
char c,*p;
int i =0;
char tail[1024];
tail[0] = '';
if(argc != 2){
fprintf(stderr,"hey,br honstn");
exit(1);
}
file_open1(argv[1]);
while(n <= 10){
p = file_next();
if(p == NULL)
break;
while((c = *--p) != ''){
if((tail[i++] = c)=='n')
n++;
if(n > 10)
break;
}
}
file_close1(file_fd);
tail[i] = '';
swift[tail];
fprintf(stdout,"ok,although tough,i get it %s n",tail);
return 0;
}void file_open1(char *name)
{
file_fd = open(name,O_RDONLY);
cur_rec = cur_nums = 0;
}char *file_next(void)
{
int reload();
char *p;
if(file_fd = -1)
return NULL;
if(cur_rec = 0 && reload() == 0)
return NULL;
p = &buf[cur_rec * MAXLINE];
cur_rec--;
return p;
}int reload(void)
{
int amt_read;
lseek(file_fd,-NRECS*MAXLINE,SEEK_END);
amt_read = read(file_fd,buf,MAXLINE);
cur_rec = cur_nums =MAXLINE;
return cur_nums;
}void file_close1(int fd)
{
if(fd != -1)
close(fd);
}void swift(char * s)
{
int min = 0;
char c;
int max = sizeof(s);
while(min <= max){
c = s[min];
s[max] = c;
s[min] = s[max];
max--;
min++;
}
}
编译了说是swift[tail]中数组下标不是整数,搞了半天也没弄成,求解答,谢谢了
解决方案
#include
#include
#include
#include
#define MAXLINE 10
int cur_rec;
int cur_nums;
int file_fd = -1;
char buf[MAXLINE ];
void file_open1(char *);
char *file_next(void);
void file_close1(int);
void swift(char *,int);
int main(int argc,char *argv[])
{
int n=0;
char c,*p;
int i =0;
char tail[1024];
tail[0] = '';
if(argc != 2){
fprintf(stderr,"hey,br honstn");
exit(1);
}
file_open1(argv[1]);
if(file_fd == -1)
fprintf(stderr,"can,t open");
while(n <= 10){
p = file_next();
if(p == NULL)
break;
while((c = *--p) != ''){
if((tail[i++] = c)=='n')
n++;
if(n > 10)
break;
}
}
file_close1(file_fd);
tail[i] = '';
swift(tail,i-1);
fprintf(stdout,"ok,although tough,i get it %s 45 n",tail);
return 0;
}
void file_open1(char *name)
{
file_fd = open(name,O_RDONLY);
lseek(file_fd,-MAXLINE,SEEK_END);
cur_rec = cur_nums = 0;
}
char *file_next(void)
{
int reload();
char *p;
if(file_fd = -1)
return NULL;
if(cur_rec == 0 && reload() == 0)
return NULL;
p = &buf[cur_rec];
cur_rec--;
return p;
}
int reload(void)
{
int amt_read;
amt_read = read(file_fd,buf,MAXLINE);
cur_rec = cur_nums =amt_read;
lseek(file_fd,-(amt_read+MAXLINE),SEEK_CUR);
return cur_nums;
}
void file_close1(int fd)
{
if(fd != -1)
close(fd);
}
void swift(char * s,int n)
{
int min = 0;
char c;
int max = n;
while(min <= max){
c = s[min];
s[max] = c;
s[min] = s[max];
max--;
min++;
}
}
xiugaile,bianyimeicuo,kedsbuchuzhilai..
解决方案二:
敲错代码了吧,swift是个函数,怎么能接下标用作数组呢,楼上的已经给你修改过了。