c++:ifstream 打开传递文本文件名字符串的问题

发布于 2024-08-09 22:12:12 字数 1608 浏览 1 评论 0原文

我正在尝试将字符串从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-08-16 22:12:12

标准流不接受标准字符串,只接受c-string!因此,使用 c_str() 传递字符串:

aStream.open(textFile.c_str());

the standard streams doesn't accept a standard string, only c-string! So pass the string using c_str():

aStream.open(textFile.c_str());
烟凡古楼 2024-08-16 22:12:12

试试这个:

aStream.open(textFile.c_str()); //line 11

我认为你的代码需要将内部 C 字符串传递给 open() 调用。请注意,我现在不在编译器处,因此无法仔细检查这一点。

您可能还想检查此方法的签名:

vector<Data*> DataReader(string textFile);

这里,当从该方法返回向量时,将获取向量的完整副本,这可能会导致计算成本高昂。请注意,它不会复制数据对象,而只会复制指针,但对于大量数据可能不是一个好主意。与字符串输入类似。

考虑一下这个:

void DataReader( const string& textFile, vector<Data*>& dataOut );

Try this:

aStream.open(textFile.c_str()); //line 11

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:

vector<Data*> DataReader(string textFile);

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:

void DataReader( const string& textFile, vector<Data*>& dataOut );
拥抱没勇气 2024-08-16 22:12:12

ifstream open 接受一个 const char* 指针作为参数,使用 std 的 c_str() 函数: :string 获取此指针。您可以在此处查看参数的含义

ifstream open takes a const char* pointer as parameter, use c_str() function of std::string to get this pointer. You can see the meaning of the parameters here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文