1.Reading from a text file
int fgetc(FILE *stream)
Description
The function returns the next character of the stream as an unsigned char (converted to an int). It takes as parameters a pointer to the stream.
Returns
The read character from the stream or EOF if it encounters the end of file or if an error occurs.
Example
#include<stdio.h> int main(void) { FILE* f = NULL; char c; /*Opens the stream*/ f = fopen("file.txt", "rt"); if (f != NULL) { /*Reads the file character by character and prints the results*/ do { c = fgetc(f); if (c != EOF) { putchar(c); } } while (c != EOF); fclose(f); } else { puts("Could not open file"); } return 0; }
The program above reads a text file and prints its content character by character.
char* fgets(char* string, int n, FILE *stream)
Description
The function reads at most n-1 characters into the character array string, stopping if it encounters a newline character (which will be included in the array as well). A string terminator will be added to the array in the end ('\0').
Returns
The function returns a pointer to the character array if the operation was successful or NULL if an error was encountered.
Example
#include<stdio.h> int main(void) { const int BUFFER_SIZE = 200; FILE* f = NULL; char buffer[BUFFER_SIZE]; /*Opens the stream*/ f = fopen("file.txt","rt"); if(f!=NULL) { /*Reads the file line by line until EOF is encountered*/ while(feof(f)==0) { /*Reads a line*/ fgets(buffer,BUFFER_SIZE,f); /*Prints the line*/ puts(buffer); } fclose(f); } else { puts("Could not open file"); } return 0; }
int fscanf(FILE *stream, const char *format, ...)
Description
The function reads from the stream formatted data and assigns values to the subsequent arguments. It is very similar to int scanf(const char *format, ...) with the exception that you can specify the used stream. If you want to learn more, check the articles on console I/O and formatted strings.
Returns
The function returns the number of items who were converted and assigned. If it encounters the end of the file or if an error happens it will return EOF.
Example
We shall consider a file containing only integers which are separated by a space character:
#include<stdio.h> int main(void) { FILE* f = NULL; int number = 0; int status = 0; /*Opens the stream*/ f = fopen("file.txt","rt"); if(f!=NULL) { /*Reads every integer from the file and prints it*/ while(feof(f)==0) { /*Tries to read an integer*/ status = fscanf(f,"%d",&number); if(status!=EOF) { printf("%d ",number); } else { puts("Non-integer found"); } } fclose(f); } else { puts("Could not open file"); } return 0; }
2.Writing to a Text File
int fputc(int c, FILE *stream)
Description
The function writes the character c (represented as an integer) to the stream. It takes as parameters the character (c) and a pointer to the stream (stream).
Returns
The function returns the written character if the operation was successful. Otherwise it returns EOF.
Example
We shall consider a file containing only integers which are separated by a space character:#include<stdio.h> #include<string.h> int main(void) { FILE* f = NULL; int i; char sentence[] = "The bird is the word"; int size, status; /*Opens the stream*/ f = fopen("file.txt","wt"); if(f!=NULL) { /*Computes the length of the character array*/ size = strlen(sentence); /*Writes the character array character by character*/ for(i = 0; i<size; i++) { status = fputc(sentence[i],f); if(status == EOF) { puts("Error occurred while writing the file"); } } } else { puts("Could not open file"); } return 0; }
int fputs(const char *s, FILE *stream)
Description
The function writes the string s to the stream stream. The string doesn't need to co
Returns
The function returns a non-negative number if the operation was successful. Otherwise it returns EOF.
Example
#include<stdio.h> int main(void) { FILE* f = NULL; char sentence1[] = "The bird is the word\n"; char sentence2[] = "Bird! Bird! Bird!\n"; /*Opens the stream*/ f = fopen("file.txt","wt"); if(f!=NULL) { /*Writes the first sentence*/ fputs(sentence1,f); /*Writes the second sentence*/ fputs(sentence2,f); } else { puts("Could not open file"); } return 0; }
int fprintf(FILE *stream, const char *format, ...)
Description
The function writes to the stream formatted data. It is very similar to int printf(const char *format, ...), with the exceptions that you can specify the used stream. If you want to learn more, check the articles on console I/O and formatted strings.
Returns
The function returns the number of written characters if the operation was successful. Otherwise it returns a negative number.
#include<stdio.h> #define NAME_SIZE 60 typedef struct { char name[NAME_SIZE]; int age; }Person; int main(void) { FILE* f = NULL; Person john = {"John Doe", 30}; /*Opens the stream*/ f = fopen("file.txt","wt"); if(f!=NULL) { /*Writes formatted text*/ fprintf(f,"Name: %s | Age: %d",john.name, john.age); } else { puts("Could not open file"); } return 0; }
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!