Q&A
C言語を使用して、タスク情報を記録できるシステムを作成しています。事前に作成したtxtファイルをdeletetaskのコードを使ってtxtファイルを削除したいのですが、現在書いているコードだとstorage size of 'file' isn't knownが表示されてしまい実行がうまくいきません。以下にソースコード、txtファイルの内容をupしますので、エラーメッセージが出てくる原因と解決方法をご教授いただけますと幸いです。
c
1#include<stdio.h> 2 3#include<stdlib.h> 4 5#include<conio.h> 6 7#include<string.h> 8 9 10void deletetask(); 11 12struct task 13 14{ 15 16 char time[8]; 17 18 char name[50]; 19 20 char status[100]; 21 22 char category[100]; 23 24 char note[2000]; 25 26} ; 27 28int main() 29 30{ 31 32 int ch; 33 34 printf("\n\n\t---------------------------------\n"); 35 36 37 38 printf("\t---------------------------------"); 39 40 while(1) 41 42 { 43 44 printf("\n\tDelete Task\t[4]"); 45 46 printf("\n\n\tEnter your choice:"); 47 48 scanf("%d",&ch); 49 50 switch(ch) 51 52 { 53 54 case 4: 55 56 deletetask(); 57 58 break; 59 60 case 5: 61 62 printf("\n\n\t\tThank you for using Task manager!!^^"); 63 64 getch(); 65 66 exit(0); 67 68 default: 69 70 printf("\nYou entered wrong choice..."); 71 72 printf("\nPress any key to try again"); 73 74 getch(); 75 76 break; 77 78 } 79 80 system("cls"); 81 82 } 83 84 return 0; 85 86} 87 88void deletetask( ) 89 90{ system("cls"); 91 92 FILE *fp,*fptr ; 93 94 struct record file ; 95 96 char filename[15],another = 'Y' ,time[10];; 97 98 int choice,check; 99 100 printf("\n\n\t\t---------------------------------\n"); 101 102 printf("\t\t* Welcome to the Delete Task Menu *"); 103 104 printf("\n\t\t---------------------------------\n"); 105 106 107 while ( another == 'Y' ) 108 109 { 110 111 printf("\n\n\tHow would you like to delete?"); 112 113 printf("\n\n\t#Delete all tasks\t\t\t[1]"); 114 115 printf("\n\t#Delete a particular record by due date\t[2]"); 116 117 do 118 119 { 120 121 printf("\n\t\tEnter a number:"); 122 123 scanf("%d",&choice); 124 125 switch(choice) 126 127 { 128 129 case 1: 130 131 printf("\n\tEnter the due date of task to be deleted:[yyyy-mm-dd]:"); 132 133 fflush(stdin); 134 135 gets(filename); 136 137 fp = fopen ("task.txt", "wb" ) ; 138 139 if ( fp == NULL ) 140 141 { 142 143 printf("\nThe file does not exist"); 144 145 printf("\nPress any key to go back..."); 146 147 getch(); 148 149 return ; 150 151 } 152 153 fclose(fp); 154 155 remove(filename); 156 157 printf("\nDeleted successfully!"); 158 159 break; 160 161 case 2: 162 163 printf("\n\tEnter the due date of task:[yyyy-mm-dd]:"); 164 165 fflush(stdin); 166 167 gets(filename); 168 169 fp = fopen ("task.txt", "rb" ) ; 170 171 if ( fp == NULL ) 172 173 { 174 175 printf("\nThe file does not exist"); 176 177 printf("\nPress any key to go back..."); 178 179 getch(); 180 181 return ; 182 183 } 184 185 fptr=fopen("task.txt","wb"); 186 187 if(fptr==NULL) 188 189 { 190 191 printf("\nSystem error..."); 192 193 printf("\nPress any key to go back..."); 194 195 getch(); 196 197 return ; 198 } 199 200 printf("\n\tEnter the due date of task to be deleted:[hh:mm]:"); 201 202 fflush(stdin); 203 204 gets(time); 205 206 while(fread(&file,sizeof(file),1,fp)==1) 207 208 { 209 210 if(strcmp(file.time,time)!=0) 211 212 fwrite(&file,sizeof(file),1,fptr); 213 214 } 215 216 fclose(fp); 217 218 fclose(fptr); 219 220 remove(filename); 221 222 rename("task.txt","newtask.txt"); 223 224 printf("\nDeleted successfully!"); 225 226 break; 227 228 default: 229 230 printf("\n\tYou entered wrong choice.."); 231 232 break; 233 234 } 235 236 } 237 while(choice<1||choice>2); 238 239 printf("\n\tDo you want to delete another record?(Y/N):"); 240 241 fflush(stdin); 242 243 scanf("%c",&another); 244 245 } 246 247 printf("\n\n\tPress any key to exit"); 248 249 getch(); 250 }
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/05/22 00:44
2022/05/23 07:20 編集
2022/05/23 07:08