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,895 questions

51,826 answers

573 users

How to create, write, read, and remove a file in C

1 Answer

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

int main(void)
{
    const char* fname = "d:/info.txt";
    int status = EXIT_FAILURE;

    FILE* fp = fopen(fname, "w+");
    if (!fp) {
        perror("File opening failed");
        return status;
    }
    fputs("text to write to file\n", fp);
    rewind(fp);

    int ch;
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);
    }

    if (ferror(fp)) {
        puts("Error reading file");
    }
    else if (feof(fp)) {
        puts("End of file - success");
        status = EXIT_SUCCESS;
    }

    fclose(fp);
    remove(fname);

    return status;
}



/*
run:

text to write to file
End of file - success

*/

 



answered Apr 19, 2024 by avibootz

Related questions

1 answer 130 views
1 answer 137 views
1 answer 139 views
1 answer 151 views
151 views asked Dec 30, 2020 by avibootz
1 answer 156 views
156 views asked Dec 29, 2020 by avibootz
2 answers 195 views
...