Jump to content
  • Advertisement

Archived

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

jumpjumpjump

opening a file in C++ not working!

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!