1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C++' started by bashamsc, Feb 25, 2008.

  1. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    I will explain how to create a directory in linux using a c++ program.

    The program to create a directory is as follows:

    Code:
    #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:
    #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.
     
  2. lead.smart34

    lead.smart34 New Member

    Joined:
    Feb 14, 2008
    Messages:
    77
    Likes Received:
    0
    Trophy Points:
    0
  3. crazytolearn57

    crazytolearn57 New Member

    Joined:
    Feb 14, 2008
    Messages:
    48
    Likes Received:
    0
    Trophy Points:
    0
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  5. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
  6. Bismarck

    Bismarck New Member

    Joined:
    Mar 10, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  7. bashamsc

    bashamsc New Member

    Joined:
    May 22, 2007
    Messages:
    51
    Likes Received:
    7
    Trophy Points:
    0
    Location:
    chennai
    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/.
     
  8. rashida.par

    rashida.par New Member

    Joined:
    Feb 14, 2008
    Messages:
    21
    Likes Received:
    1
    Trophy Points:
    0
    i tried your code good one
     
  9. heena.mca

    heena.mca New Member

    Joined:
    Feb 14, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    tried nice code
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  12. micman

    micman New Member

    Joined:
    Jan 25, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
  13. forum2006

    forum2006 New Member

    Joined:
    Oct 14, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  14. programming girl

    programming girl New Member

    Joined:
    Apr 24, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    that is good code
     
  15. Poonamol

    Poonamol New Member

    Joined:
    Mar 31, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    I am getting error
    error C2065: 'mkdir' : undeclared identifier

    Please help me out.
     
  16. saurabh vora

    saurabh vora New Member

    Joined:
    Dec 3, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    V.V.nagar
  17. bc.jat11

    bc.jat11 New Member

    Joined:
    Sep 26, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Deesa,Gujarat,India.
    Home Page:
    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.
     

Share This Page