'e and C languages writing to the same txt file
Is there is a way to write to txt file from an e (hardware language) code and then to write to the same file from a C code ?
Solution 1:[1]
In general, access to files is controlled by the Operating System.
Any number of programs, running on any number of separate processes can access the same file if they use the locking & synchronization methods provided by the Operating System.
This will typically involve opening the file in a Shared or Exclusive mode, opening it for Reading, Writing or both, and setting buffering options. It may also involve sharing a lock-mechanism, such as a mutex, between different programs.
Solution 2:[2]
You can have C code like following:
static FILE *f = NULL;
void cwrite() {
if (f == NULL)
f = fopen("ec.txt", "a");
fprintf(f, "print from C\n");
}
And then use it from e together with e write:
routine cwrite();
extend sys {
!f: file;
run() is also {
f = files.open("ec.txt", "a", "Text file");
for i from 0 to 100 {
files.write(f, "print from e");
cwrite();
};
};
};
However, the tricky part is that on linux level, fopen in C and files.open in e create separate file descriptors for exactly the same file, and this can lead to very weird result.
To make it synced, you should either keep your file closed when not writing (which might mean unneeded performance overhead), or really write only from one language, and when you need to write from another - send it as a string to the one that actually does that, you only need to define some simple API for that.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | abelenky |
| Solution 2 | Rodion Melnikov |
