Input File Name at Run Time in C++.
Dec 30, 2012 at 1:44pm UTC
How Could I Give a File Name At run time in a C++ Program.
Dec 30, 2012 at 2:32pm UTC
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.
Dec 30, 2012 at 2:37pm UTC
i want to creat a file in C++ and get user input at RUN TIME for That file name
.
Dec 30, 2012 at 2:41pm UTC
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();
//...
Dec 30, 2012 at 2:42pm UTC
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
}
Jan 4, 2013 at 4:03am UTC
*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 Jan 4, 2013 at 4:05am UTC
Topic archived. No new replies allowed.