Creating a Directory and read/write to file in Directory in C++

bashamsc's Avatar author of Creating a Directory and read/write to file in Directory in C++
This is an article on Creating a Directory and read/write to file in Directory in C++ in C++.
Rated 5.00 By 1 users
I will explain how to create a directory in linux using a c++ program.

The program to create a directory is as follows:

Code: Cpp
#include<iostream.h>
#include<sys/stat.h>
#include<sys/types.h>
using namespace std;

main()
{

    if(mkdir("pathname",0777)==-1)//creating a directory
    {
        cerr<<"Error :  "<<strerror(errno)<<endl;
        exit(1);
    }

}
This program will create a directory.

The function mkdir() will create the directory.

To know about mkdir() function do man 2 mkdir in the command window.

To use the function mkdir u have to include these header files
i.e
<sys/stat.h>
<sys/types.h>

Now i will show the program which will create the directory and then creates a file in the directory and performs read/write operations.

The Program is as follows:

Code: Cpp
#include<iostream.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fstream.h>
using namespace std;

main()
{
    if(mkdir("pathname",0777)==-1)//creating a directory
    {
        cerr<<"Error :  "<<strerror(errno)<<endl;
        exit(1);
    }
    else
    {
        ofstream write ("pathname/file.txt");//writing to a file
        if (write.is_open())
        {
            write << "This is a line."<<endl;
            write << "This is another line."<<endl;
            write.close();
        }
        else
            cout << "Unable to open file";
    }

    string line;

    ifstream read ("pathname/file.txt");//reading a file
    if (read.is_open())
    {

        while (! read.eof() )
        {
            getline (read,line);
            cout<<line<<endl;
        }
        read.close();
    }
    else
        cout << "Unable to open file";
}
I have compiled these programs in Linux not in other os.
lead.smart34's Avatar, Join Date: Feb 2008
Contributor
good info
crazytolearn57's Avatar, Join Date: Feb 2008
Go4Expert Member
good one
aisha.ansari84's Avatar, Join Date: Feb 2008
Contributor
nice
Bismarck's Avatar, Join Date: Mar 2008
Newbie Member
Well I have a problem guys...and I would like to know if you can help....is for Borland C/C++ not for linux, but I guess it is the same....
I`m in one directory EX: C://prog/test/ and there is a file "nr.in" How can I copy that file in to C://prog/ ?

Thanks for your help guys.
bashamsc's Avatar, Join Date: May 2007
Go4Expert Member
Quote:
Originally Posted by Bismarck
Well I have a problem guys...and I would like to know if you can help....is for Borland C/C++ not for linux, but I guess it is the same....
I`m in one directory EX: C://prog/test/ and there is a file "nr.in" How can I copy that file in to C://prog/ ?

Thanks for your help guys.
See u can't copy the file nr.in from C://prog/test/ to C://prog/.

U can copy some file from C://prog/ to C://prog/test/.
rashida.par's Avatar, Join Date: Feb 2008
Go4Expert Member
i tried your code good one
heena.mca's Avatar, Join Date: Feb 2008
Go4Expert Member
tried nice code
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Vote for article of the month for Feb 2008