标题:按逆序输出
取消只看楼主
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
 问题点数:0 回复次数:6 
按逆序输出

按逆序输出(从文件尾开始输出,一直到文件头)

void backforth(char *argv[])
{
FILE *fp;
char ch;
long i;

i=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp))
i++;
i--;
while (i>=0){
fseek(fp,--i,0);
ch=fgetc(fp);
fputc(ch,stdout);
}
fclose(fp);
}

这个函数哪错了呢?

搜索更多相关主题的帖子: 逆序 输出 argv char void 
2007-01-06 18:55
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 

全部函数太长了。。。。。。。。


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

main(int argc,char *argv[])
{
void prints(char *agrv[]);
void upper(char *argv[]);
void lower(char *argv[]);
void countline(char *argv[]);
void countword(char *argv[]);
void countchar(char *argv[]);
void backforth(char *argv[]);
int i;
char c;

if (argc==1)
printf("please input the file name\n");
else if (argc==2)
prints(argv);
else if (argc==3){
if (argv[1][0] !='-')
printf("usage:prog -A/a/l/w/c/r filename\n");
else
for (i=1;i<strlen(argv[1]);i++){
c=argv[1][i];
switch (c){
case 'A':upper(argv);break;
case 'a':lower(argv);break;
case 'l':countline(argv);break;
case 'w':countword(argv);break;
case 'c':countchar(argv);break;
case 'r':backforth(argv);break;
default:
printf("find:illegal %c\n",c);
argc=0;
break;
}
}
}
else
printf("usage:prog -A/a/l/w/c/r filename\n");
exit(0);
}

void prints(char *argv[])
{
FILE *fp;
char ch;

fp=fopen(argv[1],"r");
while (!feof(fp)){
ch=fgetc(fp);
fputc(ch,stdout);
}
fclose(fp);
exit(0);
}

void upper(char *argv[])
{
FILE *fp;
char ch;

fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch>='a' && ch<='z')
ch=ch+'A'-'a';
fputc(ch,stdout);
}
fclose(fp);
}

void lower(char *argv[])
{
FILE *fp;
char ch;

fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch>='A' && ch<='Z')
ch=ch-'A'+'a';
fputc(ch,stdout);
}
fclose(fp);
}

void countline(char *argv[])
{
FILE *fp;
char ch;
int count;

count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch=='\n')
++count;
}
fclose(fp);
printf("The number of lines is:%d\n",count);
}

void countword(char *argv[])
{
FILE *fp;
char ch;
int state,count;

state=count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch==' ' || ch=='\n' || ch=='\t')
state=0;
else if (state==0){
state=1;
++count;
}
}
fclose(fp);
printf("The number of words is:%d\n",count-1);
}

void countchar(char *argv[])
{
FILE *fp;
char ch;
int count;

count=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp)){
ch=fgetc(fp);
if (ch !='\n')
++count;
}
fclose(fp);
printf("The number of chars is:%d\n",count-1);
}

void backforth(char *argv[])
{
FILE *fp;
char ch;
long i;

i=0;
fp=fopen(argv[2],"r");
if (fp==NULL){
printf("can't open file\n");
exit(0);
}
while (!feof(fp))
i++;
i=i-1;
while (i>=0){
fseek(fp,--i,0);
ch=fgetc(fp);
fputc(ch,stdout);
}
fclose(fp);
}

貌似最后一个函数有问题。。。。。。

2007-01-06 18:59
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 
题目:命令行参数的程序prog.c,当运行prog filename时将文本文件filename按原样在标准输出上输出;当运行prog -[A/a/l/w/c/r] filename时:
-A: 全部大写输出 -a:全部小写输出
-l:最后输出总行数 -w:最后输出总单词数
-c:最后输出总字符数 -r: 按逆序输出(从文件尾开始输出,一直到文件头)
其中,任选项可以组合,如-Al, -lwc等。
2007-01-06 19:00
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 

哪位高手给改改啊,最后一个按逆序输出的函数

2007-01-06 19:08
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 

帮帮忙啊。。。。。。在线等

2007-01-06 19:19
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 
stdout标准输出啊
2007-01-06 19:30
slamzhzm
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-12-9
得分:0 

LS的好人啊,谢谢了

2007-01-07 13:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-114628-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.096208 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved