primer1 - Kreiranje datoteke:

#include <stdio.h>
#include <errno.h>
int main(void) {
    FILE *fp;
    fp = fopen ("write.txt","w");
    if (fp == NULL) {
        printf ("File not created okay, errno = %d\n", errno);
        return 1;
    }
    //fprintf (fp, "Hello, there.\n"); // if you want something in the file.
    fclose (fp);
    printf ("File created okay\n");
    return 0;
}

primer2 -ispis datoteke:

#include <stdio.h>

int main(void)
{             

    FILE *fp; 
    char ch;

    fp=fopen("write.txt","r");

    if(fp==NULL)
    {
       printf("Some problem in opening the file");
       return 1;
    } 
    else
    { 
        while((ch=fgetc(fp))!=EOF)
        {
            printf("%c",ch);
        }
    }

    fclose(fp);

    return 0;  
}
ZADATAK: Kreirati tekstualnu datoteku i prebrojati reči.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdio.h"
#include "ctype.h"
 
 
int main(void)
{
	FILE *dat=fopen("primer.txt","w");
	char tekst[80],znak;
	int brojac=0;
        
printf("Unesi jedan red teksta:\n"); gets (tekst); fputs(tekst,dat); fclose(dat);
dat=fopen("primer.txt","r"); while((znak=fgetc(dat))!=EOF) if(znak==' ') brojac++; printf("Broj reci u tekstualnoj datoteci je: %d\n",brojac); return 0; }


Last modified: Wednesday, 3 April 2019, 1:31 AM