-2

Here is my code:

typedef struct contact
{
    char name[20];
    char surname[20];
    char phoneNumber[13];
    char email[50];
} Contact;

int dataBaseQuantity = 0;

FILE *f = fopen(".bin", "ab+");
if(f != NULL)
{
    dataBaseQuantity++;
    fwrite(&dataBaseQuantity, sizeof(int), 1 ,f);
    fwrite(&input.name, sizeof(char), strlen(input.name), f);
    fwrite(&input.surname, sizeof(char), strlen(input.surname), f); 
    fwrite(&input.phoneNumber, sizeof(char), strlen(input.phoneNumber), f);
    fwrite(&input.email, sizeof(char), strlen(input.email), f);
}

cat .bin returns SQUAREIgnasKubilius+37065555555Ku@Ku.lt.

Instead of a number I'm getting a square. Where is the problem?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Sangi
  • 1
  • 1
  • 5
  • And I cannot fread properly an int – Sangi Dec 17 '17 at 14:30
  • 2
    A few problems here, but thats fine , you seem to be learning. You can read or write the struct in one fread / fwrite command. Do not use strlen as your data (name for example) is exactly 20 bytes long, you want to read/write 20 bytes not strlen – Grantly Dec 17 '17 at 14:32
  • 2
    `sizeof( char )` is one by definition. – Andrew Henle Dec 17 '17 at 14:37
  • `&input.name` -> `input.name`, etc... – Antti Haapala -- Слава Україні Dec 17 '17 at 14:38
  • 1
    https://stackoverflow.com/questions/216066/what-exactly-causes-binary-file-gibberish – Antti Haapala -- Слава Україні Dec 17 '17 at 14:39
  • Read carefully the documentation of every [IO function](http://en.cppreference.com/w/c/io) that you are using. Compile withj all warnings & debug info `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve your code to get no warnings. **Use the debugger `gdb`**. Think to understand the behavior of your program. Improve it. Repeat till satisfied – Basile Starynkevitch Dec 17 '17 at 14:39
  • The program asks to write your name, surname, phone number and email. the max lengths of these are in struct for example name - 20 chars. – Sangi Dec 17 '17 at 14:39
  • 1
    Write the whole struct in one go, but be aware that if you write a struct you will not necessarily be able to reliably read the data on another machine, or if you use a different compiler. – William Pursell Dec 17 '17 at 14:40
  • BTW, you might want to read about [serialization](https://en.wikipedia.org/wiki/Serialization). You could care about [portability](https://en.wikipedia.org/wiki/Software_portability), e.g. to system of different [endianness](https://en.wikipedia.org/wiki/Endianness). Then you might consider textual -and more portable- data formats, such as [JSON](http://json.org/). Look also into [databases](https://en.wikipedia.org/wiki/Database), perhaps [sqlite](http://sqlite.org/) – Basile Starynkevitch Dec 17 '17 at 14:43

1 Answers1

3

The problem is that you're using fwrite, which writes binary data. Thus, when you write an integer using fwrite, it does not format it as a string - instead, it writes the raw bytes that make up the integer. In this case, you're storing the number 1, which, as a sequence of bytes (assuming 4-byte little-endian ints), is (in hex) 01 00 00 00.

When you cat the file, it "automatically" removes the null bytes (more accurately, the terminal/console ignores them), but the 01 byte is printed as an unknown character, giving you the square you see.

There is no inherent "problem" with how you're writing the data, but you can't just use cat to display the data, since it works with text files, not binary.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57