Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to read text file using fgets, fscanf, fgetc and fread in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

#define SIZE 256

int main(void)
{
    FILE* fp;
    char buffer[SIZE];

    fp = fopen("d:\\data.txt", "r");
    if (!fp) return 1;

    puts("fgets: \n");
    while (fgets(buffer, SIZE, fp) != NULL) {
        printf("%s", buffer);
    }

    memset(buffer, 0, SIZE);
    fseek(fp, 0, SEEK_SET);

    puts("\nfscanf: \n");
    while (fscanf(fp, "%s", buffer) == 1) {
        printf("%s", buffer);
    }

    memset(buffer, 0, SIZE);
    fseek(fp, 0, SEEK_SET);

    int ch;
    puts("\n\nfgetc: \n");
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
    }
    
    fseek(fp, 0, SEEK_SET);

    puts("\n\nfread: \n");
    while (fread(buffer, sizeof(buffer[0]), 1, fp) == 1) {
        printf("%s", buffer);
    }

    fclose(fp);

    return 0;
}



/*
run: 

fgets:

C is a general-purpose programming language.
It was created in the 1970s by Dennis Ritchie,
and remains very widely used and influential.

fscanf:

Cisageneral-purposeprogramminglanguage.Itwascreatedinthe1970sbyDennisRitchie,andremainsverywidelyusedandinfluential.

fgetc:

C is a general-purpose programming language.
It was created in the 1970s by Dennis Ritchie,
and remains very widely used and influential.


fread:

C is a general-purpose programming language.
It was created in the 1970s by Dennis Ritchie,
and remains very widely used and influential.


*/

 



answered Jun 22, 2024 by avibootz
edited Jun 22, 2024 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

#define SIZE 256

int main(void)
{
    FILE* fp;
    char buffer[SIZE];

    fp = fopen("d:\\data.txt", "r");
    if (!fp) return 1;

    puts("fgets: \n");
    while (fgets(buffer, SIZE, fp) != NULL) {
        printf("%s", buffer);
        puts("------------------------------------------------");
    }

    memset(buffer, 0, SIZE);
    fseek(fp, 0, SEEK_SET);

    puts("\nfscanf: \n");
    while (fscanf(fp, "%s", buffer) == 1) {
        printf("%s", buffer);
        printf("%s", "---");
    }

    memset(buffer, 0, SIZE);
    fseek(fp, 0, SEEK_SET);

    int ch;
    puts("\n\nfgetc: \n");
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
        printf("%s", "-");
    }
    
    fseek(fp, 0, SEEK_SET);

    puts("\n\nfread: \n");
    while (fread(buffer, sizeof(buffer[0]) * 3, 1, fp) == 1) {
        printf("%s", buffer);
        printf("%s", "--");
    }

    fclose(fp);

    return 0;
}



/*
run: 

fgets:

C is a general-purpose programming language.
------------------------------------------------
It was created in the 1970s by Dennis Ritchie,
------------------------------------------------
and remains very widely used and influential.
------------------------------------------------

fscanf:

C---is---a---general-purpose---programming---language.---It---was---created---in---the---1970s---by---Dennis---Ritchie,---and---remains---very---widely---used---and---influential.---

fgetc:

C- -i-s- -a- -g-e-n-e-r-a-l---p-u-r-p-o-s-e- -p-r-o-g-r-a-m-m-i-n-g- -l-a-n-g-u-a-g-e-.-
-I-t- -w-a-s- -c-r-e-a-t-e-d- -i-n- -t-h-e- -1-9-7-0-s- -b-y- -D-e-n-n-i-s- -R-i-t-c-h-i-e-,-
-a-n-d- -r-e-m-a-i-n-s- -v-e-r-y- -w-i-d-e-l-y- -u-s-e-d- -a-n-d- -i-n-f-l-u-e-n-t-i-a-l-.-
-

fread:

C i--s a-- ge--ner--al---pur--pos--e p--rog--ram--min--g l--ang--uag--e.
--It --was-- cr--eat--ed --in --the-- 19--70s-- by-- De--nni--s R--itc--hie--,
a--nd --rem--ain--s v--ery-- wi--del--y u--sed-- an--d i--nfl--uen--tia--l.
--

*/

 



answered Jun 23, 2024 by avibootz
edited Jun 23, 2024 by avibootz

Related questions

2 answers 134 views
1 answer 126 views
1 answer 158 views
1 answer 158 views
2 answers 263 views
...