how to save file to the newly created directer ?

hi, I have on code, but I don't know how to improve it. To make the result.txt in the newly created directer ?

The code are following :

#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<time.h>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<sys/stat.h>
using namespace std;




int main()
{

int n;
cout<<"paramater for p and N: ";
cin>>n;


char folder[101];
cout<<"name your folder no fo: ";
cin>>folder;

char path[]="//Users//rw36//Desktop//new";

strcat(path, folder);
mkdir(path,0777);
cin.get();

ofstream ou("result.txt" , ios::out);

for ( int i=0; i<10; i++)
{
ou<<i*10<<endl;
}

return 0;
}
Use are you not using std::string instead of those misused char arrays?
sorry, i don't know what it is. can you correct me the code ?

Thanks ? how to make "result.txt" generated inside the directer named as path ?...
I've not compiled it, but this is what I had in mind. There may be an issue with the directory creation, but one thing at a time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int n;
    cout << "paramater for p and N: ";
    cin >> n;

    string folder;
    cout << "name your folder no fo: ";
    cin >> folder;

    string path = "/Users/rw36/Desktop/new/";
    path += folder;
    mkdir(path.c_str(), 0777);

    string filename = path + "/result.txt";
    ofstream ou(filename.c_str(), ios::out);
    for ( int i=0; i<10; i++)
    {
        ou<<i*10<<endl;
    }

    return 0;
}
Last edited on
thanks. yes, you are right,,,

it had been solved...

#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include<sys/stat.h>


using namespace std;

int main()
{
int n;
cout << "paramater for p ";
cin >> n;

string folder;
cout << "name your folder no fo: ";
cin >> folder;

string path = "/Users/rw36/Desktop/new";
path += folder;
mkdir(path.c_str(), 0777);

string filename = path + "/result.txt";
ofstream ou(filename.c_str(), ios::out);
for ( int i=0; i<n; i++)
{
ou<<i*n<<endl;
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.