Bifalgorithm post · Computer science one · Closing Files

How do you write and read binary data in C?

How do you write and read binary data in C? (Computer science one · Closing Files) — "int x = 10;\n\nFILE *binaryOutputFile = fopen(\"demo.bin\", \"w\");\n\n//…

Ideas in this flowchart

  • Binary file input and output in C
  • Writing binary data
  • Initialize the integer: int x = 10;
  • Open demo.bin for writing
  • Write one integer with fwrite
  • Reading binary data
  • Open input.bin for reading
  • Read one integer with fread
  • Allocate space for an array of 10 integers
  • Read the entire array with fread
Feed/

How do you write and read binary data in C?

No cover

Channel

Computer science one

1 page

"int x = 10;\n\nFILE *binaryOutputFile = fopen(\"demo.bin\", \"w\");\n\n//write a single int to a file:\n\nfwrite(&x, sizeof(int), 1, binaryOutputFile);\n\nFILE *binaryInputFile = fopen(\"input.bin\", \"r\");\n\n//read a single int from the file:\n\nfread(&x, sizeof(int), 1, binaryInputFile);\n\n//read an entire array from the file:\n\nint *a = (int *) malloc(10 * sizeof(int));\n\nfread(a, sizeof(int), 10, binaryInputFile);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.