ok from all of my questions you can probably tell I'm working on something. I want a file to open my program when it is double clicked on (ex .txt opens notepad) I know how to modify the registry n stuff what I need is how to make my program use that file when opened this is a CUI console app. Thanks in advance.

Recommended Answers

All 2 Replies

Your program needs to take the file name as argument. Example program:

#include <fstream>
#include <iostream>
#include <limits>
using namespace std;

int main( int argc, char** argv )
  // Make sure the program has at least two arguments:
  //   1: the program's executable path
  //   2: the file name passed to your program
  if (argc != 2)
    return 1;

  // Open the argument file
  ifstream f( argv[ 1 ] );

  // If the file could not be opened, we're done.
  if (!f)
    return 1;

  // This example program just counts the number of lines in the file...
  size_t num_lines = 1;
  while (f.ignore( numeric_limits <streamsize> ::max(), '\n' ))


  // ...and displays the count to the user
  cout << "The file \"" << argv[ 1 ] << "\" contains " << num_lines << " lines.\n";

  // Keep the console open long enough for the user to see the result.
  cout << "Press ENTER to quit.";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  return 0;

Hope this helps.

Ok a little extra explanation would be good as I rarely use normal arguments (I know there the stuff you put in Parenthases) (I dont really count system() commands as arguments)Also where in this would I put the code for the rest of my program as it is to do much more than read the file. The program also has stuff to do when it is opened as normal. The file could also have ether one number or one character. That hole hurts man, I dont know how to compare char to other stuff; but I survive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.