简体   繁体   中英

Writing and Reading to and from a binary file C language

I was given an assignment to manage a sort of music store. In the database (which is saved as a .dat file) we have an artists name, and the album.

I'm having problems writing and reading the file. First thing is, even if I don't write anything, just create the file, and then open the file in notepad, i see gibberish and letters in chinese or japanese. Even if i write to the fail, or read from it using visual studio, this doesnt seem to change. Here's my code:

I opened the file with:

p=fopen("database.dat","w+");

The add item function:

void add_item(char* artist,char* record,FILE* p) //adds an item with artist and record to store
{
    item node;
    int item_size=sizeof(item);
    rewind(p);
    strcpy(node.artist,artist);
    strcpy(node.record,record);
    fwrite(&node,item_size,1,p);
    printf("Data added\n");
}

item is the struct that is used to define a single item in the store. it has 2 fields, string artist and string record.

typedef struct item
{
    char artist[100],record[100];
}item;

This is for reading:

void print_file(FILE* p) //print the entire file
{
    int size=sizeof(item);
    item node;
    rewind(p);
    while(!feof(p))
    {
        fread(&node,size,1,p);
        printf("%s - %s\n",node.artist,node.record);
    }
}

if i use print_file, i see gibberish, if i actually open the file with notepad, i see japanese.

Help! :D

edit: Just discovered something. If I add an item, and then read the file, i will read the item. But if i run the program again, and try to read the file immediatly, i see gibberish.

Problem is with "w+", it will truncate existing file to zero length and open it for write & read. When you run the program second time this is what happens and your read (before write) returns gibberish. More on fopen here

I'm guessing based on your symptoms that the strings are defined as char* and not char[] , which would be your problem. String IO on files doesn't work that way. Keep in mind that a string is actually of type char* , that is, a pointer to one or more 8-bit integers. When you write the string to the file, you're actually writing the value of the address itself, not the characters it points to.

You should use the fprintf() function to write strings to files:
http://www.cplusplus.com/reference/cstdio/fprintf/
And then fscanf() to read them:
http://www.cplusplus.com/reference/cstdio/fscanf/

In general, if you're going to use a struct as a file format, you can't put pointers in the struct. You could put char[] values, because they aren't handled the same as pointers, but that would require a hard limit on string size. This is one of the reasons why structs as file formats are discouraged by some-- better to read and write the values one at a time, handling strings and so on appropriately.

The reason it works when you read the file back immediately (before quitting your program) is because that pointer address is still valid for that string. But the string itself never got written into the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM