954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Asoseate File

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.

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

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' ))
    ++num_lines;

  f.close();

  // ...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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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.

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: