C言語を使用して、タスク情報を記録できるシステムを作成しています。テキストファイルから読み取った各データ(日付(duedate)、進捗状況(p)、カテゴリー(cate))の情報を変更し、新しいtxtファイル(ndata.txt)からrename関数を使って名前の変更を行おうと考えています。
新しいtxtファイル(ndata.txt)には更新された情報が書き込めましたが、(ndata.txt)から(taskdata.txt)へのrenameがうまく機能せず困っています。
以下、データファイル(data.txt)です。
(左から、名前(tsn)、日付(duedate)、進捗状況(p)、カテゴリー(cat)になります。)
rtyu 20002222 0 3
ugc 2023333 20 2
sdihv 232493 30 3
vidhvb 237972702 40 3
diahwd 32870792 41 1
vwdusho 348702 11 2
w32euow 09230849 13 2
wevug 23842 32 1
qwvou 2311133 21 1
ewvie 190349 14 2
以下にソースコードをupしますので、原因と解決方法をご教授いただけますと幸いです。
c
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <stdlib.h> 5 6void addtask(); 7void edittask(); 8char menu(); 9 10int main() 11{ 12 char selection; 13 do 14 { 15 selection = menu(); 16 switch(selection) 17 { 18 case '0': addtask(); 19 break; 20 case '1': edittask(); 21 break; 22 case '9': printf("\nThank you for using Task manager!!^^"); 23 exit(0); 24 default: 25 printf("\nYou entered wrong choice..."); 26 printf("\nPress any key to try again"); 27 break; 28 } 29 } 30 while(selection != '10'); 31 return 0; 32} 33 34char menu() 35{ 36 printf("\n\n\nWelcome to Task Management System!"); 37 printf("\nThis is Main Menu"); 38 char option; 39 printf("\n0) Add New Task"); 40 printf("\n1) Edit your Task"); 41 printf("\n9) Exit"); 42 printf("\nEnter your choice:"); 43 scanf("%c[^\n]", &option); 44 return option; 45} 46 47void addtask() 48{ 49 char tsn[10]; 50 int duedate, p, cate; 51 52 printf("\n***** This is add new Task *****"); 53 FILE * f = fopen("taskdata.txt", "r+"); 54 if(f == NULL) 55 f = fopen("taskdata.txt", "w"); 56 else 57 { 58 fseek(f, 0, SEEK_END); 59 printf("\nEnter your Task name:"); 60 scanf("%s", tsn); 61 printf("\nEnter your Task due date[ex:20220518]:"); 62 scanf("%d", &duedate); 63 printf("\nEnter your Task progression[0~100]:"); 64 scanf("%d", &p); 65 printf("\nEnter Task category 1;study 2;work 3;other:"); 66 scanf("%d", &cate); 67 fprintf(f,"%s %d %d %d\n", tsn, duedate, p, cate); 68 printf("\nYour task is just added successfully!"); 69 } 70 fclose(f); 71} 72 73void edittask(){ 74 char tsn[10]; 75 char ntsn[10]; 76 int duedate, p, cate; 77 78 FILE * f = fopen("taskdata.txt", "r"); 79 FILE * ft = fopen("ndata.txt", "w"); 80 printf("\n***** This is Edit your Task *****"); 81 printf("\nEnter the name of the Task you want to edit:"); 82 scanf("%s", &ntsn); 83 while(fscanf(f,"%s",&tsn) != EOF) 84 { 85 fscanf(f,"%d", &duedate); 86 fscanf(f,"%d",&p); 87 fscanf(f,"%d",&cate); 88 if (strcmp(tsn, ntsn) == 0) 89 { 90 printf("\nUpdating-> Task name: %s \t Task due date: %d \t Progression: %d \t Task category: %d",tsn, duedate, p, cate); 91 printf("\nEnter new Task name:"); 92 scanf("%s", &tsn); 93 printf("\nEnter new due date[ex:20220518]:"); 94 scanf("%d", &duedate); 95 printf("\nEnter new Progression[0~100]:"); 96 scanf("%d", &p); 97 printf("\n\nEnter new Task category 1;study 2;work 3;other:"); 98 scanf("%d", &cate); 99 printf("\nYour task is edited now!"); 100 } 101 else 102 { 103 printf("The Task does not exist"); 104 } 105 fprintf(ft,"%s %d %d %d\n", tsn, duedate, p, cate); 106 } 107 fclose(ft); 108 fclose(f); 109 remove("taskdata.txt"); 110 rename(ndata.txt, taskdata.txt); 111}

回答1件
あなたの回答
tips
プレビュー