how do you open a file with argv

Pages: 12
Feb 27, 2013 at 1:36pm
this is what get called to do everything, but I can't figure out how to open my two files with argv[1] and argv[2] as the parameters
heres my code:

void computeCalories (const char* nutrientFileName, const char* recipeFileName)
{
string file, file2;

ifstream inFile;

cout << "Please enter the nutrient file name\n";
cin >> file;
cout << endl;

inFile.open(file.nutrientFileName);


if (!inFile)
{
cout << "bummer file name \n\n";
}//open first file/ read



ifstream inFile2;

cout << "Please enter the recipe file name\n";
cin >> file2;
cout << endl;

inFile2.open(file2.recipeFileName);


if (!inFile2)
{
cout << "bummer file name \n\n";
}//open 2nd file/ read
Last edited on Feb 27, 2013 at 1:37pm
Feb 27, 2013 at 1:37pm
Assuming you passed a valid file name into argv[1], it would simply be...

1
2
3
4
5
ifstream inFile( argv[1] );

// Do whatever

inFile.close();
Feb 27, 2013 at 1:41pm
what's your main() look like?

edit: you're passing in the name of the nutrient file and then asking the user to enter a nutrient file name?

does this actually compile? Specifically:
inFile.open(file.nutrientFileName);

?
Last edited on Feb 27, 2013 at 1:55pm
Feb 27, 2013 at 1:56pm
int main (int argc, char** argv)
{
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " nutrientFile recipeFile" << endl;
return -1;
}

computeCalories (argv[1], argv[2]);
return 0;
}
Feb 27, 2013 at 1:57pm
the file name needs to be input from the user
Feb 27, 2013 at 1:59pm
Do you mean while the program is running?

Or is it passed into the program via the command line?
Feb 27, 2013 at 2:00pm
while its running
Feb 27, 2013 at 2:00pm
Command line arguments is given by the user.

If you have a std::string and want to have a const char* you can use the c_str() member function.
inFile.open(file.c_str());
Feb 27, 2013 at 2:01pm
and to mutexe
no that line doesn't compile, I was just messing around with it
Feb 27, 2013 at 2:01pm
agreed, but that's not using the method parameter at all then. i do not understand.
Feb 27, 2013 at 2:02pm
If you want the user to give the filename while the program is running then you don't really need to use the argument vector.

Just create a std::string, use getline to get the input from the user and open using the method Peter87 has just given.

Edit: Example, if it helps...
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
#include <iostream>
#include <fstream>
#include <string>

int main( int argc, char* argv[] )
{
  std::string filename;
  std::ifstream inFile;

  std::cout << "Please enter filename: ";
  std::getline( std::cin, filename );

  inFile.open( filename.c_str() );

  if( !inFile )
  {
    std::cout << "Unable to open file: " << filename << std::endl;
    return -1;
  }

  // File operations here

  inFile.close();

  return 0;
}
Last edited on Feb 27, 2013 at 2:07pm
Feb 27, 2013 at 2:08pm
if i don't use the arguments then it doesn't get past the
if (argc != 3)
and it ends the program
Feb 27, 2013 at 2:09pm
I think you need to outline your requirements more clearly.

What you're saying is conflicting a bit.
Feb 27, 2013 at 2:14pm
I can't mess with the main, all of my code needs to be done in the void
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "vectorUtils.h"

using namespace std;




// Compute the number of calories in one serving of a recipe
void computeCalories (const char* nutrientFileName, const char* recipeFileName);



int main (int argc, char** argv)
{
  if (argc != 3)
    {
      cerr << "Usage: " << argv[0] << " nutrientFile recipeFile" << endl;
      return -1;
    }

  computeCalories (argv[1], argv[2]);
  return 0;
}




// Compute the number of calories in one serving of a recipe
void computeCalories (const char* nutrientFileName, const char* recipeFileName)
{
    string file, file2;

    ifstream inFile;

    cout << "Please enter the nutrient file name\n";
     cin >> file;
     cout << endl;

     inFile.open(file.nutrientFileName);


    if (!inFile)
    {
         cout << "bummer file name \n\n";
    }//open first file/ read



    ifstream inFile2;

    cout << "Please enter the recipe file name\n";
     cin >> file2;
     cout << endl;

     inFile2.open(file2.recipeFileName);


    if (!inFile2)
    {
         cout << "bummer file name \n\n";
    }//open 2nd file/ read

            int howmany, howmany2;
            howmany=0;
            howmany2=0;
            //compare two data sets
			for (int ir=0; ir<=howmany2; ir++)//loop through ingredients (inside recipes)
            {
                for (int in=0; in<=howmany; in++)//look for corresponding ingredients in nutrients
                {

                }
            }

			//if found, add calories found times amount

			//if not found, set all flag to false/no

	//divide total calories by serving size
    //print recipe name




}


that's everything I have
just kinda been messing around with it
Feb 27, 2013 at 2:17pm
I'll say it again. Does:
inFile.open(file.nutrientFileName);

even compile?
Feb 27, 2013 at 2:25pm
no
i was just trying different things
Feb 27, 2013 at 2:28pm
try:
ifstream.open(nutrientFileName); 


and remove all of the stuff in your method asking the user to input anything. the other doesn't have to input anything because the input is coming from the command line.

Feb 27, 2013 at 2:28pm
If you're passing the filenames in the command line then you don't need to ask the user for them.

You should be able to open them with:

1
2
3
std::ifstream inFile;

inFile.open( nutrientFileName );

Last edited on Feb 27, 2013 at 2:29pm
Feb 27, 2013 at 2:35pm
ok it compiles but when i run it doesn't get past
1
2
3
4
5
if (argc != 3)
    {
      cerr << "Usage: " << argv[0] << " nutrientFile recipeFile" << endl;
      return -1;
    }
Feb 27, 2013 at 2:39pm
Are you giving your program command line arguments?
Pages: 12