C Reference Cheat Sheet Page 8

ADVERTISEMENT

C Reference Cheat Sheet
by
Ashlyn Black
via
File Input/​ O utput (cont)
File Input/​ O utput (cont)
Sets current file position. Returns false is
Uses fgets to limit the input length, then
fseek(fptr,
fgets(strName,
successful, true otherwise. The offset is a
uses sscanf to read the resulting string
offset, origin);
length, fptr);
long integer type.
in place of scanf. (safe)
sscanf(strName, "%d",
Origins
&x);
Beginning of file.
SEEK_SET
Binary
Current position in file.
Reads a number of elements from
SEEK_CUR
fread(void *ptr,
fptr to array *ptr. (safe)
sizeof(element),
End of file.
SEEK_END
number, fptr)
Utilities
Writes a number of elements to file
fwrite(void *ptr,
Tests end-of-file indicator.
feof(fptr)
fptr from array *ptr.
sizeof(element),
Renames a file.
rename(strOldName,
number, fptr)
strNewName)
Safe functions are those that let you specify the length of the input.
Deletes a file.
remove(strName)
Unsafe functions do not, and carry the risk of memory overflow.
Characters
Placeh​ o lder Types (f/printf And f/scanf)
Returns character read or EOF if
fgetc(fptr)
unsucc​ e ssful. (safe)
printf("%d%d...", arg1, arg2...);
Returns character written or EOF if
fputc(int c, fptr)
Type
Example
Description
unsucc​ e ssful.
%d or %i
Signed decimal integer.
-42
Strings
Unsigned decimal integer.
%u
42
Reads n-1 characters from file fptr into string
fgets(char *s, int
Unsigned octal integer.
s. Stops at EOF and \n. (safe)
%o
52
n, fptr)
%x or %X
Unsigned hexadecimal
Writes string s to file fptr. Returns non-
2a or 2A
fputs(char *s,
integer.
ne​ g ative on success, EOF otherwise.
fptr)
%f or %F
Signed decimal float.
1.21
Formatted Data
%e or %E
1.21e+9 or 1.21E+9
Signed decimal w/ scientific
Same as scanf with additional file pointer
fscanf(fptr,
notation.
parameter. (unsafe)
format, [...])
%g or %G
1.21e+9 or 1.21E+9
Shortest representation of
Same as printf with additional file pointer
fprintf(fptr,
%f/%F or %e/%E.
parameter.
format, [...])
%a or %A
0x1.207c8ap+30 or
Signed hexadecimal float.
Alternative
0X1.207C8AP+30
A character.
%c
a
A character string.
%s
A String.
By Ashlyn Black
Published 28th January, 2015.
Sponsored by
Last updated 20th April, 2015.
Measure your website readability!
Page 8 of 13.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education