Force.comBat
Force.comBat

Reputation: 245

Input file name as command

I have to program to read some values from a Input text file.

int main(){

    FILE *pf;
    int i;
    int j;   

    pf = fopen("input.txt" , "r");

    fscanf(pf ,"%d , %d" , &i ,&j );

    printf("%d ,%d\n" , i ,j);

    fclose(pf);
}

and Input.txt has some values. Can anyone suggest me a way to get input.txt after running the program.

For example :

Open a terminal 
compile the code
Run the code
---Here it should ask for the file name---

Upvotes: 3

Views: 2012

Answers (2)

user520415
user520415

Reputation:

Example:

    #include <fstream>
    #include <iostream>

    int main()
    {
       std::string filename;
       cin >> filename;
       ifstream inFile;
       inFile.open(filename.append(".txt");
       int a;
       while (inFile)
       {
          inFile >> a;
          cout << a;
       }
       return 0;
    }

Upvotes: 2

bames53
bames53

Reputation: 88155

Command line arguments:

int main(int argc, char *argv[]) {
  std::cout << argv[1] << '\n';
}

Or stdin:

int main() {
  std::string filename;
  std::cout << "enter file name: ";
  std::cin >> filename;
  std::cout << filename << '\n';
}

Upvotes: 1

Related Questions