Posts

Showing posts with the label files

File Handling in C (Reading a file line by line)

Image
In this tutorial, we are working with the files in the C. File handling in any language requires Three main steps to follow Open the file in Read/Write mode. Do various operation that you want to do. Close the file. We are using fopen(file stream). there is some modes to open a file. Read mode  opens file into the read mode if file not found returns -1. Write mode opens file into the write mode if the file does not present creates a new file if there is previous data it will be overwritten. Append mode opens the file in the append mode and append data to it Without wasting much time lets start working. Create a New C file of whatever name you want mine is files.c. create the basic schema of C language. #include <stdio.h> int main(int argc, char const *argv[]) { /* code */ return 0; } Read From the file Create a FILE pointer to point a stream and open a file into the read mode using "r". you may need to include the string.h to your headers. the data of the file is #in

Easy File handling in C++ (working with files, fstream)

Image
In this tutorial, we are working with the files in the C++ or you have known to work with files as the file handling.  File handling in any language requires Three main step to follow Open the file in Read/Write mode. Do various operation that you want to do. Close the file. So In CPP, you can also use the traditional approach of C using FILE pointer and etc. We are using fstream (file stream). Without wasting much time lets start working. Create a New CPP file of whatever name you want mine is files.cpp. create the basic schema of cpp. #include <iostream> using namespace std; int main() { return 0; } To work with file add two more header first is #include<fstream>  , and second is #include<string> #include <fstream> #include <string> Now, let's start working with the writing into the files, to write into the file we need to open the file into the Write mode.  Which can be done by the ofstream class of cpp from the fstream header. string