c语言删除文件指定的某一行
答案:1 悬赏:60 手机版
解决时间 2021-02-01 13:33
- 提问者网友:佞臣
- 2021-02-01 04:17
c语言删除文件指定的某一行
最佳答案
- 五星知识达人网友:英雄的欲望
- 2021-02-01 05:22
这个程序适用于文件不是很大的情况,最好不要大于2G
先说思路,把文本文件按行读入内存,在内存中的结构是一个双向链表,每一个链表节点放一行。
文本文件 a.txt 先准备好,比如内容如下:
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
//以下是代码
#include
#include
#include
typedef struct lnode{ //define line node
struct lnode *prev;
struct lnode *next;
char *line;
} lnode;
typedef struct line_in_mem{
lnode *head;
lnode *tail;
} line_in_mem;
lnode *newnode(void){ // return a node in heap
return (lnode *)malloc(sizeof(lnode));
}
lnode *get_position(line_in_mem *lines){ // prepare an lnode whose line is NULL, to store a line read from file
lnode *node = newnode();
if(!node)
return NULL;
node->prev = node->next = NULL;
if(lines->head == NULL)
lines->head = lines->tail = node;
else{
lines->tail->next = node;
node->prev = lines->tail;
lines->tail = node;
}
return node;
}
void readfile(line_in_mem *lines, FILE *file){ // read lines from file and store them in struct line_in_mem
char *buf = NULL;
int n = 0;
while(getline(&buf, &n, file) > 0){
lnode *node = get_position(lines);
if(node)
node->line = buf;
else
exit(-1);
buf = NULL;
}
}
static void print_line(line_in_mem *line){ // show lines in mem, which may not be needed
lnode *node;
for(node = line->head; node ; node = node->next){
printf("%s", node->line);
}
}
void delete_line(line_in_mem *line, int line_no){ //given a line number, delete it from the list. if line_no is less than 0 or bigger than total of lines, do nothing
lnode *node = line->head;
while(--line_no > 0 && node)
node = node->next;
if(!node)
return;
if(node->prev)
node->prev->next = node->next;
if(node->next)
node->next->prev = node->prev;
free(node);
}
void savefile(line_in_mem *lines, FILE *file){ //truncate file to 0 and write back
freopen("a.txt","w",file);
lnode *node;
for(node = lines->head; node; node = node->next){
fwrite(node->line, sizeof(char), strlen(node->line)/sizeof(char), file);
}
}
int main(){
line_in_mem lines;
lines.head = lines.tail = NULL;
int line_no = 3;
FILE *file = fopen("a.txt","r");
if(!file)
return -1;
readfile(&lines, file); //read file into mem. the struct of file in mem is a list of line
print_line(&lines);
printf("---------------\n");
delete_line(&lines, line_no); //delete line_no in mem
print_line(&lines);
savefile(&lines, file); //write back
fclose(file);
return 0;
}
先说思路,把文本文件按行读入内存,在内存中的结构是一个双向链表,每一个链表节点放一行。
文本文件 a.txt 先准备好,比如内容如下:
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
this is line 6
//以下是代码
#include
#include
#include
typedef struct lnode{ //define line node
struct lnode *prev;
struct lnode *next;
char *line;
} lnode;
typedef struct line_in_mem{
lnode *head;
lnode *tail;
} line_in_mem;
lnode *newnode(void){ // return a node in heap
return (lnode *)malloc(sizeof(lnode));
}
lnode *get_position(line_in_mem *lines){ // prepare an lnode whose line is NULL, to store a line read from file
lnode *node = newnode();
if(!node)
return NULL;
node->prev = node->next = NULL;
if(lines->head == NULL)
lines->head = lines->tail = node;
else{
lines->tail->next = node;
node->prev = lines->tail;
lines->tail = node;
}
return node;
}
void readfile(line_in_mem *lines, FILE *file){ // read lines from file and store them in struct line_in_mem
char *buf = NULL;
int n = 0;
while(getline(&buf, &n, file) > 0){
lnode *node = get_position(lines);
if(node)
node->line = buf;
else
exit(-1);
buf = NULL;
}
}
static void print_line(line_in_mem *line){ // show lines in mem, which may not be needed
lnode *node;
for(node = line->head; node ; node = node->next){
printf("%s", node->line);
}
}
void delete_line(line_in_mem *line, int line_no){ //given a line number, delete it from the list. if line_no is less than 0 or bigger than total of lines, do nothing
lnode *node = line->head;
while(--line_no > 0 && node)
node = node->next;
if(!node)
return;
if(node->prev)
node->prev->next = node->next;
if(node->next)
node->next->prev = node->prev;
free(node);
}
void savefile(line_in_mem *lines, FILE *file){ //truncate file to 0 and write back
freopen("a.txt","w",file);
lnode *node;
for(node = lines->head; node; node = node->next){
fwrite(node->line, sizeof(char), strlen(node->line)/sizeof(char), file);
}
}
int main(){
line_in_mem lines;
lines.head = lines.tail = NULL;
int line_no = 3;
FILE *file = fopen("a.txt","r");
if(!file)
return -1;
readfile(&lines, file); //read file into mem. the struct of file in mem is a list of line
print_line(&lines);
printf("---------------\n");
delete_line(&lines, line_no); //delete line_no in mem
print_line(&lines);
savefile(&lines, file); //write back
fclose(file);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯