Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- char name[30];
- int age;
- }STUDENT;
- int main ()
- {
- FILE *f;
- STUDENT s;
- f=fopen("hallg.bin", "r");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- while (fread(&s, sizeof(STUDENT), 1, f)>0)
- printf("%s (%d)\n", s.name, s.age);
- fseek(f, -2 * sizeof(STUDENT), SEEK_CUR); //akt. poz
- fread(&s, sizeof(STUDENT), 1, f);
- printf("%s (%d)\n", s.name, s.age);
- fseek(f, 2 * sizeof(STUDENT), SEEK_SET); //eleje
- fread(&s, sizeof(STUDENT), 1, f);
- printf("%s (%d)\n", s.name, s.age);
- fseek(f, -sizeof(STUDENT), SEEK_END); //vege
- fread(&s, sizeof(STUDENT), 1, f);
- printf("%s (%d)\n", s.name, s.age);
- fclose(f);
- return EXIT_SUCCESS;
- }
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct {
- char name[30];
- int age;
- }STUDENT;
- int main ()
- {
- FILE *f;
- STUDENT t[]={{"Maci Laci", 21}, {"Bena Bela", 19}, {"Teszt Elek", 20}};
- int size=sizeof(t)/sizeof(int);
- int i;
- f=fopen("hallg.bin", "w");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- fwrite(t, sizeof(t), 1, f);
- fclose(f);
- return EXIT_SUCCESS;
- }
- */
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *f;
- int t[]={-3, -2, -1, 0, 1, 2, 3, 2015};
- int size=sizeof(t)/sizeof(int);
- int i;
- f=fopen("szamok.bin", "w");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- for (i=0; i<size; ++i)
- fwrite(&t[i], sizeof(int), 1, f),
- fwrite(t, sizeof(t), 1, f);
- fwrite(t, sizeof(int), size, f);
- fclose(f);
- return EXIT_SUCCESS;
- }
- */
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *f;
- int a, b;
- f=fopen("szamok.txt", "r");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- while (fscanf(f, "%d %d", &a, &b)!=EOF)
- printf("%d\n", a+b);
- fclose(f);
- return EXIT_SUCCESS;
- }
- */
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *f;
- int ch;
- int len=0;
- int max=0;
- char *line;
- f=fopen("gyumolcs.txt", "r");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- while ((ch=fgetc(f))!=EOF){
- if (ch=='\n'){
- if (max<len)
- max=len;
- len=0;
- }
- else
- ++len;
- }
- fclose(f);
- printf("%d\n", max);
- line=(char *)calloc(max+2, sizeof(char));
- f=fopen("gyumolcs.txt", "r");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- while(fgets(line, max+2, f)!=NULL){
- if (line[strlen(line)-1]=='\n')
- line[strlen(line)-1]='\0';
- if (strlen(line)==max)
- puts(line);
- }
- fclose(f);
- free(line);
- return EXIT_SUCCESS;
- }
- */
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *f;
- int lines=0, ch;
- f=fopen("gyumolcs.txt", "r");
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- while ((ch=fgetc(f))!=EOF)
- if (ch=='\n')
- ++lines;
- fclose(f);
- printf("%d\n", lines);
- return EXIT_SUCCESS;
- }
- */
- /*
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main ()
- {
- FILE *f;
- char line[200];
- f=fopen("gyumolcs.txt", "w"); //C:\Users\hallgato\Documents\Prog1L
- if (f==NULL){
- fprintf(stderr, "hiba az allomany megnyitasakor!\n");
- exit(-1);
- }
- fputc('a', f);
- fputc('\n', f);
- fputs("szilva", f);
- fputc('\n', f);
- fprintf(f, "%d + %d = %d\n", 2, 3, 4 + 1);
- while (fgets(line, 200, stdin)!=NULL)
- fputs(line, f);
- fclose(f);
- return EXIT_SUCCESS;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement