Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Creating a Directory and read/write to file in Directory in C++ (http://www.go4expert.com/articles/creating-directory-read-write-file-t9031/)

bashamsc 25Feb2008 11:24

Creating a Directory and read/write to file in Directory in C++
 
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 26Feb2008 18:17

Re: Creating a Directory and read/write to file in Directory in c++
 
good info

crazytolearn57 26Feb2008 18:38

Re: Creating a Directory and read/write to file in Directory in c++
 
good one

shabbir 5Mar2008 09:41

Re: Creating a Directory and read/write to file in Directory in c++
 
Nominate your favorite article of the month for Feb 2008

aisha.ansari84 5Mar2008 18:11

Re: Creating a Directory and read/write to file in Directory in c++
 
nice

Bismarck 10Mar2008 22:45

Re: Creating a Directory and read/write to file in Directory in c++
 
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 11Mar2008 12:01

Re: Creating a Directory and read/write to file in Directory in c++
 
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 13Mar2008 15:35

Re: Creating a Directory and read/write to file in Directory in c++
 
i tried your code good one

heena.mca 13Mar2008 16:00

Re: Creating a Directory and read/write to file in Directory in c++
 
tried nice code

shabbir 18Mar2008 17:35

Re: Creating a Directory and read/write to file in Directory in c++
 
Vote for article of the month for Feb 2008

shabbir 29Mar2008 17:43

Re: Creating a Directory and read/write to file in Directory in c++
 
Winner of article of the month for Feb 2008

micman 29Mar2008 20:29

Re: Creating a Directory and read/write to file in Directory in c++
 
good !

forum2006 15Apr2008 12:22

Re: Creating a Directory and read/write to file in Directory in c++
 
nice code

programming girl 25Apr2008 23:51

Re: Creating a Directory and read/write to file in Directory in c++
 
that is good code

Poonamol 22Apr2010 13:49

Re: Creating a Directory and read/write to file in Directory in c++
 
I am getting error
error C2065: 'mkdir' : undeclared identifier

Please help me out.

saurabh vora 3Dec2010 15:03

Re: Creating a Directory and read/write to file in Directory in c++
 
nice one...

bc.jat11 26Sep2011 22:07

Re: Creating a Directory and read/write to file in Directory in c++
 
Hello dear please help me on following topic:
How to get X and Y position of given character from screen in C.
e.g.

If HELLO is located at gotoxy(5,5) then when we check for H it will return x=5 and y=5.

and

assume Y is located on screen then
when we pass Y in argument it will return the x and y position of Y's location from screen.
Please help me.


All times are GMT +5.5. The time now is 06:35.