如何用cpp中的地图内容覆盖文件

时间:2021-10-15 21:15:24

I'm trying to overwrite my text file with map contents, can anyone give me the idea on that upto to now i did

我正试图用地图内容覆盖我的文本文件,任何人都可以给我这个想法,直到现在我做了

#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;

int main()
{
    map<int, string> mymap;

    mymap[34] = "hero";
    mymap[74] = "Clarie";
    mymap[13] = "Devil";

    for( map<int,string>::iterator i=mymap.begin(); i!=mymap.end(); ++i)
    {
        cout << (*i).first << ":" << (*i).second << endl;
    }

    // write the map contents to file .
    // mymap &Emp;
    FILE *fp;
    fp=fopen("bigfile.txt","w");
    if(fp!=NULL)
    {
        for(map<int,string>::iterator it =mymap.begin();it!=mymap.end();++it)
        {
            fwrite(&mymap,1,sizeof(&mymap),fp);
        }
        fclose(fp);
    }
}

I'm new to containers. Am i in correct procedure and while writing the map contents to file it is giving me garbage content in file. thanks in advance

我是容器的新手。我在正确的程序中,并在将地图内容写入文件时,它正在给我文件中的垃圾内容。提前致谢

1 个解决方案

#1


Your problem:

Your call to fwrite() is pretty broken.

你对fwrite()的调用非常糟糕。

fwrite() will write a series of bytes to the given file. For example, if we wanted to write an int to the file, we would need to do something like:

fwrite()会将一系列字节写入给定文件。例如,如果我们想在文件中写一个int,我们需要做类似的事情:

int x = 10;
char text[10];
snprintf(text, 10, "%d", x);
fwrite(text, 1, strlen(text), fp);

And for a std::string, we would need to do something like:

对于std :: string,我们需要做类似的事情:

std::string y = "Hello";
fwrite(y.c_str(), 1, y.size(), fp);

Alternatively, you could use fprintf():

或者,您可以使用fprintf():

int x = 10;
std::string y = "Hello";
fprintf(fp, "%d:%s\n", x, y.c_str());

Let's use a C++ tool instead of a C tool:

If we use C++'s std::ofstream, then things are much simpler. In fact, the code will look almost identical to how we use std::cout.

如果我们使用C ++的std :: ofstream,那么事情要简单得多。实际上,代码看起来与我们使用std :: cout的方式几乎相同。

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;

int main() {
    map<int, string> mymap;

    mymap[34] = "hero";
    mymap[74] = "Clarie";
    mymap[13] = "Devil";

    for(map<int,string>::iterator i=mymap.begin(); i!=mymap.end(); ++i)
        cout << i->first << ":" << i->second << "\n";

    // write the map contents to file.
    std::ofstream output("bigfile.txt");
    assert(output.good());

    for(map<int,string>::iterator it =mymap.begin();it!=mymap.end();++it)
        output << it->first << ":" << it->second << "\n";
}

This will output to the screen and write to bigfile.txt this:

这将输出到屏幕并写入bigfile.txt:

13:Devil
34:hero
74:Clarie

#1


Your problem:

Your call to fwrite() is pretty broken.

你对fwrite()的调用非常糟糕。

fwrite() will write a series of bytes to the given file. For example, if we wanted to write an int to the file, we would need to do something like:

fwrite()会将一系列字节写入给定文件。例如,如果我们想在文件中写一个int,我们需要做类似的事情:

int x = 10;
char text[10];
snprintf(text, 10, "%d", x);
fwrite(text, 1, strlen(text), fp);

And for a std::string, we would need to do something like:

对于std :: string,我们需要做类似的事情:

std::string y = "Hello";
fwrite(y.c_str(), 1, y.size(), fp);

Alternatively, you could use fprintf():

或者,您可以使用fprintf():

int x = 10;
std::string y = "Hello";
fprintf(fp, "%d:%s\n", x, y.c_str());

Let's use a C++ tool instead of a C tool:

If we use C++'s std::ofstream, then things are much simpler. In fact, the code will look almost identical to how we use std::cout.

如果我们使用C ++的std :: ofstream,那么事情要简单得多。实际上,代码看起来与我们使用std :: cout的方式几乎相同。

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <utility>
using namespace std;

int main() {
    map<int, string> mymap;

    mymap[34] = "hero";
    mymap[74] = "Clarie";
    mymap[13] = "Devil";

    for(map<int,string>::iterator i=mymap.begin(); i!=mymap.end(); ++i)
        cout << i->first << ":" << i->second << "\n";

    // write the map contents to file.
    std::ofstream output("bigfile.txt");
    assert(output.good());

    for(map<int,string>::iterator it =mymap.begin();it!=mymap.end();++it)
        output << it->first << ":" << it->second << "\n";
}

This will output to the screen and write to bigfile.txt this:

这将输出到屏幕并写入bigfile.txt:

13:Devil
34:hero
74:Clarie