Archived

This topic is now archived and is closed to further replies.

jumpjumpjump

opening a file in C++ not working!

Recommended Posts

Below is my code. This is my first C++ proggy that opens a file(and my first harder C++ proggy) after getting the location from the user. I get an error on the ifstream line in dev-c++. This is the error: 28 C:\Programming\my projects\main.cpp no matching function for call to `std::basic_ifstream >::basic_ifstream(std::string&, const Thanks for any hlep
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;
string dir; // holds the directory that the file exists in

string tmp; //this is the temperary file line string


int main(int argc, char *argv[])
{
  cout << "\nWhere is the file located you want to open?";
  getline(cin, dir);
  if( dir != "" )
  {
  ifstream source( dir, ios::in);
  {
   while(!source.eof())
     } 
      source.getline(cin,tmp);
      cout << tmp;
      
          source.close();
      }
   else
      {
               cout << "file not found" << endl;
          }


  
  return 0;
}
Sharp Basic baby!
My Homepage | My Forums Please Register | Google | Sharp Basic - Coming summer 2004! Wanna Beta test? Clicky! [edited by - jumpjumpjump on March 23, 2004 7:19:30 PM] [edited by - jumpjumpjump on March 23, 2004 7:20:04 PM]

Share this post


Link to post
Share on other sites
ifstream probably is expecting a char * for the file name, not a string. Try:
ifstream source( dir.c_str(), ios::in);

Share this post


Link to post
Share on other sites
at another glace i see more errors!


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>

using namespace std;
string dir; // holds the directory that the file exists in

string tmp; //this is the temperary file line string

int done=0;
int main(int argc, char *argv[])
{
cout << "\nWhere is the file located you want to open?";
getline(cin, dir);
while(!done)
{
if(ifstream(dir))
{

ifstream source( dir, ios::in);
while(!source.eof())
{
source.getline(cin,tmp);
cout << tmp;
source.close();
}
}
else
{
cout << "file not found" << endl;
}
}


return 0;
}


that should do it




Sharp Basic - Coming summer 2004!
Sign Up For Sharp Basic Beta Testing!!!

Share this post


Link to post
Share on other sites