Also muss ich ID (int) eingeben (es muss ein 3-stelliges Format sein) Argument und eine Check-Funktion, wenn es gibt gleiche ID in der Datei datoteka.txt, die ich gemacht habe. Vielen Dank für HilfeC do {} while (Funktion (argumen) == 1) in I/O Dateien
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct struktura{
int ID;
char imeprezime[20];
int placa;
int godina;
};
int clear_input_buffer(void) {
int ch;
while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
return ch;
}
ID-Check: kann es nicht bekommen Flagge zurückkehren = 1;
//function that should check if there is same ID as the one you enter
//returning 1 if there is same ID or 0 if there is not
int provjera(int IDprovjera){
int flag=0;
int datID;
FILE *datprovjera = NULL;
datprovjera = fopen("datoteka.txt","r");
if(datprovjera == NULL){
printf("Nema datoteke!");
exit(2);
}
while(fscanf(datprovjera,"%d#%[^#]#%d#%d#",&datID)==4){
if (datID == IDprovjera){
flag=1;
break;
}
}
fclose(datprovjera);
return flag;
}
Haupt
int main()
{
int i,M;
int provjera_2;
struct struktura radnik;
FILE *datoteka = NULL;
datoteka = fopen("datoteka.txt","w");
if (datoteka == NULL){
printf("Greska!");
exit(1);
}
printf("Unesi broj radnika:\n");
scanf("%d",&M);
for(i=0;i<M;i++){
//ID
do{
do{
printf("Unesi ID\n");
scanf("%d",&radnik.ID);
clear_input_buffer();
}while(
provjera(radnik.ID)!=0);
}while(
radnik.ID/100>9 || radnik.ID/100<1);
fprintf(datoteka,"%d",radnik.ID);
fprintf(datoteka,"%c",'#');
//Ime Prezime
printf("Unesi ime i prezime radnika:\n");
gets(radnik.imeprezime);
fprintf(datoteka,"%s",radnik.imeprezime);
fprintf(datoteka,"%c",'#');
//Placa
printf("Unesi placu radnika:\n");
scanf("%d",&radnik.placa);
fprintf(datoteka,"%d",radnik.placa);
fprintf(datoteka,"%c",'#');
//Godine
do{
printf("Unesi godinu pocetka rada:\n");
scanf("%d",&radnik.godina);
clear_input_buffer();
}while(
radnik.godina<1970 || radnik.godina>2016);
fprintf(datoteka,"%d",radnik.godina);
fprintf(datoteka,"%c\n",'#');
}
fclose(datoteka);
Welche Werte nimmt 'datID' beim Lesen der Datei an? Was sind die Inhalte der Datei? Was ist ein Beispielwert für 'IDprovjera'? (@DevSolar - Dieser Hinweis scheint unnötig. Der Code ist unabhängig von den Variablennamen klar, die Kommentare sind in Englisch.) –
'while (provjera (radnik.ID)! = 0);' scheint * eine Übereinstimmung zu ignorieren . –
@WeatherVane - Das ist keine leere While-Schleife; Es ist das Ende einer 'do..while'-Schleife, die einen nicht leeren Körper hat (und daher die Übereinstimmung nicht ignoriert). –