0
I’m trying to record a struct in a file via language c. But whenever I run the program on xcode nothing is recorded in the archive.
I have tried the file permissions system, but there is nothing wrong.
code:
//
//  main.c
//  arquivos3
//
#include <stdio.h>
#include <stdlib.h>
struct Pessoa{
    char nome[20];
    unsigned int idade;
    float altura;
};
int main(int argc, const char * argv[]) {
    // insert code here...
    FILE* ptr;
    char* filename = "arq_teste.dat";
    char* modo_gravacao = "w";
    struct Pessoa pessoa = {"Fernando Santos", 42, 1.75};
    //Abre o arquivo para gravação; se ocorrer erro o programa aborta.
    if ((ptr = fopen(filename, modo_gravacao)) == NULL) {
        puts("Erro ao abrir o arquivo!");
        exit(1);
    }
    fwrite(&pessoa, sizeof(struct Pessoa), 1, ptr);
    fclose(ptr);
    return 0;
}
Your write mode should be "Wb" as you are not working with a text file.
– anonimo
It’s not in this code, but I’ve tried it this way.
– a--
Here it worked properly and generated a binary file.
– anonimo