In this guide, we will learn the features of text files and learn how to use them in C++ programming.
Opening and closing text files
A text file is a collection of strings with the character of a changeable length. Each line ends with the end marker of the string – a special pair of control characters, the codes of which are: 13 (carriage return) and 10 (newline). At the end of the file, there is always an end of file marker – a control character with code 26.
Opening is performed by the fopen() function. Its prototype is declared as follows:
FILE *fopen(const char *filename, const char *mode);
The function has the following parameters: filename – a string expression, its value must be a physical file name; mode – a string expression, its value is the mode of access to the file. The second parameter of fopen() sets the mode of file access and can take the following values: “r” – open file for reading; “w” – create a new file for writing; “a” – open file for appending data to end of file or create new file if it does not exist. Use “r+” to open an existing file for reading and writing; “w+” – create a new file for writing and reading; “a+” – open file in read mode and write new data to end of file or create new file if it does not exist.
Files are closed with the function fclose():
The function fclose() returns zero in case of successful file closure or EOF if an error occurs.
Reading and writing text files
Reading individual characters from the text file function fgetc() has the prototype:
int fgetc(FILE *stream);
Reading a text file row by row has the prototype:
char *fgets(char *str, int n, FILE *stream);
Here “str” is the buffer which stores a string read from the file; “n” is the maximum number of characters that were read; stream is a pointer to file which contains the string to read.
In the C/C++ languages, there are functions fputc(), and fputs () which write characters and strings. They have the following prototypes:
int fputc(int c, FILE *stream);
int fputs(const char *str, FILE *stream);
Here “c” is a character which is written to the file, “stream” is a pointer to a file, and “str” is a pointer to a string that is written to the text file.
The functions for reading text files:
int fscanf (FILE *stream, const char *format [,argument ]…);
int fprintf(FILE *stream, const char *format [,argument ]…);
These functions have parameters: “stream” – pointer to a text file; “format” – pointer to the string format, which specifies the transformation rules of symbols; the argument is the address of the arguments, and refers to program variables.
Now let’s write a program which will create a text file by entering lines from the keyboard, each line of the generated file, and find the longest word and append it to the end of the row. We believe that words are separated from each other by the arbitrary amount of whitespace:
Thanks for your attention!
The sample that you have read allows you to deal with your own assignment. There are a lot of samples that can help you with other topics in programming on our site. You can also get other types of help if samples are not enough – for example, contact us if you need help with Java programming. In this case, visit assignment.essayshark.com/java-help.html. There you can find an expert in Java programming who can help you with assignments in the field. You can finally end your struggle with homework forever. When an expert is dealing with your task, you can contact him or her and ask about the progress or any other question related to the order.
You will not find errors in the assignments completed by our experts. You can choose the most suitable expert to work on your assignment. Our service is used by students who don’t have the time or simply can’t handle tasks on their own. To get professional help, simply place an order on our site with your requirements and the deadline.