【编程求助】用标准C实现文本文件按行反序输出

来源:百度知道 编辑:UC知道 时间:2024/06/21 18:40:43
【问题描述】
对于一个文本文件text1.log,编写一个程序,将该文件中的每一行字符按行反序后输出到另一个文件text2.log中。

【输入文件】
输入文件为当前目录下的text1.log,该文件含有多行任意字符,也可能有空行。每个文本行最长不超过80个字符。

【输出文件】
输出文件为当前目录下的text2.log。

【样例输入】

设输入文件text1.log为:

20080818 15:24 Monday
Hello, I am James.
Welcome to Beijing.
Today is the 10th day of Beijing Olympic Games.

【样例输出】

输出文件text2.log为:

Today is the 10th day of Beijing Olympic Games.
Welcome to Beijing.
Hello, I am James.
20080818 15:24 Monday

写得比较繁琐,而且。。。直接在main里面写的。。。

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

/* 每行最大数据数 */
#define MAX_NUM 80
/* 分页的行数,每次读了MAX_LINE行数据之后写入临时文件 */
#define MAX_LINE 2
/* 临时文件的前缀 */
#define TMP_FILE_NAME_PRE "text2.log.temp"

void err_exit(const char *info, int exit_code) {
puts(info);
exit(exit_code);
}

int main() {
FILE *in, *out, *out_tmp;
char str_data[MAX_LINE][MAX_NUM]; /* 最大读取内存 */
char tmp_file[256]; /* 保存临时文件名 */
int line_id; /* 读取的行数 */
int i;
int seq_flag; /* 分页数,即临时文件数 */
line_id = seq_flag = 0;

if ( (in=fopen("text1.log", "r")) == 0)
err_exit("Can't open file - text1.log\n", 1);
if ( (out=fopen("text2.log", "w")) == 0) {
fclose(in);
err_exit("Can't create file - text2.log\n", 2);