Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int i;
  5. FILE *puhluettelo;
  6. char luettelo[] = "luettelo.txt";
  7.  
  8. struct henkilo {
  9. char etunimi[20];
  10. char sukunimi[20];
  11. char puhnumero[20];
  12. };
  13.  
  14. struct henkilo henkilo_lista[49];
  15.  
  16.  
  17. int lkm = 0;
  18.  
  19. if((puhluettelo = fopen("luettelo.txt", "r")) == NULL) {
  20. printf("Tiedostoa ei löydetty!"); return 0;}
  21. else {
  22.  
  23. fscanf(puhluettelo, "%d", &lkm);
  24. fclose(puhluettelo);
  25. lkm++;
  26. }
  27.  
  28.  
  29. printf("Anna etunimi:");
  30. scanf("%s", &henkilo_lista[lkm].etunimi[0]);
  31. printf("Anna sukunimi:");
  32. scanf("%s", &henkilo_lista[lkm].sukunimi[0]);
  33. printf("Anna puhelinnumero:");
  34. scanf("%s", &henkilo_lista[lkm].puhnumero[0]);
  35.  
  36.  
  37. if(lkm<50)
  38. {
  39. if ((puhluettelo = fopen(luettelo, "r+")) == NULL) {
  40. printf("Virhe tiedoston avaamisessa.");
  41. return 0;
  42. }
  43. else {
  44. fprintf(puhluettelo, "%d", lkm);
  45. }
  46.  
  47. if ((puhluettelo = fopen(luettelo, "a")) == NULL) {
  48. printf("Virhe tiedoston avaamisessa.");
  49. return 0;
  50. }
  51. else {
  52.  
  53. for(i=lkm;i<=lkm;i++)
  54. {
  55. fprintf(puhluettelo, "%s %s %s", henkilo_lista[i].etunimi,
  56. henkilo_lista[i].sukunimi, henkilo_lista[i].puhnumero);
  57. }
  58. }
  59.  
  60. fclose(puhluettelo);
  61. printf("Tietojen tallennus onnistui.");
  62. return 0;
  63.  
  64. }
  65.  
  66.  
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement