11 Replies - 1808 Views - Last Post: 05 September 2011 - 03:31 PM Rate Topic: -----

#1 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

calling filename in command line

Posted 04 September 2011 - 02:18 PM

Hello, I am new to this website. Here is the problem I am having, i have to write a program in C++ that calls a text file, the text file should have integers and no spaces in between the integers, then it should add the integers and send sum to the output file, otherwise it will skip straight to the output file and write "invalid sequence", My main problem is that it always skips to the output file name and i do not know what is going wrong.
//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	ifstream inputfile;
	ofstream outfile;
	argv[2]="input=<filename>", "sum input=<filename>";

	if (argc>1)
	{
	ifstream inputfile;
	inputfile.open(argv[2],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);
	int i=0;
	char ch[256];
	while(!inputfile.eof())
	ch[i]=inputfile.peek();
	inputfile>>ch[256];
	for (i=0;i<256;i++)
	if(ch[i]<char(32)|| ch[i]>char(127) || ch[i]!=' ')
	{
		
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}

This post has been edited by efisoccer: 04 September 2011 - 02:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: calling filename in command line

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: calling filename in command line

Posted 04 September 2011 - 03:33 PM

What are you doing here?

argv[2]="input=<filename>", "sum input=<filename>";

Was This Post Helpful? 1
  • +
  • -

#3 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 04 September 2011 - 05:55 PM

Sorry, i forgot to remove that it should be like this

//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	ifstream inputfile;
	ofstream outfile;

	if (argc>1)
	{
	ifstream inputfile;
	inputfile.open(argv[1],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);
	int i=0;
	char ch[256];
	while(!inputfile.eof())
	ch[i]=inputfile.peek();
	inputfile>>ch[256];
	for (i=0;i<256;i++)
	if(ch[i]<char(32)|| ch[i]>char(127) || ch[i]!=' ')
	{
		
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}


Was This Post Helpful? 0
  • +
  • -

#4 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 04 September 2011 - 06:19 PM

I mean this one.
//Esteban Efigenio
//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	ifstream inputfile;
	ofstream outfile;
	argv[1]="input=<filename>", "sum input=<filename>";

	if (argc>0)
	{
	ifstream inputfile;
	inputfile.open(argv[1],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);
	int i=0;
	char ch[256]={0};
	while(!inputfile.eof())
	ch[i]=inputfile.peek();
	inputfile>>ch[i];
	for (i=0;i<256;i++)
	if(ch[i]<char(32)|| ch[i]>char(127) || ch[i]!=' ')
	{
		
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}

Was This Post Helpful? 0
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: calling filename in command line

Posted 04 September 2011 - 06:36 PM

well your latest version put that back in, let's go over what that dose real quick.

the ',' or comma, is an operator in C++. it takes 2 values of any type and returns the value from the right. this means that you can actually chain the values so that the last value in the sequence is returned.

take a look at the following
cout<<(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


that prints 10. here is another look, something i always thought was tricky and deceptive.

cout<<("%i", 10);


Wow!! that looks like i worked!! let's try it again!

cout<<("%i %i", 10, 10);


what? it doesn't work that way? nope, sorry. it just prints 10, not "10 10"
Was This Post Helpful? 1
  • +
  • -

#6 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: calling filename in command line

Posted 04 September 2011 - 07:05 PM

One problem, before everything else, is to never write to argv[], not a single element.
It's dangerous on most prompts, as it could be only as big as it needed to be for the original info.

Edit:
also, you lost your prompt input, it won't write if it's comma separated since it's a character array, and argc is always greater than 0, since foo.exe is an element in argv.

This post has been edited by OLH064: 04 September 2011 - 07:10 PM

Was This Post Helpful? 1
  • +
  • -

#7 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 04 September 2011 - 07:37 PM

Ok one more update, i removed the argv[1]="input=filename","sum input=filename";
but should i still have the argv[1]="filename"; because when i take that out and just leave
argv[1];
the command prompt does not do anything not even send anything to the output file. So i left
argv[1]="filename";
//Esteban Efigenio
//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	fstream inputfile;
	ofstream outfile;
	argv[1]="filename";


	if (argc>0)
	{
	fstream inputfile;
	inputfile.open(argv[1],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);

	char ch[256]={0};
	while(!inputfile.eof())

	for (int i=0;i<256;i++)
	if(ch[i]>char(32) && ch[i]< char(127) && ch[i]!=' ')
	{
	inputfile>>argv[1]>>ch[i];
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}

Was This Post Helpful? 0
  • +
  • -

#8 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: calling filename in command line

Posted 04 September 2011 - 09:53 PM

No, see, argv[1] is an acceptable character array for opening a file, and "filename" gets written as the word filename. "filename" is not a macro, it is a constant string.

Also, have you tried to compile this, and look at any errors? You can actually learn from those errors.
Was This Post Helpful? 1
  • +
  • -

#9 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 04 September 2011 - 10:39 PM

Yes i removed that arv[1]="filename"; and no longer use it,thanks for all the help.

This post has been edited by efisoccer: 04 September 2011 - 10:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 OLH064  Icon User is offline

  • Junior bit compressor

Reputation: 20
  • View blog
  • Posts: 725
  • Joined: 06-June 11

Re: calling filename in command line

Posted 05 September 2011 - 01:03 PM

:bigsmile: Make sure to call your program using a prompt, and pass a filename when starting the program!

foo file.txt
foo.exe runs, gets argv[1] full of "file.txt", but without quotes. I use quotes like C does.
Was This Post Helpful? 0
  • +
  • -

#11 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 05 September 2011 - 01:37 PM

Thanks for the help im almost done just stuck on a couple of things, in my program im supposed to set
special cases so that the additions dont work. Acceptable cases are (1+2+3+4+5), Unacceptable
are (1+2+3+A+) and (1+2+3 5+5)so far my code works for the acceptable case, but for the others it just doesnt.
    //Run in Command Line
    #include<iostream>
    #include<fstream>
    #include<iomanip>
     
    using namespace std;
     
    int main (int argc,char *argv[])
     
    {
    int sum=0;
    int x=0;
    char sign='+';
	
     
    string filename;
    std::ifstream inputfile;
    std::ofstream outfile;
     
    if (argc>1)
    {
    inputfile.open(argv[1], ios::in);
     
    if(inputfile.is_open())
    {
    outfile.open("sum.txt", ios::app);
     
     
    while(!inputfile.eof() && !inputfile.fail())
    {
    inputfile>>x>>sign;
    if(sign=='+')
    {
    sum+=x;
    }
	else if ((sign>(char)31)&&(sign<(char)128))
	{
	inputfile.fail();
	}
    }
    cout<<sum;
    outfile<<"sum= "<<sum<<endl;
     
    inputfile.close();
    outfile.close();
    exit(0);
    }
     
    }
    else if(inputfile.fail())
    {
    ofstream outfile;
    outfile.open("sum.txt", ios::app);
    outfile<<"Invalid Sequence"<<endl;
    outfile.close();
    exit (1);
     
    }
     
    }


Was This Post Helpful? 0
  • +
  • -

#12 efisoccer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-September 11

Re: calling filename in command line

Posted 05 September 2011 - 03:31 PM

Finally figured it out!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1