再请教一段简单C语言代码

我想把a.txt文件里面的文字调转,输出文件b.txt,应该如何编写

a.txt 里面的内容是第一行是“abcde”,第二行是“12345”,第三行是“ABCDE”
b.txt 里面的内容是第一行是“edcba”,第二行是“54321”,第三行是“EDCBA”

就是每行倒序调换,而不是全部文字调换

请详细编写,要能运行的全部代码

急用
谢谢

!子程序已补全。
这么长的代码,难怪没人帮你。不要把这里当成无偿索取的地方。
花了一个多小时,实在没心情调试了,自己调试吧。
要求C99。

// tline.c -- by Richard Grenville
// Shall use with complier supporting C99 to be safe
// Not Tested
#include <stdio.h>
#include <stdlib.h>

#define HOUZHUI ".tmp"
#define LINESIZE 256

bool chkoverwrite(char *fname);
bool filecpy(FILE *source, FILE *destination);
inline void erroroverflow(void){
printf(ERRMSG[8]);
exit(6);
}

int main(int argc, char *argv[]){
const *ERRMSG[] = {
"Author: Richard Grenville\nUsage: %s Source File [Destination]\n",
"Can't find enough argument\n",
"%s failed, check the order of arguments and filename.\n",
"Read",
"Write"
"Close file failed, check your disk.\n"
"%s already exists. Are you sure you want to overwrite it?(Y/N) ",
"Please enter \"y\" or \"n\"\n",
"Too many characters in a line.\n"
};

char linetmp[LINESIZE + 2];
char *linepos;
int ctmp;
FILE fr, fw;

if(argc < 1 || argc > 2){
printf(ERRMSG[0], argv[0]);
printf(ERRMSG[1]);
exit(1);
}
else{
if((fr = fopen(argv[1], "r") == NULL)
printf(ERRMSG[0], argv[0]);
printf(ERRMSG[3]);
printf(ERRMSG[2], argv[1]);
exit(2)
if(argc == 1){
if((fw = fopen(strcat(char tmp[LINESIZE + 5], HOUZHUI), argv[1], "rb") == NULL || (fclose(fw) && chkoverwrite(argv[1]))) && fopen(tmp, "w+b") != NULL){
filecpy(fr, fw);
}
fclose(fr);
fw = fopen(argv[1], "w");
}
else if(!((fw = fopen(argv[2], "r") == NULL || (fclose(fw) && chkoverwrite(argv[2]))) && fopen(argv[2], "w") != NULL)){
printf(ERRMSG[0], argv[0]);
printf(ERRMSG[3]);
printf(ERRMSG[2], argv[2]);
}
}
while(fgets(linetmp, LINESIZE + 2, fr) != EOF){
for((linepos = strchr(linetmp, '\n')) == NULL ? (getc(ctmp, fr) == EOF ? ungetc(ctmp, fr), (linepos = strchr(linetmp, '\0')): (erroroverflow(): 0; linepos > linetmp; linepos--){
putc(linepos, fw);
}
putc(linetmp, fw);
}
if(fclose(fr) || fclose(fw)){
printf(ERRMSG[5]);
exit(5);
}
printf("Finished!\n");
getchar();

return 0;
}

bool chkoverwrite(char *fname){
char ans;

printf(ERRMSG[6], fname);
while((ans = getchar()) == 'y' ? return 1: (ans == 'n' ? return 0: 1)){
printf(ERRMSG[7]);
while(getchar() != '\n'){ continue; }
}
}

bool filecpy(FILE *source, FILE *destination){
int ch;

while((ch = getc(source)) != EOF)
putc(ch, destination);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-11-11
不难

1。打开文件a.txt
2。打开文件b.txt
3。从a.txt读一行到内存,倒序,写到b.txt
4。重复3。直到a.txt文件结束
5。关闭文件a.txt
6。关闭文件b.txt
第2个回答  2008-11-11
麽分
第3个回答  2008-11-11
这种问题没分行么???
相似回答