forked from keineahnung2345/cpp-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_read_write_concurrently.c
37 lines (30 loc) · 983 Bytes
/
file_read_write_concurrently.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
//https://stackoverflow.com/questions/42046611/reading-and-writing-to-a-file-at-the-same-time-in-c
int main() {
char str[] = "This is gitbook.net";
/**
* https://www.tutorialspoint.com/cprogramming/c_file_io.htm
* w+: Opens a text file for both reading and writing.
* It first truncates the file to zero length if it exists,
* otherwise creates a file if it does not exist.
**/
FILE* fp = fopen("file.txt", "w+");
fwrite(str, 1, sizeof(str), fp);
fflush(fp);
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
printf("Wrote %d bytes\n", size);
//https://stackoverflow.com/questions/32366665/resetting-pointer-to-the-start-of-file/32366729
fseek(fp, 0, SEEK_SET);
char* fcontent = (char*)malloc(size);
// if we open the file with "w", then we cannot read from fp!!
fread(fcontent, 1, size, fp);
fclose(fp);
printf("We just wrote: %s\n", fcontent);
return 0;
}
/*
Wrote 20 bytes
We just wrote: This is gitbook.net
*/