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

51,780 answers

573 users

How to write a dynamic number of strings to a binary file in C

2 Answers

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

int writeToFile(char* filename, char* first, ...) {
    FILE* fp = fopen(filename, "wb");
    
    if (!fp) { fputs("File open error", stderr); return 0; }

    va_list vl;

    va_start(vl, first);
    char* s = first;

    while (s != 0) {
        vfprintf(fp, s, vl);
        s = va_arg(vl, char*);
        
    }

    fclose(fp);
    va_end(vl);

    return 1;
}

int readFile(char* filename) {
    FILE* fp = fopen(filename, "rb");
    if (!fp) { fputs("File open error", stderr); return 0; }

    fseek(fp, 0, SEEK_END);
    long file_size = ftell(fp);
    rewind(fp); // sets fp to the beginning of the file

    char* buffer = (char *)malloc(sizeof(char) * file_size + 1);
    if (!buffer) { fputs("Memory allocation error", stderr); return 0; }

    size_t rv = fread(buffer, 1, file_size, fp); // copy the file content into a buffer
    if (rv != file_size) { fputs("File reading error", stderr); return 0; }

    buffer[file_size] = '\0';
    printf("file content = %s", buffer);

    fclose(fp);
    free(buffer);

    return 1;
}


int main()
{
    char* filename = "d:\\data.bin";

    writeToFile(filename, "c", "c++", "java", "python");

    readFile(filename);

    return 0;
}



/*

run:

file content = cc++javapython

*/

 



answered Apr 26, 2024 by avibootz
edited Apr 26, 2024 by avibootz
0 votes
#include <stdio.h> 
#include <stdarg.h>
#include <stdlib.h> 

int writeToFile(char* filename, const char* format, ...) {
    FILE* fp = fopen(filename, "wb");

    if (!fp) { fputs("File open error", stderr); return 0; }

    va_list vl;
    va_start(vl, format);

    vfprintf(fp, format, vl);

    fclose(fp);
    va_end(vl);

    return 1;
}

int readFile(char* filename) {
    FILE* fp = fopen(filename, "rb");
    if (!fp) { fputs("File open error", stderr); return 0; }

    fseek(fp, 0, SEEK_END);
    long file_size = ftell(fp);
    rewind(fp); // sets fp to the beginning of the file

    char* buffer = (char*)malloc(sizeof(char) * file_size + 1);
    if (!buffer) { fputs("Memory allocation error", stderr); return 0; }

    size_t rv = fread(buffer, 1, file_size, fp); // copy the file content into a buffer
    if (rv != file_size) { fputs("File reading error", stderr); return 0; }

    buffer[file_size] = '\0';
    printf("file content = %s", buffer);

    fclose(fp);
    free(buffer);

    return 1;
}


int main()
{
    char* filename = "d:\\data.bin";

    writeToFile(filename, "%s %s %s %s", "c", "c++", "java", "python");

    readFile(filename);

    return 0;
}



/*

run:

file content = c c++ java python

*/

 



answered Apr 26, 2024 by avibootz
edited Apr 26, 2024 by avibootz

Related questions

2 answers 163 views
2 answers 123 views
1 answer 245 views
245 views asked Oct 17, 2014 by avibootz
1 answer 130 views
1 answer 136 views
1 answer 169 views
169 views asked Dec 29, 2020 by avibootz
...