Zapis struktury do pliku :)
Ostatnio zmodyfikowano 2017-01-22 18:56
xyz12 Temat założony przez niniejszego użytkownika |
Zapis struktury do pliku :) » 2017-01-22 15:00:45 Hej, napisalem program, struktury działają, ale wiele razy mi drukuje.. :
#include <stdio.h> #include <string.h>
struct student { char nazwisko[10]; char imie[10]; int rokStudiow ; char wydzial[10]; }studenci [10];
int main() { int i;
printf("\nWczytanie danych z klawiatury:");
for(i=1;i<11;i++) { printf("\nNazwiko student numer %d:", i); scanf("%s", studenci[i-1].nazwisko); fflush(stdin); printf("\nImie student numer %d:", i); scanf("%s", studenci[i-1].imie); fflush(stdin); printf("\nRok studiow:"); scanf("%d",&studenci[i-1].rokStudiow); fflush(stdin); printf("\nWydzial studenta %d:", i); scanf("%s", studenci[i-1].wydzial);
}
FILE *fp ; fp=fopen("ListaStudentow.dat", "wb"); fseek(0,0,SEEK_SET); //niepotrzebne? fwrite(&studenci, sizeof(struct student), 1, fp) ;
fseek(0,0, SEEK_SET);
while (fread(studenci,sizeof(struct student),1,fp)) { for(i=1;i<11;i++) {printf("\n Dane studenta %d",i); printf("\n Nazwisko:%s", studenci[i-1].nazwisko); printf("\n Imie:%s", studenci[i-1].imie); printf("\n Rok studiow: %d", studenci[i-1].rokStudiow); printf("\n Wydzila studenta:%s", studenci[i-1].wydzial); }
}
system("PAUSE"); return 0; }
|
|
j23 |
» 2017-01-22 18:56:33 Po pierwsze, otwierasz strumień w trybie tylko-do-zapisu, a próbujesz z niego czytać. Użyj trybu w+b. Po drugie, zapisujesz tylko jeden element tablicy, a nie całą tablicę (bo chyba o to Ci chodzi, prawda?). |
|
« 1 » |