Advertisement
zsoltizbekk

bin all orai

May 13th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6.     char name[30];
  7.     int age;
  8. }STUDENT;
  9.  
  10. int main ()
  11. {
  12.     FILE *f;
  13.     STUDENT s;
  14.  
  15.     f=fopen("hallg.bin", "r");
  16.     if (f==NULL){
  17.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  18.         exit(-1);
  19.     }
  20.     while (fread(&s, sizeof(STUDENT), 1, f)>0)
  21.         printf("%s (%d)\n", s.name, s.age);
  22.  
  23.     fseek(f, -2 * sizeof(STUDENT), SEEK_CUR); //akt. poz
  24.     fread(&s, sizeof(STUDENT), 1, f);
  25.     printf("%s (%d)\n", s.name, s.age);
  26.  
  27.     fseek(f, 2 * sizeof(STUDENT), SEEK_SET); //eleje
  28.     fread(&s, sizeof(STUDENT), 1, f);
  29.     printf("%s (%d)\n", s.name, s.age);
  30.  
  31.     fseek(f, -sizeof(STUDENT), SEEK_END); //vege
  32.     fread(&s, sizeof(STUDENT), 1, f);
  33.     printf("%s (%d)\n", s.name, s.age);
  34.  
  35.     fclose(f);
  36.     return EXIT_SUCCESS;
  37. }
  38. /*
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. typedef struct {
  44.     char name[30];
  45.     int age;
  46. }STUDENT;
  47.  
  48. int main ()
  49. {
  50.     FILE *f;
  51.     STUDENT t[]={{"Maci Laci", 21}, {"Bena Bela", 19}, {"Teszt Elek", 20}};
  52.     int size=sizeof(t)/sizeof(int);
  53.     int i;
  54.  
  55.     f=fopen("hallg.bin", "w");
  56.     if (f==NULL){
  57.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  58.         exit(-1);
  59.     }
  60.  
  61.     fwrite(t, sizeof(t), 1, f);
  62.  
  63.     fclose(f);
  64.     return EXIT_SUCCESS;
  65. }
  66. */
  67. /*
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71.  
  72. int main ()
  73. {
  74.     FILE *f;
  75.     int t[]={-3, -2, -1, 0, 1, 2, 3, 2015};
  76.     int size=sizeof(t)/sizeof(int);
  77.     int i;
  78.  
  79.     f=fopen("szamok.bin", "w");
  80.     if (f==NULL){
  81.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  82.         exit(-1);
  83.     }
  84.  
  85.     for (i=0; i<size; ++i)
  86.         fwrite(&t[i], sizeof(int), 1, f),
  87.  
  88.     fwrite(t, sizeof(t), 1, f);
  89.  
  90.     fwrite(t, sizeof(int), size, f);
  91.  
  92.     fclose(f);
  93.     return EXIT_SUCCESS;
  94. }
  95. */
  96. /*
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100.  
  101. int main ()
  102. {
  103.     FILE *f;
  104.     int a, b;
  105.  
  106.     f=fopen("szamok.txt", "r");
  107.     if (f==NULL){
  108.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  109.         exit(-1);
  110.     }
  111.  
  112.     while (fscanf(f, "%d %d", &a, &b)!=EOF)
  113.         printf("%d\n", a+b);
  114.  
  115.     fclose(f);
  116.     return EXIT_SUCCESS;
  117. }
  118. */
  119. /*
  120. #include <stdio.h>
  121. #include <stdlib.h>
  122. #include <string.h>
  123.  
  124. int main ()
  125. {
  126.     FILE *f;
  127.     int ch;
  128.     int len=0;
  129.     int max=0;
  130.     char *line;
  131.  
  132.  
  133.     f=fopen("gyumolcs.txt", "r");
  134.     if (f==NULL){
  135.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  136.         exit(-1);
  137.     }
  138.  
  139.     while ((ch=fgetc(f))!=EOF){
  140.         if (ch=='\n'){
  141.             if (max<len)
  142.                 max=len;
  143.             len=0;
  144.         }
  145.         else
  146.             ++len;
  147.     }
  148.     fclose(f);
  149.     printf("%d\n", max);
  150.     line=(char *)calloc(max+2, sizeof(char));
  151.  
  152.     f=fopen("gyumolcs.txt", "r");
  153.  
  154.     if (f==NULL){
  155.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  156.         exit(-1);
  157.         }
  158.     while(fgets(line, max+2, f)!=NULL){
  159.         if (line[strlen(line)-1]=='\n')
  160.             line[strlen(line)-1]='\0';
  161.         if (strlen(line)==max)
  162.             puts(line);
  163.     }
  164.  
  165.     fclose(f);
  166.  
  167.     free(line);
  168.  
  169.     return EXIT_SUCCESS;
  170. }
  171. */
  172. /*
  173. #include <stdio.h>
  174. #include <stdlib.h>
  175. #include <string.h>
  176.  
  177. int main ()
  178. {
  179.     FILE *f;
  180.     int lines=0, ch;
  181.     f=fopen("gyumolcs.txt", "r");
  182.     if (f==NULL){
  183.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  184.         exit(-1);
  185.     }
  186.  
  187.     while ((ch=fgetc(f))!=EOF)
  188.         if (ch=='\n')
  189.             ++lines;
  190.  
  191.     fclose(f);
  192.  
  193.     printf("%d\n", lines);
  194.  
  195.     return EXIT_SUCCESS;
  196. }
  197. */
  198. /*
  199. #include <stdio.h>
  200. #include <stdlib.h>
  201. #include <string.h>
  202.  
  203. int main ()
  204. {
  205.     FILE *f;
  206.     char line[200];
  207.     f=fopen("gyumolcs.txt", "w"); //C:\Users\hallgato\Documents\Prog1L
  208.     if (f==NULL){
  209.         fprintf(stderr, "hiba az allomany megnyitasakor!\n");
  210.         exit(-1);
  211.     }
  212.     fputc('a', f);
  213.     fputc('\n', f);
  214.  
  215.     fputs("szilva", f);
  216.     fputc('\n', f);
  217.  
  218.     fprintf(f, "%d + %d = %d\n", 2, 3, 4 + 1);
  219.  
  220.     while (fgets(line, 200, stdin)!=NULL)
  221.         fputs(line, f);
  222.     fclose(f);
  223.     return EXIT_SUCCESS;
  224. }
  225.  
  226. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement