I have used std::fstream
for reading and writing from a file, but it appeared, that after writing i couldn't do reading right away, the console would crash. I tried closing the file after writing and reopening before reading, and got no crashes, so is that the real issue here? Here are the codes for both cases
Without closing:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
std::fstream output("text.txt", std::ios::out | std::ios::in | std::ios::trunc);
if (!output)
{
std::cerr << "Error";
exit(1);
}
char a[10], b[10];
std::cin >> b;
output << b;
output >> a;
std::cout << a;
return 0;
}
With closing/reopening:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
std::fstream output("text.txt", std::ios::out | std::ios::in | std::ios::trunc);
if (!output)
{
std::cerr << "Error";
exit(1);
}
char a[10], b[10];
std::cin >> b;
output << b;
output.close();
output.open("text.txt");
output >> a;
std::cout << a;
return 0;
}