Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何将文件中的字符串读入std::string变量,其中file是file';s指针_C++_String_C++11 - Fatal编程技术网

C++ C++;如何将文件中的字符串读入std::string变量,其中file是file';s指针

C++ C++;如何将文件中的字符串读入std::string变量,其中file是file';s指针,c++,string,c++11,C++,String,C++11,我想将文件输入中的字符串读取到std::string变量中。 我声明一个指向打开文件的文件指针: FILE*f=fopen(“IN.txt”、“r”) 然后,我使用fscanf()函数阅读: std::string tempStr fscanf(f,“%s”,tempStr)//编译错误 //fscanf(f、%s、&tempStr)//运行时错误 所以,我有两个问题: 1.是否可以修复上述问题(仍然使用FILE*f和fscanf()函数)? 2.我是C程序员,是C++新手。如何用不同的方法解决

我想将文件输入中的字符串读取到
std::string
变量中。
我声明一个指向打开文件的文件指针:

FILE*f=fopen(“IN.txt”、“r”)

然后,我使用
fscanf()
函数阅读:

std::string tempStr
fscanf(f,“%s”,tempStr)//编译错误

//fscanf(f、%s、&tempStr)//运行时错误

所以,我有两个问题:
1.是否可以修复上述问题(仍然使用
FILE*f
fscanf()
函数)?
2.我是C程序员,是C++新手。如何用不同的方法解决这个问题

这是我的代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    string cities[100];
    FILE * f = fopen("IN.txt", "r");
    fscanf(f,"%d",&n);
    for (int i=0; i<n;i++)
    {
        string tempStr;
        fscanf(f,"%s",tempStr);
        cities[i] = tempStr;
    }
    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int n;
串城市[100];
iff流(“IN.txt”);
f>>n;
对于(int i=0;i>城市[i];
}
返回0;
}

更好的方法是,对城市使用
std::vector
(此修改留给读者作为练习)。

您应该使用流类和
std::getline
或直接读取字符串。但是,如果您真的需要,在您的情况下可以执行以下操作:

fscanf(f,"%s",tempStr); // tempStr is declared as char tempStr[N]
cities[i] = std::string(tempStr); // convert to a string
请注意,您最初在
char
缓冲区(超级不安全的btw)中读取,然后将其转换为
std::string
。但如果您可以简单地执行此操作,我认为没有理由使用此代码

fin >> cities[i] 

在循环中,
fin
std::ifstream

如果要直接读取
std::string
可以使用
FILE*
但不能使用
fscanf()
:完成此舞蹈的方法是创建一个流缓冲区,将
文件*
表示为可由
std::istream使用的内容:

#include <iostream>
#include <stdio.h>
class cfilebuf
    : public std::streambuf {
    FILE* file;
    char  c;
    int underflow() {
        int value = fgetc(this->file);
        if (value != EOF) {
            c = value;
            this->setg(&c, &c, &c + 1);
            return c;
        }
        return std::char_traits<char>::eof();
    }
public:
    cfilebuf(FILE* file): file(file) {}
    // to own or not to own? ~cfilebuf() { fclose(this->file; }
};

int main() {
    cfilebuf     sbuf(stdin);
    std::istream in(&sbuf);
    std::string  s;
    if (in >> s) {
         std::cout << "read '" << s << "'\n";
    }
}

如果你想使用C++字符串类,如果你还使用C++文件流类,你的生活会更容易。它们是一起工作的;文件*/COD>不是。当然,它可以用中间字符数组来固定,但是为什么你想这样做。你混淆C和C++。请解释,我是C++新手。你看过文档吗?对于我使用的课程,我发现它缺少什么?它在什么方面不能满足你对知识的渴望?是的,这一点都不复杂OP@LightnessRacesinOrbit:如果存在非平凡的需求,则可能需要使用非平凡的解决方案。将流缓冲区和流打包到标头中,并仅使用它但是,OP说他/她是C程序员:我在编程C++,因为C太复杂了,我无法应付,所以我能想出的任何C程序员都可能是微不足道的!
fin >> cities[i] 
#include <iostream>
#include <stdio.h>
class cfilebuf
    : public std::streambuf {
    FILE* file;
    char  c;
    int underflow() {
        int value = fgetc(this->file);
        if (value != EOF) {
            c = value;
            this->setg(&c, &c, &c + 1);
            return c;
        }
        return std::char_traits<char>::eof();
    }
public:
    cfilebuf(FILE* file): file(file) {}
    // to own or not to own? ~cfilebuf() { fclose(this->file; }
};

int main() {
    cfilebuf     sbuf(stdin);
    std::istream in(&sbuf);
    std::string  s;
    if (in >> s) {
         std::cout << "read '" << s << "'\n";
    }
}
class icfilestream
    : private virtual cfilebuf
    , public std::istream {
public:
    icfilestream(FILE* file)
        : cfilebuf(file)
        , std::ios(static_cast<std::streambuf*>(this))
        , std::istream(static_cast<std::streambuf*>(this)) {
    }
};