Friday, June 26, 2015

File Handling in C++



Hey, what's up guys??

Today we will look in to File Handling in C++.

Before read or write to files we must know what are the classes or the header files that we must include to our program.
There are three classes:

  • ofstream = Class to write in files
  • ifstream = Class to read from files
  • fstream  = Class to both read and write from and to files
Including the header file to the program can be done as follows:
#include<fstream>

After including the header file we can use all the methods declared in that class. Since C++ is object oriented we must have to create a object from one of the above classes to use the functions.

At first we are using the class to read a file, so its better to create the object from the ofstream class.

ofstream MyObj; //Created a object named MyObj

After creating the object we must first use the open() function to open the file for reading.(or writing)
This function accepts two parameters. First on is the file name and second one is the mode that we want to open the file. Second parameter is optional.

MyObj.open("myTextFile.txt"); //opened a text file named myTextFile.txt in the readable form

After opening the file in the readable format lets write something to the file. First lets define a string variable named myContent. And let's read from the file.

String myContent;
MyObj>>myContent; //Reading the whole file content  and storing in a single string variable

Or else we can read the file line by line.

while(getline(MyObj,myContent)){
    cout<<myContent<<endl;  //this will print the content in the file line by line to the screen till the end of the line reaches
}


Well there, we read the content from a file. Let's try to write in to a file in C++.

To write to a file, first we must open the file in writable mode. We can do it by creating an object from the ofstream and opening the file using open() method.

ofstream myObj2; //created an object using ofstream class
myObj2.open("myTextFile.txt"); //opened a text file named myTextFile.txt in the writable mode




After opening the file in writable form we can simply pass values or variables and write values and content to the file.

myObj2<<"This line will be added to the text file"<<endl;

myObj2<<myContent<<endl;

To check whether the file is still open in the program we can use is_open() method. This method does not accept any arguments and will return a boolean value.

if(myObj2.is_open()){
    //body of the if condition
}


After we read and write and using the file we must close the file from our program. Otherwise it will hold some memory space and will make the program inefficient and affect the system resources badly. To close the filr from the program we can use close() function in C++.

myObj2.close();

These are the basic functions used in file handling in C++.



Past Topics:-
Like Us on Facebook :-



Comment box is open for your comments, ideas, suggestions and complaints!

Sunday, June 21, 2015

File Read and Write in PHP


Hello guys, how are you?

Today we will look in to the File Operations in PHP.

Whether to read or a write to a file, first of all we must open the file using PHP. The function that we use to open a file in PHP is fopen() function. This function accepts two parameters:-

  • file path
  • the mode that the file should be opened
$fp = fopen("input.txt", "r");

Here file is opened and assigned to a variable. This variable is usually called as file pointer. This will make our operations easy.



Modes that we can open a file :-
  • r - read only
  • w - write only(existing data will be re-written)
  • a - write only(new text will be added to the end of the existing text)
  • x - creates a new file for write only
  • r+ - opens a file for read and write
  • w+ - opens a file for read and write
  • a+ - opens a file for read and write
  • x+ - creates a new file for read and write

After opening the file in read  mode we can read the text which is existing in the file and assign it to a PHP variable. To do that we can use fread()  function. This function accepts two parameters.
  • file path
  • maximum bytes to read
$content = fread("input.txt",1024);

Or else we can use the file pointer variable as parameter. If we use that as a parameter it will read the file till it reach the end of the file.

$content = fread($fp);

If we want to read a single line of the file we can do it using fgets() function. This function also accepts two parameters. 
  • file path
  • file open mode
$content = fgets("input.txt","r");
$content = fread($fp);

In some context we maybe want to check whether the file pointer has reached the end of the file. So we can do it using feof() function. This function accepts one parameter which is the file pointer variable.

if(feof($fp)){
     echo("End of the file has reached!");
}

After opening a file for reading or writing purpose we have to close the file. Otherwise the file pointer will keep the file open and that will lead for errors and unexpected problems. To close a file we can use fclose() function. This function accepts one parameter which is also the file pointer variable.

fclose($fp);

These are the basic and most used file handling functions in PHP and there are plenty of more functions available to do various tasks.
Below link will show some more functions :-


Past Topics :-
Like us on Facebook:



Comment box is open for your comments, ideas and suggestions!