Scroll Down to See All
abortabsacosasctimeasctime_rasinassertatanatan2atexitatofatoiatolbsearchbtowccalloccatclosecatgetscatopenceilclearerrclockcoscoshctimectime_rdifftimediverferfcexitexpfabsfclosefdopenfeofferrorfflushfgetcfgetposfgetsfgetwcfgetwsfprintffputcfputwsfreadfreefreopenfrexpfscanffseekfsetposftellfwidefwprintffwritefwscanfgetcgetchargetenvgetwcgetwchargmtimegmtime_rhypotisalnumisalphaisasciiisblankiscntrlisdigitisgraphislowerisprintispunctisspaceisupperiswalnumiswalphaiswblankiswcntrliswctypeiswdigitiswgraphiswloweriswprintiswpunctiswspaceiswupperiswxdigitisxdigitj0j1jnlabsldexpldivlocaleconvlocaltimelocaltime_rloglog10longjmpmallocmblenmbrlenmbrtowcmbsinitmbsrtowcsmbstowcsmbtowcmemchrmemcmpmemcpymemmovememsetmktimemodfnextafternextafterlnexttowardnexttowardlnl_langinfoperrorpowprintfputcputcharputenvputsputwcputwcharqsortquantexpd32quantexpd64quantexpd128quantized32quantized64quantized128samequantumd32raiserandrand_rreallocregcompregerrorregexecregfreeremoverenamerewindscanfsetbufsetjmpsetlocalesetvbufsignalsinsinhsnprintfsprintfsqrtsrandsscanfstrcasecmpstrcatstrchrstrcmpstrcollstrcpystrcspnstrerrorstrfmonstrftimestrlenstrncasecmpstrncatstrncmpstrncpystrpbrkstrptimestrrchrstrspnstrstrstrtodstrtod32strtod64strtod128strtofstrtokstrtok_rstrtolstrtoldstrtoulstrxfrmswprintfswscanfsystemtantanhtimetime64tmpfiletmpnamtoasciitolowertouppertowctranstowlowertowupperungetcungetwcva_argva_copyva_endva_startvfprintfvfscanfvfwprintfvfwscanfvprintfvscanfvsprintfvsnprintfvsscanfvswprintfvswscanfvwprintfvwscanfwcrtombwcscatwcschrwcscmpwcscollwcscpywcscpywcsftimewcslenwcsncatwcsncmpwcsncpywcspbrkwcsptimewcsrchrwcsrtombswcsspnwcsstrwcstodwcstod32wcstod64wcstod128wcstofwcstokwcstolwcstoldwcstombswcstoulwcsxfrmwctobwctombwctranswctypewcwidthwmemchrwmemcmpwmemcpywmemmovewmemsetwprintfwscanfy0y1yn



Function Details : fopen

FILE *fopen(const char * filename,const char * mode) ;

Return Type : FILE *

Pointer to FILE structure for file operations
Read about return values of fopen function .

1st Parameter Type : const char *

Pointer to read-only string

1st Parameter


If file doesn't exist:
With "r" mode: fopen() fails, returns NULL
With "w" mode: new file is created
With "a" mode: new file is created

fopen will create a new file in ALL modes EXCEPT those starting with "r"

2nd Parameter Type : const char *

Pointer to read-only string

2nd Parameter


Basic modes:
"r" - read only (file must exist)
"w" - write only (creates new/truncates existing)
"a" - append (creates new/preserves existing)
"r+" - read and write (file must exist)
"w+" - read and write (create new/truncates existing)
"a+" - read and append (creates new/preserves existing)
Additional flags:
"b" - binary mode (e.g., "rb", "wb", "ab")
"t" - text mode (usually default, rarely used explicitly)
Important notes:
Mode string is always constant ("r" not 'r')
Order matters for additional flags (use "rb" not "br")
Binary mode is crucial for Windows systems, optional but harmless on Unix

Read more about parameters of fopen in parameters section
The fopenfunction in C language opens the file specified by user defined filename, associates a stream with it using the given mode and returns a pointer to the stream.
fopen is a fundamental C library function that serves as the entry point for all file operations, required before any file reading or writing can occur. It works in tandem with fclose for proper file handling, and it returns FILE pointer that is used by the entire family of file operations like fread, fwrite, fprintf, fscanf, fgets, and fputs. What makes fopen special is its mode parameter that determines the type of operations allowed on the file (read, write, append) and its behavior with existing/non-existing files, as well as its automatic handling of different operating system file access methods through a unified stream interface.


To Summarize the Workflow:
  1. 1.
    Function attempts to open a file with specified name and mode
  2. 2.
    Takes two parameters: filename (path to file) and mode (type of access)
  3. 3.
    Returns FILE pointer on success, NULL on failure
  4. 4.
    Sets errno when operation fails
  5. 5.
    Creates new file for write modes if file doesn't exist
  6. 6.
    Supports various modes: r, w, a, r+, w+, a+ for read/write operations


Yes

No

Yes

No

Yes

No

Yes

No

fopen(const char *filename, const char *mode)

File exists?

Mode = r/r+?

Mode = w/w+?

Mode = a/a+?

Open existing file

Create new file

Open & truncate

Open for append

Return NULL Set errno

Return FILE pointer




Read more about return type and value offopen function in return section.
The fopenfunction takes 2 parameters:
  • const char * `filename`:
    If file doesn't exist:
    With "r" mode: fopen() fails, returns NULL
    With "w" mode: new file is created
    With "a" mode: new file is created

    fopen will create a new file in ALL modes EXCEPT those starting with "r"
  • const char * `mode`:
    Basic modes:
    "r" - read only (file must exist)
    "w" - write only (creates new/truncates existing)
    "a" - append (creates new/preserves existing)
    "r+" - read and write (file must exist)
    "w+" - read and write (create new/truncates existing)
    "a+" - read and append (creates new/preserves existing)
    Additional flags:
    "b" - binary mode (e.g., "rb", "wb", "ab")
    "t" - text mode (usually default, rarely used explicitly)
    Important notes:
    Mode string is always constant ("r" not 'r')
    Order matters for additional flags (use "rb" not "br")
    Binary mode is crucial for Windows systems, optional but harmless on Unix
The fopen function return value :
  • Success: Returns pointer to FILE object
  • Failure:
    Returns NULL and sets errno to indicate error:
    EACCES: Permission denied
    ENOENT: File doesn't exist (for "r" modes)
    EINVAL: Invalid mode argument
    EMFILE: Too many open files
    (and other system-specific errno values)
  • The FILE pointer returned on success is used for all subsequent operations on the file stream


Yes

No

Permission error

No file

Invalid mode

System limits

fopen(filename, mode)

Operation successful?

Check specific error

Return FILE pointer

Return NULL EACCES: Permission denied

Return NULL ENOENT: No such file ('r' modes)

Return NULL EINVAL: Invalid mode string

Return NULL EMFILE: Too many open files


Output

This example demonstrates how to use `fopen` to open a file for reading. It opens 'example.txt' in read mode, reads its contents line by line, and prints them to the console.