This program writes, edits and deletes tasks from a txt file. I'm having problems with my add task function (case (1)) in this situation. Here's a snippet of the code:
switch(choice)
{
case '1':
system("cls");
fseek(fp,0,SEEK_END);
another = 'y';
while(another == 'y')
{
printf("\nEnter Task_Name: ");
scanf("%s", &t.Task_Name);
printf("\nEnter Leader: ");
scanf("%s", &t.Leader);
printf("\nEnter L_Email: ");
scanf("%s", &t.L_Email);
printf("\nEnter Member: ");
scanf("%s", &t.Member);
printf("\nEnter Mem_Email: ");
scanf("%s", &t.Mem_Email);
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
fwrite(&t,recsize,1,fp);
printf("\nAdd another task(y/n) ");
fflush(stdin);
another = getche();
}
break;
However, when I attempt to add a date with the above code I get gibberish in return. For example, when I type 11/11/1111 as the start date and 11/11/1111 as the end date, I get 6421994 and 6422005 in return. The same numbers shows up when I input any other date too.
To keep things reproducible I will have to include the entire code;
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<dos.h>
int main()
{
FILE * fp, * ft;
char another, choice;
struct task
{
char Task_Name[50], Leader[50], L_Email[50], Member[50], Mem_Email[50], Begin_date[11], End_date[11];
};
struct task t;
int mm, dd, yyyy;
char TaskName[40];
long int recsize;
fp = fopen("task.txt","rb ");
if(fp == NULL)
{
fp = fopen("task.txt","wb ");
if(fp == NULL)
{
printf("Cannot open file");
exit(1);
}
}
recsize = sizeof(t);
while(1)
{
system("cls");
printf("\n\t **** Welcome to Personal System Management ****");
printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Task\n\t\t[2] View all Task\n\t\t[3] Update Task\n\t\t[4] Delete Task\n\t\t[5] Exit Program\n\t\t=================\n\t\t");
printf("Enter choice: ");
choice = getche();
switch(choice)
{
case '1':
system("cls");
fseek(fp,0,SEEK_END);
another = 'y';
while(another == 'y')
{
printf("\nEnter Task_Name: ");
scanf("%s", &t.Task_Name);
printf("\nEnter Leader: ");
scanf("%s", &t.Leader);
printf("\nEnter L_Email: ");
scanf("%s", &t.L_Email);
printf("\nEnter Member: ");
scanf("%s", &t.Member);
printf("\nEnter Mem_Email: ");
scanf("%s", &t.Mem_Email);
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
fwrite(&t,recsize,1,fp);
printf("\nAdd another task(y/n) ");
fflush(stdin);
another = getche();
}
break;
case '2':
system("cls");
rewind(fp);
printf("Task Name|Leader|Leader Email|Member|Member Email|Begin Date|End Date");
while(fread(&t,recsize,1,fp)==1)
{
printf("\n%s %s %s %s %s %d %d",t.Task_Name,t.Leader,t.L_Email,t.Member,t.Mem_Email,t.Begin_date,t.End_date);
}
getch();
break;
case '3':
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the task name you want to update: ");
scanf("%s",TaskName);
rewind(fp);
while(fread(&t,recsize,1,fp)==1)
{
if(strcmp(t.Task_Name,TaskName) == 0)
{
printf("Enter new Member name: ");
scanf("%s",&t.Member);
printf("Enter new Member Email: ");
scanf("%s",&t.Mem_Email);
printf("Enter new End Date(dd/mm/yyyy): ");
scanf("%d/%d/%d",&dd,&mm,&yyyy);
fseek(fp,-recsize,SEEK_CUR);
fwrite(&t,recsize,1,fp);
break;
}
}
printf("\nUpdate another task(y/n)");
another = getche();
}
break;
case '4':
system("cls");
another = 'y';
while(another == 'y')
{
printf("Enter the task name you want to delete: ");
scanf("%s",TaskName);
ft = fopen("Temp.dat","wb");
rewind(fp);
while(fread(&t,recsize,1,fp) == 1)
{
if(strcmp(t.Task_Name,TaskName) != 0)
{
fwrite(&t,recsize,1,ft);
}
}
fclose(fp);
fclose(ft);
remove("task.txt");
rename("Temp.dat","task.txt");
fp = fopen("taxt.txt", "rb ");
printf("Delete another task(y/n)");
fflush(stdin);
another = getche();
}
break;
case '5':
fclose(fp);
exit(0);
}
}
return 0;
}
CodePudding user response:
You're never copying the dates into the structure.
printf("\nEnter Begin_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
sprintf(t.Begin_date, "d/d/d", dd, mm, yyyy);
printf("\nEnter End_date(dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd,&mm,&yyyy);
sprintf(t.End_date, "d/d/d", dd, mm, yyyy);
And since these are strings, you need to use %s
when printing them, not %d
.
printf("\n%s %s %s %s %s %s %s",t.Task_Name,t.Leader,t.L_Email,t.Member,t.Mem_Email,t.Begin_date,t.End_date);