Input File Name at Run Time in C++.

How Could I Give a File Name At run time in a C++ Program.
Can you provide more info on your problem? If you want to read/write from/to a file you can ask for the file's name and store it in a char array or a string.
i want to creat a file in C++ and get user input at RUN TIME for That file name
.
1
2
3
4
5
6
7
8
9
10
11
//...

char fileName[100];
cout << "File path(name): "; cin >> fileName;
fstream f(fileName, ios::out);   //this creates the file

//...write to f

f.close();

//... 
So what exactly is the problem ?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <string>

int main() {
string file;
cout << "Enter gile name: " << endl;
cin >> file;

ofstream of (file.c_str()); 

// file is opened
}
*edit* not all headers are required.
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
#include <conio.h>
#include <cstdlib>
#include <dos.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
using namespace std;

int main (int argc, char *argv[])
{
    if (argc!=2)
    {
        cout << "Incorrect arguments\n";
    }
	else
	{
	// open file argv[1]
	ifstream in (argv[1]);
	}    
return 0;
}
Last edited on
Topic archived. No new replies allowed.