关于读入文件的疑问。

LAST_MAN 2011-11-20 04:42:52

int main(int argc,char **agrv) //注释说这里main函数接受文件名为参数 ,那为什么要写成char **agrv?
{
if(agrc<2) //此处小于2是什么意思?注释说是检查命令行参数个数,是什么意思,为什么小于2,而不是其他数字?
{
cerr<<"no input file!"<<endl;
return EXIT_FAILUREl
}
ifstream infile;
infile.open(argv[1]); //agrv[1]是什么东西?
if(!infile)
{
cerr<<"can not open input file!"<<endl;
return EXIE_FAILURE;
}
//后面还有一些代码,不过不是我的疑问




我想知道,读入一个文件,有没有规定是什么格式,要怎么读入,输入文件的文件名吗?那文件的路径有没有规定?
希望能有详细的解答,这些在C++PRIMER上的IO库那章没说清楚
请高人指点
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
支持英文数字 2011-11-20
  • 打赏
  • 举报
回复

sorry, you have to find out the book which the answer is in.

Find out the answer before you ask anyone else for help!
a707000646 2011-11-20
  • 打赏
  • 举报
回复
至于文件的路径,可以使用相对路径,也可以使用绝对路径
绝对路径 譬如C盘中的test.txt就是C://test.txt
如果是相对路径就是你编写的可执行程序所在的目录找
如果你的程序在C盘
文件路径在你的程序里面可以直接写test.txt
a707000646 2011-11-20
  • 打赏
  • 举报
回复
ios_base::openmode
typedef T3 openmode;
static const openmode app, ate, binary, in, out, trunc;
The type is an enumerated type T3 that describes an object that can store the opening mode for several iostreams objects. The distinct flag values are:

app, to seek to the end of a stream before each insertion
ate, to seek to the end of a stream when its controlling object is first created
binary, to read a file as a binary stream, rather than as a text stream
in, to permit extraction from a stream
out, to permit insertion to a stream
trunc, to truncate an existing file when its controlling object is first created
pengzhixi 2011-11-20
  • 打赏
  • 举报
回复
1.读写文件有文本模式和二进制模式,如果没写那就是文本模式了。你这里是以命令行参数 输入文件名的是argv[1]这个作为文件名。 至于测试为什么小于2就是说如果命令行参数个数小于2意味着你没有输入文件名。文件名如果你没输入文件路径那么就在你的工程目录下面找

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧