Я не понимаю, как сделать это как одну функцию, в которой пользователя спросят, где он хочет добавить следующий элемент.
- как заставить этот код работать?
Я знаю, что код действительно далек от хорошего. `
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main()
{
struct disk
{
char name [15];
int year;
float duration;
char singer [15];
struct disk *prvs;
struct disk *nxt;
};
typedef struct disk disk;
struct disk *head, *tail;
Я не хочу повторять одни и те же« вводные имена »в хвосте
void input_file(disk *p){
char ch;
FILE *A=fopen("disk_list.dat", "wb");
system("cls");
printf("\nEnter\n");
disk *p, *prev;
prev=NULL;
p=head;
do
{
if (p==head)
// while (p != NULL)
{
p=(disk *)malloc(sizeof(disk));
printf("\nName: ");
scanf("%s", p->name);
printf("\nYear: ");
scanf("%d", &p->year);
printf("\nDuration: ");
scanf("%f", &p->duration);
printf("\nSinger: ");
scanf("%s", p->singer);
fwrite(&p, sizeof(disk), 1, A);
p=p->nxt;
}
else if (p==tail)
//while (p!= NULL)
{
p=(disk *)malloc(sizeof(disk));
printf("\nName: ");
scanf("%s", p->name);
printf("\nYear: ");
scanf("%d", &p->year);
printf("\nDuration: ");
scanf("%f", &p->duration);
printf("\nSinger: ");
scanf("%s", p->singer);
fwrite(&p, sizeof(disk), 1, A);
p=p->prvs;
}
информация не записывается на фронт или в конец
printf("\nStop? y/n \n");
ch=getch();
if(ch=='n'){printf("Add info to 1head 2tail");
int an;
scanf("%d", &an);
switch(an)
{
case 1:
p=head;
case 2:
p=tail;
}}
}
while(ch!='y');
fclose(A);
}
int main()
{
int code;
FILE *disk;
disk=fopen("disk_list.dat", "wb");
error_file(disk);
Я пытался проверить, есть ли какая-либо информация в файле
fseek(disk,0,SEEK_END);
long pos=ftell(disk);
int blank;
if(pos>0)
{
rewind(disk);
}
else
blank=0;
while(1)
{
puts("Menu:");
puts("1-Enter information to file");
puts("2-Display information");
puts("Введіть код бажаної дії");
scanf("%d", &code);
printf("Choosen code is %d ", code);
int kod1;
switch(code)
{
case 1 :if(blank!=0){
puts("Next operation");
puts("1 - Сreate new playlist");
puts("2 - Add information to old playlist");
puts("Choose code:");
scanf("%d", &kod1);
printf("Your code is %d ", kod1);
switch(kod1){
case 1 : puts("New data will be written to file disk_list.dat. Allinformation from file will be deleted");
input_disk(head);
break;
case 2: puts("\nWhere add information?");
puts("1 - to end");
puts("2 - to front");
puts("Enter code ");
int kod1_2;
scanf("%d", &kod1_2);
printf("You entered %d ", kod1_2);
switch(kod1_2)
{
case 1 : //input_disk(tail);
break;
case 2: //input_disk(head);
break;
}
}
}else {puts("Data will be written to disk_list.dat");
input_disk(head);}
case 2 :
//output_disk(head);
break;
}
return 0;
}