Click here to Skip to main content
16,020,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to take an input file from user using command line arguments.

the format must be
exe method1 [fileName]


Here Square bracketed items are optional. If filename is not specified, program should read from stdin.

So i used argc, argv parameters to get the value in the main function. From argv[2] i made a variable using

C++
char *flName=argv[2]; 

and then passed the value flName into a function method1 using

C++
{method1(flName);}	


Method1 receives this


C++
void method1(char *flNamepm)
	{
	char *fileN=flNamepm;
	cout<<"flNamepm  "<<flNamepm<<"\ttypeid:"<<typeid(fileN).name()<<endl;		
	cout<<"fileN  "<<fileN<<"\ttypeid:"<<typeid(fileN).name()<<endl;		
	cout<<"*fileN  "<<*fileN<<"\ttypeid:"<<typeid(*fileN).name()<<endl;		
	ifstream myTfile;
	myTfile.open(flNamepm, ifstream::in);			
			
			char output[100];
				if (myTfile.is_open()) 
				{				
					while (!myTfile.eof()) 
					{				
						myTfile >> output; //detect tokens and fill it in the list
						cout<<output;						
					}
				}
			myTfile.close();

here i have a problem that it is not reading the file but specifying its contents as random garbage characters.
Also how can i strip off [ ] brackets from this command line parameter.

I have been trying a lot of permutations and combinations from last 6 hours but not able to figure this out. Pls suggest.

Thanks.
Posted
Updated 23-Sep-11 2:42am
v2

1. I hope you checked that argc>2 before accessing argv[2]?

2. I suspect that the line 'exe method1 [fileName]' just indicates the syntax, and the square brackets that indicate an optional parameter are therefore not meant to be actually written literally! If, say, your application is called 'reader.exe', then either of the following would be valid calls:

reader.exe method1
reader.exe method1 mydata.txt
reader.exe method1 myotherdata.txt

In other words, there is no need to 'strip off' the [] brackets, as there are supposedly none!

3. ifstream::operator>>() has multiple overloads to deal with various built-in types, but none of these cover the type char[100]. Even if they did, the best you could hope for would be that this operator reads exactly 100 characters from the file, including whitespace and control characters such as tabs and linebreaks, but without adding a terminating 0. As a result, if you tried to print that string, it might look halfway reasonable until the 100th character, but then turn into a garbled mess as the print function continues to read into the buffer past the char array! (until it eventually encounters a 0 character).

As Richard already pointed out, you should use other functions for this. And besides it is a bad idea to use a fixed size buffer for reading from a file of undefined format unless you use functions that take the size of that buffer as an additional argument. That will enable them to catch overly long input and prevent writing past the buffer.
 
Share this answer
 
I don't think you can use the insertion operator (>>) to read into an array. You should use read()[^] or getline()[^].

As to your second point why use brackets in the first place? If you do not include a filename then argc will have the value 2, to indicate that argv[2] does not exist.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900