-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.java
More file actions
30 lines (24 loc) · 895 Bytes
/
Runner.java
File metadata and controls
30 lines (24 loc) · 895 Bytes
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
package main;
import java.lang.foreign.*;
class Runner {
public static void main(String[] args) {
try (Arena allocator = Arena.ofConfined()) {
var libc = new LibC(allocator);
MemorySegment filePtr = libc.fopen("result.txt", "r");
if (filePtr.equals(MemorySegment.NULL)) {
System.out.println("File could not be opened or invoke call failed");
return;
}
MemorySegment buffer = allocator.allocate(100);
MemorySegment result;
for (; ; ) {
result = libc.fgets(buffer, 100, filePtr);
if (result.equals(MemorySegment.NULL)) break;
System.out.print(buffer.getString(0));
}
if (libc.fclose(filePtr) != 0) {
System.out.println("File was not closed");
}
}
}
}