C言語を使用して、タスク情報を記録できるシステムを作成しています。addtaskで受け取ったデータをtxtファイルに追加しedittaskで新しく受け取ったデータをtxtファイルに書き換えるコードを書こうと試みています。
しかしコードを実行したところ、edittaskのところで、tasknameが一致せずデータの変更及びtxtファイルが開けませんでした。
以下にソースコードをupしますので、原因と解決方法をご教授いただけますと幸いです。
c
1#include <stdio.h> 2#include <stdlib.h> 3 4 5void addtask(); 6void viewtask(); 7void edittask(); 8char menu(); 9 10int main() { 11 char selection ; 12 13 do 14 { 15 selection = menu(); 16 switch(selection) 17 { 18 case '1': addtask(); 19 break; 20 case '2': viewtask(); 21 break; 22 case '3': edittask(); 23 break; 24 case '5': printf("\nThank you for using Task manager!!^^"); 25 exit(0); 26 default: 27 printf("\nYou entered wrong choice..."); 28 printf("\nPress any key to try again"); 29 break; 30 } 31 } 32 while(selection != '9'); 33 return 0; 34} 35 36char menu() { 37 printf("\n\n\nWelcome to Task Management System!"); 38 printf("\nThis is Main Menu"); 39 char option; 40 printf("\n1) Add New Task"); 41 printf("\n2) Check your Task"); 42 printf("\n3) Edit your Task"); 43 printf("\n5) Exit"); 44 printf("\nEnter your choice: "); 45 scanf("%c[^\n]", &option); 46 return option; 47} 48 49void addtask() 50{ 51 int id, value; 52 char tsn[8]; 53 printf("\nThis is add new Task"); 54 FILE * f = fopen("data.txt", "r+"); 55 if(f == NULL) 56 f = fopen("data.txt", "w"); 57 else 58 { 59 fseek(f, 0, SEEK_END); 60 printf("Enter your Task name:"); 61 scanf("%s", tsn); 62 printf("\nENTER id:"); 63 scanf("%d", &id); 64 printf("\nEnter value: "); 65 scanf("%d", &value); 66 fprintf(f,"%s %d %d\n", tsn, id, value); 67 } 68 fclose(f); 69} 70 71void viewtask(){ 72 int i ,v; 73 char tsn[8]; 74 printf("* Viewing Task Menu *"); 75 printf("\nThis is your task"); 76 FILE * f = fopen("data.txt", "r"); 77 while(fscanf(f,"%s",&tsn) != EOF){ 78 fscanf(f,"%d", &i); 79 fscanf(f,"%d",&v); 80 printf("\n Task name: %s \t id: %d \t Value: %d",tsn, i, v); 81 } 82 fclose(f); 83} 84 85void edittask() { 86 char tsn[8]; 87 char ntsn[8]; 88 int v, i; 89 FILE * f = fopen("data.txt", "r"); 90 FILE * ft = fopen("ndata.txt", "w"); 91 printf("\nThis is edit your Task"); 92 printf("\nEnter Task name:"); 93 scanf("%s", &ntsn); 94 while(fscanf(f,"%s",&tsn) != EOF){ 95 fscanf(f,"%d", &i); 96 fscanf(f,"%d",&v); 97 if(ntsn == tsn){ 98 printf("\nUpdating -> task name: %s \t id: %d \t Value: %d",tsn, i, v); 99 printf("\nEnter new Task name:"); 100 scanf("%s", &tsn); 101 printf("\nEnter new id:"); 102 scanf("%d", &i); 103 printf("\nEnter new Value:"); 104 scanf("%d", &v); 105 } 106 else{ 107 printf("not correct"); 108 } 109 fprintf(ft,"%s %d %d\n", tsn, i, v); 110 } 111 fclose(ft); 112 fclose(f); 113 remove("data.txt"); 114 rename("ndata.txt", "data.txt");
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/06/01 07:00
2022/06/01 07:17