按相反的顺序文件的副本内容 [英] copy content of file in reverse order
问题描述
我需要编写的文件复制到另一个文件中的内容,并逆转它的程序。
我发现了一个例子,看了一遍,了解正在发生的事情。
问题是,我的程序具有使用两个功能:
void反转(焦线[]){
INT I;
INT长;
焦炭TMP;
..
..
..
返回;
}
(没有进一步paramters或局部变量)
第二个功能做的工作的其余部分(打开文件,复制文件,关闭文件)
主程序只读取的文件的名称,并调用复制功能
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;void反转(焦线[])
{
INT I;
INT长;
焦温度;
如果(行== NULL)
返回;
长度= strlen的(线);
对于(i = 0; I<长/ 2 +长度%2; ++ I)
{
如果(行[I] ==行[长度 - 我 - 1])
继续;
TEMP =行[I]
线[I] =行[长度 - 我 - 1];
行[长度 - 我 - 1] =温度;
}
返回;
}
诠释的main()
{ FILE * src_fh,* dst_fh;
炭src_fn [256 + 1〕,dst_fn [256 + 1〕;
的printf(进入源文件名:\\ n);
与fgets(src_fn,sizeof的(src_fn),标准输入);反向(src_fn);
如果((src_fh = FOPEN(src_fn,R))== NULL)
{
的printf(ERROR:源文件%s无法打开...... \\ n,src_fn);
返回(-1);
}
的printf(请输入目标文件名:\\ n);
与fgets(dst_fn,sizeof的(dst_fn),标准输入);反向(dst_fn);
如果((dst_fh = FOPEN(dst_fn,W +))== NULL)
{
FCLOSE(src_fh);
的printf(ERROR:目标文件%s无法打开...... \\ n,dst_fn);
回报(-2);
} INT CH;
而((CH =龟etc(src_fh))!= EOF)
{
的fputc(CH,dst_fh);
}
FCLOSE(src_fh);
FCLOSE(dst_fh);
返回0;
}
您只需要交换的第一个字符与最后一个,第二个具有pre-最后,依此类推。
您其实并不需要在 INT温度
变量,但因为它似乎是必需的,这是
void反转(焦线[])
{
INT I;
INT长;
焦温度;
如果(行== NULL)
返回;
长度= strlen的(线);
对于(i = 0; I<长/ 2 +长度%2; ++ I)
{
如果(行[I] ==行[长度 - 我 - 1])
继续;
TEMP =行[I]
线[I] =行[长度 - 我 - 1];
行[长度 - 我 - 1] =温度;
}
返回;
}
这是一个改进版本,不用内部温度
,而不是我们存储的结果长/ 2 +长度%2
所以它不是重新计算在每个迭代
void反转(焦线[])
{
INT I;
INT长;
INT一半; 如果(行== NULL)
返回;
长度= strlen的(线);
半长= / 2 +长度%2;
对于(i = 0; I<一半; ++ I)
{
如果(行[I] ==行[长度 - 我 - 1])
继续;
行[长度] =行[I]
线[I] =行[长度 - 我 - 1];
行[长度 - 我 - 1] =行[长度]
}
行[长度] ='\\ 0';
返回;
}
只是使用终端的位置'\\ 0'
字节作为温度
交换时。
对于第二个功能,阅读每一行中使用与fgets
,并将其写入与文件fprintf中
,只记得从读删除字符串的换行符,您可以使用格格
函数y张贴的是,如果你不删除换行符,反向线路将有新行在该行的开头。
I need to write a program that is copying the content of a file to another file and reverses it. I found an example and read it through to understand what is going on. The problem is that my program has to use two functions:
void reverse(char line[]){
int i;
int length;
char tmp;
..
..
..
return;
}
(no further paramters or local variables)
The second function does the rest of the work(opens files, copies files, closes files) The main program only reads the name of the files and calls the copy function.
#include<stdio.h>
#include<string.h>
void reverse(char line[])
{
int i;
int length;
char temp;
if (line == NULL)
return;
length = strlen(line);
for (i = 0 ; i < length / 2 + length % 2 ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
temp = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = temp;
}
return;
}
int main()
{
FILE *src_fh, *dst_fh;
char src_fn[256+1], dst_fn[256+1];
printf("Enter Source File Name:\n");
fgets(src_fn, sizeof(src_fn), stdin); reverse(src_fn);
if( (src_fh = fopen(src_fn, "r")) == NULL )
{
printf("ERROR: Source File %s Failed To Open...\n",src_fn);
return(-1);
}
printf("Enter Destination File Name:\n");
fgets(dst_fn, sizeof(dst_fn), stdin); reverse(dst_fn);
if( (dst_fh = fopen(dst_fn, "w+")) == NULL )
{
fclose(src_fh);
printf("ERROR: Destination File %s Failed To Open...\n",dst_fn);
return(-2);
}
int ch;
while( (ch = fgetc(src_fh)) != EOF )
{
fputc(ch, dst_fh);
}
fclose(src_fh);
fclose(dst_fh);
return 0;
}
You only need to swap the first character with the last, the second with the pre-last, and so on.
You actually don't need the int temp
variable, but since it seems to be required, here it is
void reverse(char line[])
{
int i;
int length;
char temp;
if (line == NULL)
return;
length = strlen(line);
for (i = 0 ; i < length / 2 + length % 2 ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
temp = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = temp;
}
return;
}
This is an improved version, without int temp
, instead we store the result of length / 2 + length % 2
so it's not recalculated on each iteration
void reverse(char line[])
{
int i;
int length;
int half;
if (line == NULL)
return;
length = strlen(line);
half = length / 2 + length % 2;
for (i = 0 ; i < half ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
line[length] = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = line[length];
}
line[length] = '\0';
return;
}
just use the location of the terminating '\0'
byte as the temp
when swapping.
For the second function, read each line using fgets
and write it to the file with fprintf
, just remember to remove the newline character from the read strings, you can use the chomp
function y posted for that, if you don't remove the newline, the reversed lines will have the newline at the beginning of the line.
这篇关于按相反的顺序文件的副本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!