Class Reference stdio.h

C file input and output for C++ macros. More...

Member

FILE * fopen(string path, string mode)
int fclose(FILE *fp)
int fputs(string s, FILE *fp)
string fgets(FILE *fp, int maxLen)
int fread(void *ptr, int size, int nmemb, FILE *fp)
int fwrite(void *ptr, int size, int nmemb, FILE *fp)
int fgetc(FILE *fp)
int fputc(int c, FILE *fp)
int feof(FILE *fp)
int ferror(FILE *fp)
int fflush(FILE *fp)
void rewind(FILE *fp)
int fseek(FILE *fp, int offset, int whence)
int ftell(FILE *fp)
int puts(string s)

Detailed Description

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.

Example

#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);
}

Constants

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.

Member Function Documentation


FILE * stdio.h::fopen(string path, string mode)

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 write
    • add b for binary on some systems ("rb", "wb")

Returns: FILE * to use with the other functions, or NULL if the file could not be opened.


int stdio.h::fclose(FILE *fp)

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.


int stdio.h::fputs(string s, FILE *fp)

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.


string stdio.h::fgets(FILE *fp, int maxLen)

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.


int stdio.h::fread(void *ptr, int size, int nmemb, FILE *fp)

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).


int stdio.h::fwrite(void *ptr, int size, int nmemb, FILE *fp)

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.


int stdio.h::fgetc(FILE *fp)

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.


int stdio.h::fputc(int c, FILE *fp)

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.


int stdio.h::feof(FILE *fp)

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.


int stdio.h::ferror(FILE *fp)

Asks whether a previous operation set the file’s error flag.

Parameters:

  • fp (FILE *).

Returns: non-zero if there was an error, otherwise 0.


int stdio.h::fflush(FILE *fp)

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.


void stdio.h::rewind(FILE *fp)

Moves the read/write position back to the start of the file (same as fseek(fp, 0, SEEK_SET)).

Parameters:

  • fp (FILE *).

Returns: nothing.


int stdio.h::fseek(FILE *fp, int offset, int whence)

Moves the read/write position.

Parameters:

  • fp (FILE *).
  • offset — how far to move, in bytes (can be negative).
  • whenceSEEK_SET (from start), SEEK_CUR (from here), SEEK_END (from end).

Returns: 0 on success, non-zero on error. NULL file returns -1.


int stdio.h::ftell(FILE *fp)

Current position in the file, in bytes from the start.

Parameters:

  • fp (FILE *).

Returns: the position, or -1 on error.


int stdio.h::puts(string s)

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.