Reading a User-Defined File

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.
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;
};


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();
}
and your above programme gives the follwoing error :

`std::ifstream file' has incomplete type and cannot be defined
Last edited on
thats because ifstream is defined in <fstream> and not <ifstream>. Sorry about that.
You can use std::getline() instead of istream::getline().

http://www.cplusplus.com/reference/string/getline/
can you correct my code so that it can read upto period.
Topic archived. No new replies allowed.