C++ 使用 fstream 对象作为函数参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14503605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using fstream Object as a Function Parameter
提问by DragonClaw
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
void vowel(fstream a){
char ch;
int ctr = 0;
while(!a.eof()){
a.get(ch);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
cout << ch;
ctr++;
}
}
cout << "Number of Vowels: " << ctr;
}
main(){
fstream a;
a.open("temp.txt", ios::in);
vowel(a);
return 0;
}
In this simple program , I am trying t count the number of caps Vowels in the file temp.txt. However, I am getting the error:
在这个简单的程序中,我试图计算文件 temp.txt 中大写元音的数量。但是,我收到错误:
ios::ios(ios &) is not accessible in function fstream::fstream(fstream&)
ios::ios(ios &) 在函数 fstream::fstream(fstream&) 中不可访问
Instead opening the file in the function itself does the job. Why is it so? Thanks a lot
而是在函数本身中打开文件来完成这项工作。为什么会这样?非常感谢
NB:
注意:
How do I use fstream (specifically ofstream) through a functions parameters
Here it says, that it should work the way I am trying.
它在这里说,它应该按照我尝试的方式工作。
Rick
瑞克
回答by hmjd
An fstream
object is not copyable. Pass by reference instead: fstream&
:
一个fstream
对象是不可拷贝。而是通过引用传递fstream&
::
void vowel(fstream& a)
Note you can avoid the call to open()
by providing the same arguments to the constructor:
请注意,您可以open()
通过向构造函数提供相同的参数来避免调用:
fstream a("temp.txt", ios::in);
and don't use while(!a.eof())
, check the result of read operations immediately. The eof()
will only be set when an attempt is made to read beyond the last character in the file. This means that !a.eof()
will be true when the previous call to get(ch)
read the last character from the file, but subsequent get(ch)
will fail and set eof but the code won't notice the failure until after it has processed ch
again even though the read failed.
并且不要使用while(!a.eof())
,立即检查读取操作的结果。在eof()
当试图读取超出文件中的最后一个字符将只设置。这意味着!a.eof()
当前一个调用get(ch)
从文件中读取最后一个字符时,它将为真,但随后的调用get(ch)
将失败并设置 eof 但代码不会注意到失败,直到它ch
再次处理后,即使读取失败。
Example correct structure:
示例正确结构:
while (a.get(ch)) {
回答by juanchopanza
You need to pass the fstream
by reference:
您需要fstream
通过引用传递:
void vowel(fstream& a){ .... }
// ^ here!
回答by Arpit
try this. instead of sending the file count the vowels in line.
尝试这个。而不是发送文件计数元音。
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int vowels=0;
void vowel(string a){
char ch;
int ctr = 0;
int temp=0;
for(temp=0,temp<a.length();temp++){
ch=a.at(temp);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
cout << ch;
ctr++;
}
}
vowels+=ctr;
}
main(){
fstream a;
a.open("temp.txt", ios::in);
string temp;
while(getline(a,temp))
{
vowel(temp);
function2(temp);
function3(temp);
... so on for more then one functions.
}
vowel(a);
return 0;
}
if you want to pass the file then use the above ans.(pass fstream by reference).
如果要传递文件,请使用上面的 ans.(通过引用传递 fstream)。