C file input and output for C++ macros. More...
C file input and output for C++ macros. Load with #include
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: declare a FILE pointer, do not try to look inside it.
Opens a file on disk.
Parameters:
Returns: FILE pointer 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.
Takes fp (FILE pointer), 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.
Takes s (string), the text to write, and fp (FILE pointer), 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.
Takes fp (FILE pointer), an open file opened for reading, and 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.
Takes ptr, destination memory, size (int), size of one element in bytes, nmemb (int), number of elements to read, and fp (FILE pointer), 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.
Takes ptr, source memory, size (int), size of one element in bytes, nmemb (int), number of elements to write, and fp (FILE pointer).
Returns: int — how many elements were written.
Reads one byte from the file.
Takes fp (FILE pointer), open for reading.
Returns: int — the byte value 0…255, or EOF at end of file / on error.
Writes one byte to the file.
Takes c (int), byte to write (only the lowest 8 bits are used), and fp (FILE pointer), open for writing.
Returns: the byte written, or EOF on error.
Asks whether the last read hit the end of the file.
Takes fp (FILE pointer).
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.
Takes fp (FILE pointer).
Returns: non-zero if there was an error, otherwise 0.
Forces buffered output to be written to disk now.
Takes fp (FILE pointer), 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)).
Takes fp (FILE pointer).
Returns: nothing.
Moves the read/write position.
Takes fp (FILE pointer), offset, how far to move, in bytes (can be negative), and 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.
Takes fp (FILE pointer).
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.
Takes 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.