使用命令行输入文件比读取文件. [英] Input file using Command line than reading it.

查看:71
本文介绍了使用命令行输入文件比读取文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用命令行参数从用户那里获取输入文件.

格式必须为

I want to take an input file from user using command line arguments.

the format must be

exe method1 [fileName]



在此,方括号中的项目是可选的.如果未指定文件名,则程序应从stdin中读取.

因此,我使用了argcargv参数来获取主函数中的值.在argv[2]中,我使用
创建了一个变量



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

char *flName=argv[2]; 


然后使用
将值flName传递给函数method1


and then passed the value flName into a function method1 using

{method1(flName);}	



方法1收到此




Method1 receives this


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();


在这里,我有一个问题,它不是读取文件,而是将其内容指定为随机垃圾字符.
另外,如何从此命令行参数中删除[]括号.

最近6个小时以来,我一直在尝试许多排列和组合,但无法弄清楚.请建议.

谢谢.


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.

推荐答案

1.我希望您在访问argv[2]之前先检查argc>2吗?

2.我怀疑" exe method1 [fileName] "行仅表示语法,因此表示可选参数的方括号实际上并不是按字面意思编写的!例如,如果您的应用程序名为"reader.exe",则以下任意一项将是有效的调用:

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

换句话说,不需要剥开" []括号,因为据认为没有!

3. ifstream :: operator>>()具有多个重载来处理各种内置类型,但是这些重载都不能覆盖char [100]类型.即使它们做到了,您最好希望的是该运算符从文件中精确地读取 个100个字符,包括空格和制表符和换行符之类的控制字符,但不添加结尾0.结果,如果您尝试打印该字符串,在第100个字符之前看起来可能是合理的一半,但是随着打印功能继续通过char数组读取到缓冲区中,它变成了乱码! (直到最终遇到0个字符).

正如Richard指出的那样,您应该为此使用其他功能.此外,除非使用将缓冲区大小作为附加参数的函数,否则使用固定大小的缓冲区来读取未定义格式的文件是一个坏主意.这将使它们能够捕获过长的输入并防止写入缓冲区.
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.


我认为您不能使用插入运算符(>>)来读取数组.您应该使用 read() [ getline() [
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.


这篇关于使用命令行输入文件比读取文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆