文件输入在C + + [英] File Input in C++
问题描述
我试着在互联网上搜索一段时间,但是我可以用C ++找到文件输入。编写一个程序来执行文件中添加2个大于0的数字,但不使用scanf或cin。我想把文件加载到内存中,但是我能找到的所有代码都需要知道文件名。该文件在一行中用2个整数格式化,由一个空格分隔,并且有多行整数。程序将输出这两个数字的总和。我可以很容易地使用scanf做到这一点,但如果我得到一个巨大的文件,我想将它加载到内存(稍后保存映射)。
加载文件进入内存是给我的麻烦,因为我不知道文件名,也不知道如何查找,除非用户输入文件的名称(不会发生)。我希望程序像这样被执行,但使用最原始的,基本的C ++ IO形式:
./ myprog < boatloadofnumbers
我如何开始我的程序,以整个boatloadofnumbers作为文件,所以我可以使用更多的基本功能,如read()? (还有,上面的方法是什么?传入输入?)
int main(){
int a, b;
while(scanf(%i,%i,& a,& b)> -1){
printf(%i \ n,(a + b)) ;
} // endwhile
return 0;
} // endmain
清楚在不知道文件名的情况下如何阅读文件。大概你不知道编译时间的文件名。没关系,你可以从命令行在 runtime 中得到这个,就像这样:
。 / myprog boatloadofnumbers
然后你的文件名在 argv [1]
,你可以使用 std :: ifstream
来访问它。
通过直接在 stdin
上输入重定向 (例如 ./ myprog< boatloadofnumbers
),您根本不需要文件名, code> std :: cin 。
以下 main()
将处理这两种情况:
$ pre $ int
if(argc == 2)
{
std :: cerr<< 从文件中读取<< argv [1]<<的std :: ENDL;
std :: ifstream ifs(argv [1]);
if(ifs)
{
sum_lines(ifs);
}
else
{
std :: cerr<< 无法读取<< argv [1]<<的std :: ENDL;
}
}
else
{
std :: cerr<< 从标准输入读取<<的std :: ENDL;
sum_lines(std :: cin);
$ sum_lines()
可能看起来有点像这样:
$ b $ $ $ $ p $ void $ sum_lines(std :: istream& is)
{
int first = 0,second = 0;
std :: string line =;
while(std :: getline(is,line))
{
std :: istringstream iss(line);
if(>>> first>> second)
{
std :: cout<<第一<< +<<第二< =<<第一+第二<<的std :: ENDL;
}
else
{
std :: cerr<< 无法解析[<<行<< ]<<的std :: ENDL;
code
$ b
这并不关心在那里输入,所以你可以很容易地注入一个 std :: istringstream
来进行单元测试。此外,这不会将整个文件读入内存,每次只读一行,所以它应该处理 averybigboatloadofnumbers
。
I've been searching the internet for a while, but all I can find for file input in C++ is when you know the filename.
I'm trying to write a program to perform an addition of 2 numbers that are greater than 0 from a file, but without using scanf or cin. I want to load the file into memory, but all of the code I can find in regards to this situation requires knowledge of the filename. The file is formatted with 2 integers on a single line, separated by a space, and there are multiple lines of integers. The program will output the sum of the two numbers. I can easily do this with scanf, but if I were given a massive file, I would want to load it into memory (save mapping for later).
Loading the file into memory is giving me trouble, because I do not know the filename, nor how to find out, unless the user inputs the name of the file (not going to happen). I want the program to be executed like so, but using the most raw, and basic forms of C++ IO:
./myprog < boatloadofnumbers
How would I start my program to take the whole "boatloadofnumbers" as a file, so I can use more basic functions like read()? (also, what is the above method called? passing input?)
int main(){
int a,b;
while (scanf("%i,%i",&a,&b)>-1){
printf("%i\n",(a+b));
} //endwhile
return 0;
} //endmain
It's not entirely clear how you're going to read a file when you don't know the filename. Presumably you don't know the filename at compile-time. That's okay, you can get this from the command-line at runtime, like this:
./myprog boatloadofnumbers
Then your filename is in argv[1]
and you can access it using a std::ifstream
.
If you're being given the input directly on stdin
via redirection (such as ./myprog < boatloadofnumbers
) you don't need a filename at all, you can just use std::cin
.
The following main()
will deal with both of these situations:
int main(int argc, char* argv[])
{
if (argc == 2)
{
std::cerr << "Reading from file" << argv[1] << std::endl;
std::ifstream ifs(argv[1]);
if (ifs)
{
sum_lines(ifs);
}
else
{
std::cerr << "Could not read from " << argv[1] << std::endl;
}
}
else
{
std::cerr << "Reading from stdin" << std::endl;
sum_lines(std::cin);
}
}
A sample sum_lines()
may look a bit like this:
void sum_lines(std::istream& is)
{
int first = 0, second = 0;
std::string line = "";
while (std::getline(is, line))
{
std::istringstream iss(line);
if (is >> first >> second)
{
std::cout << first << " + " << second << " = " << first + second << std::endl;
}
else
{
std::cerr << "Could not parse [" << line << "]" << std::endl;
}
}
}
This doesn't care from where the input comes, so you can easily inject a std::istringstream
for unit-testing. Also, this doesn't read the whole file into memory, just one line at a time, so it should deal with averybigboatloadofnumbers
.
这篇关于文件输入在C + +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!