• Create Account

### #ActualSavalric

Posted 22 December 2012 - 02:52 PM

Can you actually use the command-line arguements to call files?

The tutorial I'm reading is showing me code on how to use int main() but it isn't actually showing me how to open any files... I'm not sure if it's referencing files within the program or text files outside of the original source file.

This is the small bit of code he wrote with the comments:

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
// We assume argv[1] is a filename to open
ifstream the_file ( argv[1] );
// Always check to see if file opening succeeded
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
// the_file.get ( x ) returns false if the end of the file
//  is reached or an error occurs
while ( the_file.get ( x ) )
cout<< x;
}
// the_file is closed implicitly here
}
}

and then is the code I wrote, minus comments, plus cin.get(); so I can actually see the fruits of my labor before it closes.

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: " << argv[0] << " <filename>\n";
else
{
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else
{
char x;
while (the_file.get( x ) )
cout<< x;
}
}
cin.get();
}

Is there some concepts that I missed? Because this is the second time file opening was reference and I wasn't actually able to open a file.

EDIT: Oops, i still had a mistake in the code here: cing.get should've been cin.get. I had already fixed that issue before, not sure how the g got back but at any rate, the code runs the same as I mentioned.

### #2Savalric

Posted 22 December 2012 - 02:17 PM

Can you actually use the command-line arguements to call files?

The tutorial I'm reading is showing me code on how to use int main() but it isn't actually showing me how to open any files... I'm not sure if it's referencing files within the program or text files outside of the original source file.

This is the small bit of code he wrote with the comments:

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
// We assume argv[1] is a filename to open
ifstream the_file ( argv[1] );
// Always check to see if file opening succeeded
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
// the_file.get ( x ) returns false if the end of the file
//  is reached or an error occurs
while ( the_file.get ( x ) )
cout<< x;
}
// the_file is closed implicitly here
}
}

and then is the code I wrote, minus comments, plus cin.get(); so I can actually see the fruits of my labor before it closes.

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: " << argv[0] << " <filename>\n";
else
{
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else
{
char x;
while (the_file.get( x ) )
cout<< x;
}
}
cing.get();
}

Is there some concepts that I missed? Because this is the second time file opening was reference and I wasn't actually able to open a file.

### #1Savalric

Posted 22 December 2012 - 02:17 PM

Can you actually uses the command-line arguements to call files?

The tutorial I'm reading is showing me code on how to use int main() but it isn't actually showing me how to open any files... I'm not sure if it's referencing files within the program or text files outside of the original source file.

This is the small bit of code he wrote with the comments:

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
// We assume argv[1] is a filename to open
ifstream the_file ( argv[1] );
// Always check to see if file opening succeeded
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
// the_file.get ( x ) returns false if the end of the file
//  is reached or an error occurs
while ( the_file.get ( x ) )
cout<< x;
}
// the_file is closed implicitly here
}
}

and then is the code I wrote, minus comments, plus cin.get(); so I can actually see the fruits of my labor before it closes.

#include <fstream>
#include <iostream>

using namespace std;

int main ( int argc, char *argv[] )
{
if ( argc != 2 )
cout<<"usage: " << argv[0] << " <filename>\n";
else
{
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else
{
char x;
while (the_file.get( x ) )
cout<< x;
}
}
cing.get();
}

Is there some concepts that I missed? Because this is the second time file opening was reference and I wasn't actually able to open a file.

PARTNERS