#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile("text.txt", ios::trunc);
std::ifstream infile("text.txt", ios::trunc);
outfile.seekp(0);
std::cout << "This is a file";
infile.seekg(0, ios::end);
int length = infile.tellg();
infile.read(0, length);
infile.close();
outfile.close();
return 0;
}
私はこれの背後にあるアイデアを得ると思いますが、と私はかなり確信しています)私は何をやっているのか分かりません。私はそれを見て、すべてが私を混乱させました。私はC + +のリファレンスを読んで、私はそれをgoogled、しかし私はまだ間違っていることを理解していない。私はファイルストリームなどを設定しようとしていますが、私は何をすべきかについて非常に混乱しています
#include <iostream>
#include <fstream>
#include <cstring>
int main() {
std::fstream file("text.txt", std::ios_base::in | std::ios_base::out);
file << "This is a file";
int length = file.tellg();
std::string uberstring;
file >> uberstring;
std::cout << uberstring;
char *buffer = new char[length + 1];
file.read(buffer, length);
buffer[length] = '\0';
file.close();
delete [] buffer;
return 0;
}
私はこれを試しましたが、何も印刷していません。なぜこれは機能しないのですか?
[this](http://www.cplusplus.com/doc/tutorial/files/)にチェックを入れましたか?それはより良い理解を与えるかもしれません。 – unexplored