C file input and output for C++ macros. More...
C file input and output for C++ macros. Load with #include <stdio.h> or #include <cstdio> (same plugin). Available from LayoutEditor 20260918.
A FILE * is a pointer to an open file. You get it from fopen. You must fclose it when you are done. If fopen fails, you get NULL — always check before using the pointer.
There is no printf / scanf. Write text with fputs / fputc / puts, or use cout and the LayoutEditor file class.
fgets is not the standard C form fgets(buffer, n, fp). Here you call fgets(fp, maxLen) and it returns a string.
#include <stdio.h>
int main(){
string path = file::tempPath() + "/le_stdio_example.txt";
FILE *fp = fopen(path, "w");
if (fp == NULL){
cout("cannot write\n");
return 1;
}
fputs("hello stdio\n", fp);
fclose(fp);
fp = fopen(path, "r");
string line = fgets(fp, 64);
fclose(fp);
cout("read: ", line);
}
| Name | Meaning |
|---|---|
NULL |
empty pointer (fopen failed) |
EOF |
end of file or error for some functions (typically -1) |
SEEK_SET |
fseek: offset is from the start of the file |
SEEK_CUR |
fseek: offset is from the current position |
SEEK_END |
fseek: offset is from the end of the file |
FILE is an opaque type: write FILE *fp, do not try to look inside it.
Opens a file on disk.
Parameters:
path (string) — full or relative file name, for example "/tmp/out.txt".mode (string) — how to open:
"r" — read an existing file (fails if it does not exist)"w" — write; creates the file or overwrites it"a" — append (write at the end)"r+", "w+", "a+" — read and writeb for binary on some systems ("rb", "wb")Returns: FILE * to use with the other functions, or NULL if the file could not be opened.
Closes a file opened with fopen. Always close files you opened.
Parameters:
fp (FILE *) — the pointer from fopen.Returns: 0 on success, EOF on error. Closing NULL returns EOF.
Writes a string to the file. It does not add a newline unless s already contains \n.
Parameters:
s (string) — the text to write.fp (FILE *) — an open file opened for writing.Returns: a non-negative value on success, EOF on error.
Reads one line (or up to maxLen - 1 characters) from the file, including the newline if it fits.
Parameters:
fp (FILE *) — an open file opened for reading.maxLen (int) — maximum length of the returned string. If you pass less than 2, 256 is used. The upper limit is 1 048 576.Returns: string with the bytes read. Empty string on end-of-file or error (check feof / ferror).
This is not fgets(buffer, n, fp) from desktop C.
Reads raw bytes into a memory block. This is an advanced call: ptr must already point to enough memory. Beginners should prefer fgets / fgetc or the file class.
Parameters:
ptr — destination memory.size (int) — size of one element in bytes.nmemb (int) — number of elements to read.fp (FILE *) — open file.Returns: int — how many elements were actually read (can be less than nmemb at end of file).
Writes raw bytes from a memory block. Same remark as fread: advanced.
Parameters:
ptr — source memory.size (int) — size of one element in bytes.nmemb (int) — number of elements to write.fp (FILE *).Returns: int — how many elements were written.
Reads one byte from the file.
Parameters:
fp (FILE *) — open for reading.Returns: int — the byte value 0…255, or EOF at end of file / on error.
Writes one byte to the file.
Parameters:
c (int) — byte to write (only the lowest 8 bits are used).fp (FILE *) — open for writing.Returns: the byte written, or EOF on error.
Asks whether the last read hit the end of the file.
Parameters:
fp (FILE *).Returns: non-zero if end of file was reached, otherwise 0. If fp is NULL, returns 1.
Asks whether a previous operation set the file’s error flag.
Parameters:
fp (FILE *).Returns: non-zero if there was an error, otherwise 0.
Forces buffered output to be written to disk now.
Parameters:
fp (FILE *) — usually a file opened for writing.Returns: 0 on success, EOF on error.
Moves the read/write position back to the start of the file (same as fseek(fp, 0, SEEK_SET)).
Parameters:
fp (FILE *).Returns: nothing.
Moves the read/write position.
Parameters:
fp (FILE *).offset — how far to move, in bytes (can be negative).whence — SEEK_SET (from start), SEEK_CUR (from here), SEEK_END (from end).Returns: 0 on success, non-zero on error. NULL file returns -1.
Current position in the file, in bytes from the start.
Parameters:
fp (FILE *).Returns: the position, or -1 on error.
Writes a string to the console (standard output) and adds a newline. Useful for a quick print; cout is more flexible.
Parameters:
s (string) — the text.Returns: a non-negative value on success, EOF on error.
Python: open(), pathlib. The file class is available in both languages.