Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library –

C Standard Library -

Introduction

stdio .h The header file defines three variable types, some macros, and various functions for performing input and output.

is a header file in the C Standard Library that defines various functions and types for handling files and standard input/output streams.

Library variables

The variable types defined in the header file stdio.h are listed below:

No. Variable & Description
1 **size_t **
This is an unsigned integer type; it is the sizeof The result of the keyword, representing the size of an object.
2 **FILE **
File stream type, an object type suitable for storing information about a file stream.
3 **fpos_t **
File position type, an object type suitable for storing any position within a file.

Library macros

The macros defined in the header file stdio.h are listed below:

No. Macro & description
1 NULL
This macro is the value of a null pointer constant.
2 _IOFBF, _IOLBF and ** _IONBF **
These macros expand to integer constant expressions with specific values and are suitable for use as setvbuf the third argument of the function.
3 BUFSIZ
This macro is an integer that represents setbuf the buffer size used by the function.
4 EOF
This macro is a negative integer indicating that the end of the file has been reached.
5 FOPEN_MAX
This macro is an integer that represents the number of files the system can open simultaneously.
6 FILENAME_MAX
This macro is an integer that represents the maximum length of a filename that a character array can store. If the implementation imposes no limit, the value should be the recommended maximum.
7 L_tmpnam
This macro is an integer that represents the maximum length of a temporary filename created by the tmpnam function that a character array can store.
8 SEEK_CUR, SEEK_END and SEEK_SET
These macros are used in the fseek function to locate different positions within a file.
9 **TMP_MAX **
This macro is the maximum number of unique filenames that the tmpnam function can generate.
10 stderr, stdin and **stdout **
These macros are pointers to type FILE, corresponding to the standard error, standard input, and standard output streams, respectively.

Library functions

The functions defined in the header file stdio.h are listed below:

To better understand the functions, study them in the following order, because the file created in the first function is used by the subsequent functions.

No. Function & Description
1 int fclose(FILE *stream)
Closes the stream. Flushes all buffers.
2 void clearerr(FILE *stream)
Clears the end-of-file and error indicators of the given stream.
3 int feof(FILE *stream)
Tests the end-of-file indicator of the given stream.
4 int ferror(FILE *stream)
Tests the error indicator of the given stream.
5 int fflush(FILE *stream)
Flushes the output buffer of the stream.
6 int fgetpos(FILE *stream, fpos_t *pos)
Gets the current file position of the stream and writes it to pos.
7 FILE *fopen(const char *filename, const char *mode)
Opens the file pointed to by filename using the given mode.
8 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Reads data from the given stream into the array pointed to by ptr.
9 FILE *freopen(const char *filename, const char *mode, FILE *stream)
Associates a new filename with the given open stream and closes the old file in the stream.
10 int fseek(FILE *stream, long int offset, int whence)
Sets the file position of the stream to the given offset; the argument offset means from the given whence position.
11 int fsetpos(FILE *stream, const fpos_t *pos)
Sets the file position of the given stream to the given position. The argument pos is the position given by the function fgetpos.
12 long int ftell(FILE *stream)
Return the current file position of the given stream stream.
13 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Writes data from the array pointed to by ptr to the given stream.
14 int remove(const char *filename)
Deletes the given filename so that it is no longer accessible.
15 int rename(const char *old_filename, const char *new_filename)
Changes the filename pointed to by old_filename to new_filename.
16 void rewind(FILE *stream)
Sets the file position to the beginning of the file for the given stream.
17 void setbuf(FILE *stream, char *buffer)
Defines how the stream stream should be buffered.
18 int setvbuf(FILE *stream, char *buffer, int mode, size_t size)
Another function that defines how a stream should be buffered.
19 FILE *tmpfile(void)
Creates a temporary file in binary update mode (wb+).
20 char *tmpnam(char *str)
Generates and returns a valid temporary filename that did not previously exist.
21 int fprintf(FILE *stream, const char *format, ...)
Send formatted output to the stream stream.
22 int printf(const char *format, ...)
Sends formatted output to the standard output stdout.
23 int sprintf(char *str, const char *format, ...)
Sends formatted output to a string.
24 int vfprintf(FILE *stream, const char *format, va_list arg)
Sends formatted output to the stream stream using an argument list.
25 int vprintf(const char *format, va_list arg)
Sends formatted output to standard output stdout using an argument list.
26 int vsprintf(char *str, const char *format, va_list arg)
Sends formatted output to a string using an argument list.
27 int fscanf(FILE *stream, const char *format, ...)
Read formatted input from the stream stream.
28 int scanf(const char *format, ...)
Reads formatted input from the standard input stdin.
29 int sscanf(const char *str, const char *format, ...)
Reads formatted input from a string.
30 int fgetc(FILE *stream)
Gets the next character (an unsigned char) from the specified stream and advances the position indicator.
31 char *fgets(char *str, int n, FILE *stream)
Reads a line from the specified stream and stores it in the string pointed to by str. When (n-1) characters are read, or when a newline character is read, or when the end of the file is reached, it stops, depending on the case.
32 int fputc(int char, FILE *stream)
Writes the character specified by char (an unsigned char) to the specified stream and advances the position indicator.
33 int fputs(const char *str, FILE *stream)
Write the string to the specified stream stream, without the null character.
34 int getc(FILE *stream)
Gets the next character (an unsigned char) from the specified stream and advances the position indicator.
35 int getchar(void)
Gets a character (an unsigned char) from standard input stdin.
36 char *gets(char *str)
Reads a line from standard input stdin and stores it in the string pointed to by str. It stops when a newline character is read or the end of the file is reached, whichever comes first.
37 int putc(int char, FILE *stream)
Writes the character specified by char (an unsigned char) to the specified stream and advances the position indicator.
38 int putchar(int char)
Writes the character specified by the char argument (an unsigned character) to the standard output stdout.
39 int puts(const char *str)
Writes a string to the standard output stdout up to, but not including, the null character. A newline character is appended to the output.
40 int ungetc(int char, FILE *stream)
Pushes the character char (an unsigned char) back onto the specified stream so that it is the next character to be read.
41 void perror(const char *str)
Outputs a descriptive error message to standard error stderr. First the string str is output, followed by a colon and then a space.
42 int snprintf(char *str, size_t size, const char *format, ...)
format string into str.

Example

The following are some uses of Example of the functions in stdio.h:

Opening and closing files:

Example

#include

int

main

(

)

{

    FILE

file

=

fopen

(

"example.txt"

,

"r"

)

;

   

if

(

file

==

NULL

)

{

       

perror

(

"Error opening file"

)

;

       

return

1

;

   

}

   

// Perform file operations

   

fclose

(

file

)

;

   

return

0

;

}

Read file contents:

Example

#include

int

main

(

)

{

    FILE

file

=

fopen

(

"example.txt"

,

"r"

)

;

   

if

(

file

==

NULL

)

{

       

perror

(

"Error opening file"

)

;

       

return

1

;

   

}

   

char

buffer

[

256

]

;

   

while

(

fgets

(

buffer

,

sizeof

(

buffer

)

,

file

)

!=

NULL

)

{

       

printf

(

"%s"

,

buffer

)

;

   

}

   

fclose

(

file

)

;

   

return

0

;

}

Write file contents:

Example

#include

int

main

(

)

{

    FILE

file

=

fopen

(

"example.txt"

,

"w"

)

;

   

if

(

file

==

NULL

)

{

       

perror

(

"Error opening file"

)

;

       

return

1

;

   

}

   

fprintf

(

file

,

"Hello, World!\n"

)

;

   

fclose

(

file

)

;

   

return

0

;

}

Notes

By understanding and using The functions provided make it convenient to perform file and standard input/output operations, allowing you to write more robust and flexible C programs.