c++:ifstream 打开传递文本文件名字符串的问题
我正在尝试将字符串从 main 传递到另一个函数。该字符串是需要打开的文本文件的名称。据我所知,我正在传递字符串,但是当我尝试使用 ifstream.open(textFileName) 时,它不太起作用。但是当我手动将其硬编码为 ifstream.open("foo.txt") 时,它工作得很好。我需要多次使用这个函数,所以我希望能够传入一串文本文件名。
这是我的
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
#ifndef DATAREADER_H
#define DATAREADER_H
#include "DataReader.h"
#endif
using namespace std;
int main()
{
vector<Data*> database = DataReader("foo.txt");
return 0;
}
DataReader 的主标头
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
using namespace std;
vector<Data*> DataReader(string textFile);
,最后是 DataReader.cpp
#include "DataReader.h"
using namespace std;
vector<Data*> DataReader(string textFile)
{
ifstream aStream;
aStream.open(textFile); //line 11
我查找了 ifstream.open() 和它接受一个字符串和一个模式作为参数。不太确定如何处理这些模式,但我尝试了它们,但它们给出了相同的错误消息,
DataReader.cpp: In function 'std::vector<Data*, std::allocator<Data*> > DataReader(std::string)':
DataReader.cpp:11: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/lib/gcc/sparc-sun-solaris2.9/4.0.3/../../../../include/c++/4.0.3/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
提前感谢您的任何输入/建议。
院长
i'm trying to pass a string from main to another function. this string is a name of text file that needs to be oepened. as far as i can see, i am passing the string alright, but when i try to use ifstream.open(textFileName)
, it doesn't quite work. but when i manually hardcode it as ifstream.open("foo.txt")
, it works just fine. i would need to use this function several times so i would like to be able to pass in a string of text file name..
here's my main
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
#ifndef DATAREADER_H
#define DATAREADER_H
#include "DataReader.h"
#endif
using namespace std;
int main()
{
vector<Data*> database = DataReader("foo.txt");
return 0;
}
header of DataReader
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
using namespace std;
vector<Data*> DataReader(string textFile);
and finally the DataReader.cpp
#include "DataReader.h"
using namespace std;
vector<Data*> DataReader(string textFile)
{
ifstream aStream;
aStream.open(textFile); //line 11
i looked up the ifstream.open() and it takes a string and a mode as parameters. not really sure what to do with the modes, but i tried them but they gave the same error message
DataReader.cpp: In function 'std::vector<Data*, std::allocator<Data*> > DataReader(std::string)':
DataReader.cpp:11: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/lib/gcc/sparc-sun-solaris2.9/4.0.3/../../../../include/c++/4.0.3/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
thank you in advance for any input/suggestions.
Dean
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准流不接受
标准字符串
,只接受c-string
!因此,使用c_str()
传递字符串:the standard streams doesn't accept a
standard string
, onlyc-string
! So pass the string usingc_str()
:试试这个:
我认为你的代码需要将内部 C 字符串传递给 open() 调用。请注意,我现在不在编译器处,因此无法仔细检查这一点。
您可能还想检查此方法的签名:
这里,当从该方法返回向量时,将获取向量的完整副本,这可能会导致计算成本高昂。请注意,它不会复制数据对象,而只会复制指针,但对于大量数据可能不是一个好主意。与字符串输入类似。
考虑一下这个:
Try this:
I think your code needs to take the internal C string to pass to the open() call. Note I'm not at a compiler right now, so can't double check this.
You may also want to check the signature of the this method:
Here, a complete copy of the vector will be taken when it's returned from the method, which could be computationally expensive. Note, it won't copy the Data objects, just the pointers, but with a lot of data might not be a good idea. Similarly with the string input.
Consider this instead:
ifstream
open
接受一个const char*
指针作为参数,使用std 的
获取此指针。您可以在此处查看参数的含义c_str()
函数: :stringifstream
open
takes aconst char*
pointer as parameter, usec_str()
function ofstd::string
to get this pointer. You can see the meaning of the parameters here