复制文件跳过前n行和后m行 [英] Copy file skipping first n lines and last m lines
问题描述
(例如n = 1,m = 2,源文件:
AAAAAAA
BBBBBBB
CCCCCCC
dest文件:
BBBBBBB)
我知道如何复制文件,但不知道如何跳过这些行。这里是我的复制代码:
char buf [128];
size_t size;
int source = open(argv [1],O_RDONLY);
int dest = open(argv [2],O_CREAT | O_APPEND | O_WRONLY);
if(source == -1){
printf(error);
return;
}
if(dest == -1){
printf(error);
return; ((size = read(source,buf,sizeof(buf)))> 0){
write(dest,buf,size);
}
。
}
close(source);
close(dest);
如何解决这个问题?
因为要说总共有多少行,我建议你
- 使用
fgets
逐行读取文件
- 跳过输出第一个
n
行 li> - 将其余的写入输出文件并计算行数并记住每行的长度。 使用
ftruncate
code>截断最后一个
m
行。 应该做的伎俩:
$ $ p $ code void copy_nm(char * source,char * dest,int n,int m){
FILE * in = fopen(source,r);
FILE * out = fopen(dest,w);
size_t file_length = 0;
size_t line_lengths [m + 1];
memset(line_lengths,0,sizeof(line_lengths));
int lengths_iterator = 0;
char buffer [0x400];
while(fgets(buffer,sizeof(buffer),in)){
size_t length = strlen(buffer);
if(n){//跳过这一行
if(buffer [length - 1] =='\\\
')//仅当它是一个实线时
-
继续;
}
fwrite(buffer,length,1,out);
line_lengths [lengths_iterator] + =长度;
file_length + = length;
if(buffer [length - 1]!='\\\
'){//行长于缓冲区
continue;
}
lengths_iterator ++;
lengths_iterator%= m + 1;
line_lengths [lengths_iterator] = 0;
for(lengths_iterator = 0; lengths_iterator< m + 1; lengths_iterator ++){
file_length - = line_lengths [lengths_iterator];
}
fseek(out,0,SEEK_SET); //在截断
之前回滚ftruncate(fileno(out),file_length);
}
I want to copy a file with skipping first n of its lines and last m lines using open, read, write and lseek
(eg. n = 1, m = 2, source file:
AAAAAAA
BBBBBBB
CCCCCCC
DDDDDDD
dest file:
BBBBBBB )
I know how to copy a file but don't know how to skip the lines. Here is my code for copy:
char buf[128];
size_t size;
int source = open(argv[1], O_RDONLY);
int dest = open(argv[2], O_CREAT | O_APPEND | O_WRONLY);
if(source == -1) {
printf("error");
return;
}
if(dest == -1) {
printf("error");
return;
}
while((size = read(source, buf, sizeof(buf))) > 0) {
write(dest, buf, size);
}
close(source);
close(dest);
How can i solve this problem?
You should use fgets
to read your file since that will read it line by line.
Because it is not trivial to say how many lines you have in total I would suggest you
- use
fgets
to read the file line by line - skip outputting the first
n
lines - write the rest to your output files and count the number of lines and remember the length of each.
- use
ftruncate
to truncate off the lastm
lines.
This should do the trick:
void copy_nm(char * source, char * dest, int n, int m) {
FILE * in = fopen(source, "r");
FILE * out = fopen(dest, "w");
size_t file_length = 0;
size_t line_lengths[m + 1];
memset(line_lengths, 0 , sizeof(line_lengths));
int lengths_iterator = 0;
char buffer[0x400];
while (fgets(buffer, sizeof(buffer), in)) {
size_t length = strlen(buffer);
if (n) { // skip this line
if (buffer[length - 1] == '\n') // only if it is a real line
n--;
continue;
}
fwrite(buffer, length, 1, out);
line_lengths[lengths_iterator] += length;
file_length += length;
if (buffer[length - 1] != '\n') { // line was longer then the buffer
continue;
}
lengths_iterator++;
lengths_iterator %= m+1;
line_lengths[lengths_iterator] = 0;
}
for (lengths_iterator = 0; lengths_iterator < m+1; lengths_iterator++) {
file_length -= line_lengths[lengths_iterator];
}
fseek(out, 0, SEEK_SET); // rewind before truncating
ftruncate(fileno(out), file_length);
}
这篇关于复制文件跳过前n行和后m行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!