How To Write to File on C++
How To Read and Write Files in C++
This C++ feature allows users to read and write from files, this feature is available in many coding languages. The file contents are added using an input device such as a keyboard, the user's input is then added into a file via the program. When a user wants the code to read from a file, the file goes through the program again and outputs the file contents. This post will teach users how to create a file, read from a file and append a file specifically text files.
There are individual code parameters and declerations to open files, close files, read from files, write files and append files, this post will go over the ways to execute them. These are called File Access Modes, these are arguments that have set parameters for if the file exist or doesnt exist.
Write File
FILE* file; //Variable decleration for file access
file = fopen("Filename.txt" , "w"); //Filename in the first argument and access mode in the second argument.
fprintf(file, "I wrote into this file");
fclose(file); //Closes file
}
fopen is a function that takes in two arguments and opens a file named after the argument "Filename" ending in txt.
The "w" is a file access mode which helps the code know how to open it and what to do if the file exist, The "w" mode opens the file and puts the file into write mode but erases all the contents of the file if it already exists, otherwise if the file doesnt exist it will create a brand new file. fclose closes the file, all the data that has been loaded but not written is taken to the operating system and all unreasd data is discarded. The fprintf function takes in characters and prints it into the file being declared in the first argument.
Read File
FILE* file; //Variable decleration for file access
char fileLine[81]
file = fopen("Filename.txt" , "r"); //Filename in the first argument and access mode in the second argument.
while(!feof(file)) {
fgets(fileLine, 81, file);
puts(fileLine);
}
fclose(file); //Closes file
The "r" file access mode opens the file in read mode and if the file exist it reads the file content from the start, If the file doesnt exist the user will be met with an error. "fgets" function prints out the contents from the file, the first argument specifys the array youre reading into and the second argument in the function specifys how much of the array you will be reading from or how much characters you will be reading from the file. The arguments in the while loop means to loop while there is no end of file.
Append File
FILE* file; //Variable decleration for file access
file = fopen("Filename.txt" , "a"); //Filename in the first argument and access mode in the second argument.
fprintf(file, "I wrote into this file");
fclose(file); //Closes file
The "a" file access mode opens the file in append mode and starts writing contents from the end of the code if the file already exists but if the file doesnt exist it will automatically create a new file.
Comments
Post a Comment