Hi guys, having a bit of trouble with this, whenever I specify a file to open it obviously isn't opening properly due to what is returned to the_file.is_open(). I can't see what's wrong with it, but I'm still learning so yeah... Cheers.
Code:
#include <iostream>
#include <string>
#include <fstream>
int main(int argc, char *argv[])
{
std::string file_contents;
std::ifstream the_file;
if (argc == 2)
{
the_file.open(argv[1]);
std::cout<<"Attempting to open file stream for: "<<argv[1]<<std::endl;
}
else
{
std::cout<<"Too many or too few arguments.";
std::cin.get();
std::exit(0);
}
if (!the_file.is_open())
{
std::cout<<"The file couldn't be opened!";
std::cin.get();
std::exit(0);
}
else
{
std::cout<<"File stream open!"<<std::endl<<"Printing contents of file..."<<std::endl<<std::endl;
std::cout<<"Press enter to see file contents!";
std::cin.get();
system("cls");
getline(the_file,file_contents,'\0');
std::cout<<file_contents<<std::endl<<std::endl;
std::cout<<"File contents printed. Press enter to quit.";
the_file.close();
std::cin.get();
}
return 0;
}
This is what I get:
Code:
C:\Documents and Settings\patrick>"C:\Documents and Settings\patrick\Desktop\Coding\project\Debug\project.exe" C:\services.txt
Attempting to open file stream for: C:\services.txt
The file couldn't be opened!