2016-05-05 319 views
-5

我有一些問題,我想讀取輸入的.txt文件參數並寫出output.txt。我只能找到一行,我怎麼能找到這個問題的所有行相同?而當input.txt文件參數刪除時,會刪除output.file中的字符。讀取文件並寫入文件C

input.txt的文件的詳細信息

write: 3 a 4 b 1 \n 2 d 
delete: 1 a 2 b 

output.txt的適用第一paramater將導致

aaa bbbb 

dd 

output.txt的適用第二paramater將導致

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; 
    } 
+0

0)'1 \ N' - >'2 \ N' 1)'字符選擇[] = 「寫」;'爲小'delete'。 2)'%s%d%c「' - >''%[^:]:%d%c」'但是不能讀取'\ n'(兩個字符) – BLUEPIXY

+0

謝謝您的迴應,當我試過我看到了錯誤,我怎麼能解決這個問題? – youngman

+0

這個建議,我認爲在你對delete命令應用delete命令後,對待任何好的(並且容易的),例如'write:3 a 4 b 1 \ n 2 d delete:1 a 2 b' ==>將寫入命令更新爲'write:2 a 2 b 1 \ n 2 d' – BLUEPIXY

回答

0

嘗試這個(省略一些檢查):

#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; 
    } 
} 
+0

[DEMO](http://ideone.com/xQzclF) – BLUEPIXY