Reading a User-Defined File
asad393 (10)
Oct 10, 2012 at 7:30am UTC
I am writing a programe that can read a user defined specific file , meaning that user has to select the file , which function i have to use for that as on default we read the file that is already setup by the programmer.
pogrady (410)
Oct 10, 2012 at 7:47am UTC
You can get input from the user. That is, if you are talking about a console window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <ifstream>
#include <string>
using namespace std;
int main()
{
ifstream file;
string filepath;
cout<<"Please enter the file name: " ;
cin>>filepath;
if (!file.open(filepath))
cout<<"No such file or directory." ;
//do stuff
return 0;
};
asad393 (10)
Oct 10, 2012 at 7:52am UTC
thanks very much and tell me anoter this . my this programe down here only reads upto 50 array characters. i want to read until a period(.) is observed in the text file.
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
//formatoo.cpp
//writes formateed output to a file, using <<
#include <fstream>
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
int main(){
ifstream infile("readdata.txt" );
ofstream outfile("1data.txt" );
int MAX=500;
char string[MAX];
while (!infile.eof)
{
infile.getline(string,MAX,'.' );
outfile<<string;
cout << "Data Written" ;
}
getch();
}
asad393 (10)
Oct 10, 2012 at 7:55am UTC
and your above programme gives the follwoing error :
`std::ifstream file' has incomplete type and cannot be defined
Last edited on Oct 10, 2012 at 7:55am UTC
pogrady (410)
Oct 10, 2012 at 8:05am UTC
thats because ifstream is defined in <fstream> and not <ifstream>. Sorry about that.
asad393 (10)
Oct 10, 2012 at 9:08am UTC
can you correct my code so that it can read upto period.
Topic archived. No new replies allowed.