Input files

accepting an input file from a command prompt

Page 1 of 1

4 Replies - 1894 Views - Last Post: 07 December 2010 - 09:00 AM Rate Topic: -----

#1 woloszpl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Input files

Posted 07 December 2010 - 06:47 AM

Hi.
I have a C++ program that I have made a makefile that runs in a command line. I have successfully generated and run the .exe from a command line. I would like to be able to run the .exe and have it accept an input file by typing at the command prompt.. ./my_program file.txt

Currently I have the input file hardcoded as below. How do I change this to accept the input file name from the command line.

inFile.open("C:\Input\input1.txt", ios::in); // opens the input file

My code is attached in case you need it.
Any help would be appreciated. Thanks!

/*
   Shortest Common Superstring Program
   By ********
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   
    ifstream inFile;
	ofstream outFile;
	string SuperStr;
	string STRING;
	string sub;
	string newstr;
	bool done;
	size_t found;
	int pos = 0;
	int m = SuperStr.size();
	int len = 0;


    
    inFile.open("C:\Input\input1.txt", ios::in);  // opens the input file
	outFile.open("c:\Input\superstring.txt", ios::out);  // opens the input file
   
   
 
   getline(inFile,SuperStr); // Saves the first line as the superstring
   getline(inFile,STRING); //Saves the line in STRING
   
   while (getline(inFile, STRING))  // keep reading until end-of-file
	 {
		 cerr << "Entered in first While loop" << endl;
	  
	  done = false;
		
		while (done != true)
		{
			cerr << "Entered in second While loop" << endl;
			if (SuperStr.compare(STRING)==0)
				{
				done = true;
				}
			else
			{
				pos = 1;
				len = m;
				while (len >= 2)
					{
					cerr << "Entered in third While loop" << endl;
					len = m - pos;
					sub = SuperStr.substr(pos,len);
					found = STRING.find(sub);
					if (found !=string::npos)
						{
						cerr << "Entered the last if statement" << endl;
						newstr = STRING.erase(found+1,len);
						SuperStr.append(newstr);
						done = true;
						len = 0;
						}
					else
						{
						pos ++;
						}
					}
				done = true;
			} // third while loop check for len of 2
		
		}  //second while loop waiting for bool done
		
     getline(inFile,STRING); //Saves the line in STRING	
	 }  //first while loop reads file		

 
    outFile << SuperStr;
   	inFile.close();
	outFile.close();
}



MOD EDIT: Moved code into post.

Please do not attach your code to your message, but copy and paste it -- IN CODE TAGS AS SHOWN BELOW -- into the body of your post.

:code:

Is This A Good Question/Topic? 0
  • +

Replies To: Input files

#2 -shadow-   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Input files

Posted 07 December 2010 - 07:18 AM

View Postwoloszpl, on 07 December 2010 - 08:47 AM, said:

Hi.
I have a C++ program that I have made a makefile that runs in a command line. I have successfully generated and run the .exe from a command line. I would like to be able to run the .exe and have it accept an input file by typing at the command prompt.. ./my_program file.txt

Currently I have the input file hardcoded as below. How do I change this to accept the input file name from the command line.

inFile.open("C:\Input\input1.txt", ios::in); // opens the input file

My code is attached in case you need it.
Any help would be appreciated. Thanks!

It sounds to me like you want to use parameters of main.
if you do something like
void main( int argc, char *argv[] ){

you can call your program as:
<programname> hello world!

then you will have a local character array called argv
and argc. argc is an integer indicating the number of
arguments given, and argv is an array in which they are
stored. so in this case:
argc = 3 ( remember the term used to call the program is counted as an argument )
argv[0] = (your program's name )
argv[1] = hello
argv[2] = world!

hope this helps :)
~Wes
Was This Post Helpful? 0
  • +
  • -

#3 woloszpl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-December 10

Re: Input files

Posted 07 December 2010 - 07:29 AM

Yes it does. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 -shadow-   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Input files

Posted 07 December 2010 - 08:25 AM

View Postwoloszpl, on 07 December 2010 - 09:29 AM, said:

Yes it does. Thanks!

No problem :)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6247
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Input files

Posted 07 December 2010 - 09:00 AM

View Post-shadow-, on 07 December 2010 - 08:18 AM, said:

It sounds to me like you want to use parameters of main.
if you do something like
void main( int argc, char *argv[] ){


No, Wes...NO, NO, NO. void main() has never, EVER been correct in C++, per the creator of C++ himself. Please break yourself of this habit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1