Ошибка в функциях вывода данных в файл - C (СИ)
Формулировка задачи:
Программа вводит записи, состоящие из 3 полей в 2 разных файла по выбору. Но в чём-то ошибка в функциях вывода данных в файл. А именно в строчке
Т.к. новичёк вопрос ещё- можно ли scanf("%s",a), где char *a
scanf("%s",port->str1);#include "stdio.h"
#include "stdlib.h"
struct Record{
char *str1;
char *str2;
char *str3;
} *port=NULL;
void input1();
void input2();
int main(){
int c;
printf("This program let you add cards of ports and ships\n");
printf("If you want to add port card press 1\nTo add ship card press 2\n");
printf("To close program press 3\n"); //zamenil escape na 3. Kak sdelat escape?
if ((c=getchar())==51){
exit (0);
}
else if (c==49){//Kod 1
input1();
}
else if (c==50){//Kod 2
input2();
}
return 0;
void input1(){
FILE *f=fopen("ports.dat","ab");
int c;
int flag=0;
if (f==NULL){
perror("Error opening file");
}
while(!flag){
printf("You are filling information about port\n\n\n");
printf("Fill-in port name\n");// Vstavit proverki. Kakie?
scanf("%s",port->str1);
fprintf(f,"%s",port->str1);
printf("Fill-in country\n");
scanf("%s",port->str2);
fprintf(f,"%s",port->str2);
printf("Fill-in port's sea\n");
scanf("%s",port->str3);
fprintf(f,"%s",port->str3);
printf("Return to main menu press 3. Otherwise press any key\n");
//scanf("%c",&a);//
if ((c=getchar())==51){//Mojno odni () ybrat ?Kak v windows proverit escape i enter?
flag=1;
}
}
fclose(f);
}
void input2(){
FILE *f=fopen("ships.dat","ab");
int c;
int flag=0;
if (f==NULL){
perror("Error opening file");
}
while(!flag){
printf("You are filling information about ship\n\n\n");
printf("Fill-in ship name\n");// Vstavit proverki
scanf("%s",port->str1);
fprintf(f,"%s",port->str1);
printf("Fill-in port of departure\n");
scanf("%s",port->str2);
fprintf(f,"%s",port->str2);
printf("Fill-in arrival port\n");
scanf("%s",port->str3);
fprintf(f,"%s",port->str3);
printf("Return to main menu press escape. Otherwise press any key\n");
//scanf("%c",&a);//
if ((c=getchar())==27){//Mojno odni () ybrat ?Kak v windows proverit escape i enter?
flag=1;
}
}
fclose(f);
}
up. Нужны советы
Решение задачи: «Ошибка в функциях вывода данных в файл»
textual
Листинг программы
struct my_struct {
char str1[20];
char str2[20];
char str3[20];
} *tmp;