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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1662624/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:49:07  来源:igfitidea点击:

c++: ifstream open problem with passing a string for text file name

c++stringparameterstext-files

提问by user200632

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..

我正在尝试将一个字符串从 main 传递给另一个函数。这个字符串是需要打开的文本文件的名称。据我所知,我正在传递字符串,但是当我尝试使用时ifstream.open(textFileName),它不太工作。但是当我手动将其硬编码为 时ifstream.open("foo.txt"),它工作得很好。我需要多次使用这个函数,所以我希望能够传入一串文本文件名..

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

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

最后是 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

我查找了 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>]

thank you in advance for any input/suggestions.

提前感谢您的任何意见/建议。

Dean

院长

回答by AraK

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

标准流不接受 a standard string,只接受c-string! 所以使用传递字符串c_str()

aStream.open(textFile.c_str());

回答by pxb

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.

我认为您的代码需要将内部 C 字符串传递给 open() 调用。注意我现在不在编译器,所以不能仔细检查这个。

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.

在这里,当从方法返回时,将获取向量的完整副本,这在计算上可能很昂贵。请注意,它不会复制 Data 对象,只会复制指针,但如果有大量数据,可能不是一个好主意。与字符串输入类似。

Consider this instead:

考虑一下:

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

回答by Ponting

ifstreamopentakes a const char*pointer as parameter, use c_str()function of std::stringto get this pointer. You can see the meaning of the parameters here

ifstreamopen以一个const char*指针作为参数,使用c_str()of函数std::string来获取这个指针。你可以在这里看到参数的含义