how do you open a file with argv

Pages: 12
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
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();
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
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;
}
the file name needs to be input from the user
Do you mean while the program is running?

Or is it passed into the program via the command line?
while its running
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());
and to mutexe
no that line doesn't compile, I was just messing around with it
agreed, but that's not using the method parameter at all then. i do not understand.
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
if i don't use the arguments then it doesn't get past the
if (argc != 3)
and it ends the program
I think you need to outline your requirements more clearly.

What you're saying is conflicting a bit.
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
I'll say it again. Does:
inFile.open(file.nutrientFileName);

even compile?
no
i was just trying different things
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.

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
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;
    }
Are you giving your program command line arguments?
Pages: 12