用C语言读写文件
我有一些问题,我想读input.txt文件的参数,写output.txt。我只能找到一行,怎么才能找到所有这行相同的问题?当input.txt文件参数删除时,会删除output.txt文件中的字符 input.txt文件详细信息用C语言读写文件,c,C,我有一些问题,我想读input.txt文件的参数,写output.txt。我只能找到一行,怎么才能找到所有这行相同的问题?当input.txt文件参数删除时,会删除output.txt文件中的字符 input.txt文件详细信息 write: 3 a 4 b 1 \n 2 d delete: 1 a 2 b output.txt将应用第一个参数将是结果 aaa bbbb dd output.txt将应用第二个参数作为结果 aa bb dd 提前谢谢你的回答 #include
write: 3 a 4 b 1 \n 2 d
delete: 1 a 2 b
output.txt将应用第一个参数将是结果
aaa bbbb
dd
output.txt将应用第二个参数作为结果
aa bb
dd
提前谢谢你的回答
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(void)
{
FILE *read, *write; /* files pointers */
char *source_file = "input.txt";
char *dest_file= "output.txt";
int count;
char ch;
char choose []="write";
int z=0;
if( (read=fopen(source_file, "r")) == NULL ) /* files check ? */
{
printf("%s didnt' open .\n", source_file);
exit(1);
}
if( (write=fopen(dest_file, "w")) == NULL )
{
printf("%s didnt. open \n", source_file);
exit(2);
}
fscanf(read,"%s%d%c",&choose,&count,&ch);
while( ( ch = fgetc(read) ) != EOF )
{
printf("%d%c",count,ch);
z=0;
for (; z<count; z++)
{
fprintf(write,"%c",ch);
printf("\n");
}
fprintf(write," ");
fscanf(read,"%d%c",&count,&ch);
printf("\n");
}
fclose(read); /* files close */
fclose(write);
printf("%s > %s\n",source_file, dest_file);
return 0;
}
#包括
#包括
#包括
内部主(空)
{
文件*读、*写;/*文件指针*/
char*source_file=“input.txt”;
char*dest_file=“output.txt”;
整数计数;
char ch;
字符选择[]=“写入”;
int z=0;
如果((read=fopen(source_file,“r”))==NULL)/*文件检查*/
{
printf(“%s未打开。\n”,源文件);
出口(1);
}
if((write=fopen(dest_文件,“w”))==NULL)
{
printf(“%s didnt.open\n”,源文件);
出口(2);
}
fscanf(读取,“%s%d%c”、&choose、&count、&ch);
而((ch=fgetc(读取))!=EOF)
{
printf(“%d%c”,计数,ch);
z=0;
对于(;z试试这个(省略一些检查):
#包括
#包括
#包括
类型定义结构任务{
int-id;
char ch;
整数倍;
结构任务*下一步;
}任务;
类型定义结构任务列表{
任务*负责人;
任务*尾部;
}任务清单;
typedef枚举模式{NOP,WRITE,DELETE}模式;
任务*新任务(int-id、int-freq、char-aChar);
作废添加任务列表(任务列表*列表,任务*任务);
无效更新任务列表(任务列表*列表,任务*任务);
作废任务列表(任务列表*列表);
作废fout_任务列表(文件*fp,任务列表*list);
内部主(空){
字符行[1024];
char命令[16];
任务列表={NULL,NULL};
文件*fp;
int i=0;
fp=fopen(“input.txt”,“r”);
而(fgets(生产线、生产线尺寸、fp)){
内部长度,位置=0;
模式=NOP;
sscanf(行“%15[^:::%n”、命令和len);
pos+=len;
如果(!strcmp(命令,“写入”))
模式=写入;
如果(!strcmp(命令,“删除”))
模式=删除;
其他的
继续;//忽略
intfreq=0;
查阿查;
而(2==sscanf(行+位置,%d%c%n,&freq,&aChar,&len)){
如果(aChar=='\\'){
char esc_char=行[pos+len];
len+=1;
开关(esc_字符){
案例“n”:
阿喀尔='\n';
打破
案例“t”:
亚喀尔='\t';
打破
案例“”:
阿喀尔='';
打破
}
}
任务*任务=新任务(++i,freq,aChar);
如果(模式==写入)
添加任务列表(&列表,任务);
else//if(模式==删除)
更新任务列表(&列表,任务);
pos+=len;
}
}
fclose(fp);
fp=fopen(“output.txt”,“w”);
fout_任务列表(fp和列表);
fclose(fp);
删除任务列表(&L);
返回0;
}
任务*新任务(int-id、int-freq、char-aChar){
Task*new_Task=malloc(sizeof(*new_Task));
如果(新任务){
新建任务->id=id;
新建任务->次数=频率;
新任务->ch=aChar;
}
返回新的任务;
}
作废添加任务列表(任务列表*列表,任务*任务){
如果(列表->标题){
列表->尾部=列表->尾部->下一步=任务;
}否则{
列表->头部=列表->尾部=任务;
}
}
无效更新任务列表(任务列表*列表,任务*任务){
任务*p=列表->头;
while(p){
如果(p->ch==任务->ch){
p->times-=task->times;//如果(p->times<0)p->times=0;
如果(task->id>0){//只执行找到的第一个元素
打破
}
}
p=p->next;
}
}
作废删除任务列表(任务列表*列表){
任务*p=列表->头;
while(p){
任务*tmp=p->next;
自由基(p);
p=tmp;
}
列表->头部=列表->尾部=空;
}
作废fout_任务列表(文件*fp,任务列表*list){
char prev='\n';
任务*p=列表->头;
while(p){
对于(int i=0;itimes;++i){
fputc(上一个=p->ch,fp);
}
如果(上一个!='\n')
fputc(“”,fp);
p=p->next;
}
}
0)1\n
-->2\n
1)char选择[]=“写入”
小字符delete
.2)%s%d%c”
-->“%[^::%d%c”
但不能读取\n
(两个字符)感谢您的回复,当我尝试时,我看到了错误,我如何解决问题?该建议,我认为在您将delete命令应用于write命令后,处理任何好的(和简单的)问题。例如write:3a4b1\n2d delete:1a2b
=>将write-comand更新为write:2a 2b1\n2d
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct task {
int id;
char ch;
int times;
struct task *next;
} Task;
typedef struct task_list {
Task *head;
Task *tail;
} TaskList;
typedef enum mode { NOP, WRITE, DELETE } Mode;
Task *new_task(int id, int freq, char aChar);
void add_taskList(TaskList *list, Task *task);
void update_taskList(TaskList *list, Task *task);
void drop_taskList(TaskList *list);
void fout_taskList(FILE *fp, TaskList *list);
int main(void) {
char line[1024];
char command[16];
TaskList list = { NULL, NULL};
FILE *fp;
int i = 0;
fp = fopen("input.txt", "r");
while(fgets(line, sizeof line, fp)){
int len, pos = 0;
Mode mode = NOP;
sscanf(line, " %15[^:]:%n", command, &len);
pos += len;
if(!strcmp(command, "write"))
mode = WRITE;
else if(!strcmp(command, "delete"))
mode = DELETE;
else
continue;//ignore
int freq = 0;
char aChar;
while(2 == sscanf(line + pos, "%d %c%n", &freq, &aChar, &len)){
if(aChar == '\\'){
char esc_char = line[pos + len];
len += 1;
switch(esc_char){
case 'n':
aChar = '\n';
break;
case 't':
aChar = '\t';
break;
case ' ':
aChar = ' ';
break;
}
}
Task *task = new_task(++i, freq, aChar);
if(mode == WRITE)
add_taskList(&list, task);
else //if(mode == DELETE)
update_taskList(&list, task);
pos += len;
}
}
fclose(fp);
fp = fopen("output.txt", "w");
fout_taskList(fp, &list);
fclose(fp);
drop_taskList(&list);
return 0;
}
Task *new_task(int id, int freq, char aChar){
Task *new_task = malloc(sizeof(*new_task));
if(new_task){
new_task->id = id;
new_task->times = freq;
new_task->ch = aChar;
}
return new_task;
}
void add_taskList(TaskList *list, Task *task){
if(list->head){
list->tail = list->tail->next = task;
} else {
list->head = list->tail = task;
}
}
void update_taskList(TaskList *list, Task *task){
Task *p = list->head;
while(p){
if(p->ch == task->ch){
p->times -= task->times;//if(p->times < 0) p->times = 0;
if(task->id > 0){//Execute only the first found element
break;
}
}
p = p->next;
}
}
void drop_taskList(TaskList *list){
Task *p = list->head;
while(p){
Task *tmp = p->next;
free(p);
p = tmp;
}
list->head = list->tail = NULL;
}
void fout_taskList(FILE *fp, TaskList *list){
char prev = '\n';
Task *p = list->head;
while(p){
for(int i = 0; i < p->times; ++i){
fputc(prev = p->ch, fp);
}
if(prev != '\n')
fputc(' ', fp);
p = p->next;
}
}