🔸Input / Output
<stdio.h>
char functions (input)
int getc(FILE *stream); → read a single character from a file
example read from a file:
example read from stdin ( i.e: keyboard):
we can do it this way too:
we can do it with catching spaces:
int ungetc(int char, FILE *stream); → pushes the character char (an unsigned char) into the specified stream so that this is available for the next read operation.
char functions (output)
int putc(int char, FILE *fp) → write a single character to a file or stdout
takes 2 args:
1.character
2.file pointer
example redirecting the input to a file:
use the above program like this:
int fputc(int character, FILE *stream); → writes a character (an unsigned char) to the specified stream and advances the position indicator for the stream.
example write characters a-z in a file:
example count the number of characters and words in a file or from stdin:
to enter EOF press Ctrl+D keys
example convert uppercase to lowercase and vice versa in a file and stdin:
string functions(input)
ssize_t getline(char *buffer,size_t size, FILE *stream);
buffer → a pointer to a block allocated with malloc or calloc (type char **)
there is never a shortage of space cause it automatically enlarge the block of memory using realloc as needed ( getline is safe) returns the line read by getline
example:
int fscanf(FILE _fp, const char_** format [,argument,...]);** → same as printf but on a file. returns the number of arguments that are successfully read and assigned (on success), returns EOF if the end of the file is reached before any of the conversion specifications have been processed
fscanf(myFile, "%i", &i); → reads the next integer value from the file "myFile" and stores it in the variable i
example:
Formatting functions
int sprintf(char _string, const char_** format,...)** → used to write formatted output to a string. we can combine several data variables into a character array, instead of printing on the console we store the output to a char buffer
sprintf(string, "%d %c %f", value, c, fit);
first parameter is a char pointer for output(buffer) the function returns the number of characters stored in the string
this function is unsafe and vulnerable to buffer overflow
sscanf(const char _str, const char_** control_string [arg1,arg2,...]);** → allows to read formatted data from a string rather than stdin or keyboard
sscanf(buffer,"%s %d", name,&age);
first arg is a pointer to string from where to read the data returns the number of items read from the string and -1 if an error is encountered
example:
fflush() → used to flush/clean a file or buffer
for float and double values we can print a specific width of the value like this:
for integer input always use '&' before variable name
Printing system error messages
perror("error text"); → used for printing error messages followed by system stderr
returns the number of items that it successfully reads . while using scanf() to read a value for one of the basic variable types, prepend the variable name with an & sign . while using scanf() to read a string into a character array, don't use an & sign.example:
Last updated