Here I have two pieces of C++ code that is supposed to write to a file some data. The first one is below and it works:
void ParameterManager::Save()
{
std::ofstream saveFile;
saveFile.open(path, std::ios::trunc | std::ios::out);
if (saveFile.is_open())
{
saveFile << "File opened. Begin saving.\n";
for (int i = 0; i < 4; ++i)
{
saveFile << "Hoppa" << std::endl;
}
}
saveFile.close();
}
The result in output file is:
File opened. Begin saving.
Hoppa
Hoppa
Hoppa
Hoppa
Like expected.
The second one is below and it don't work:
void ParameterManager::Save()
{
std::ofstream saveFile;
saveFile.open(path, std::ios::trunc | std::ios::out);
if (saveFile.is_open())
{
saveFile << "File opened. Begin saving.\n";
for (auto item : map)
{
std::cout << "Hoppa" << std::endl;
saveFile << "Hoppa" << std::endl;
}
}
saveFile.close();
}
Where the map is a hash map containing 4 entries and it is a member of the class where the function Save is implememnted. The result in output file is:
File opened. Begin saving.
The Hoppa lines are printed in terminal but never written to file. I verified in debug mode and the execution write 4 times but the content is not written to file.
I test it on a Virtual machine windows 7 pro. The host is MacBookPro. I use Visual Studio 2013 Pro.
Can you help me please to understand why the second version of code don't work like expected.
A big thanks for all of you.
Welp, I dunno, but it works when I use std::map<int, int> and input 4 std::pair<int, int> and use the same for loop:
http://coliru.stacked-crooked.com/a/e694252e96aebab5
#include <iostream>
#include <fstream>
#include <string>
#include <map>
void save()
{
std::map<int, int> mappa;
for (size_t i = 0; i < 4; ++i) {
mappa.insert(mappa.begin(), std::pair<int, int>(i,i));
}
std::ofstream saveFile;
saveFile.open("test.txt", std::ios::trunc | std::ios::out);
if (saveFile.is_open())
{
saveFile << "File opened. Begin saving.\n";
for (auto it : mappa)
{
saveFile << "Hoppa" << std::endl;
std::cout << "PRINTED LINE" << std::endl;
}
}
saveFile.close();
}
int main() {
save();
}
you can try:
for (auto &item : map)
{
std::cout << item << std::endl;
saveFile << "Hoppa" << std::endl;
}
Related
I wrote the code below to save a vector of struct to a binary file. The problem is the reading stops before all the written data is read.
Changing the reinterpret_cast<const char*> with (char*) or (const char*) does not help or change the output.
Seeing as it does read the right number for the data it does read the code is at least partially right.
Is there something obvious i'm missing? Or why does this not work?
#include <iostream>
#include <fstream>
#include <vector>
struct Pixel {
long long data;
Pixel() {
data = 0;
}
Pixel(long long data) :
data(data)
{}
};
void saveToBin(std::vector<Pixel>* pixels) {
std::ofstream binFile;
binFile.open("test.bin", std::ios::binary);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
for (int i = 0; i < pixels->size(); i++) {
binFile.write(reinterpret_cast<const char*>(&pixels->at(i)),sizeof(Pixel));
}
binFile.close();
}
void readBin(std::vector<Pixel>* pixels) {
std::ifstream binFile;
binFile.open("test.bin", std::ifstream::in);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
Pixel p;
while (binFile.read(reinterpret_cast<char*>(&p), sizeof(Pixel))) {
pixels->push_back(p);
}
binFile.close();
}
int main()
{
std::vector<Pixel> vecOrig;
for (int i = 0; i < 1000; i++) {
vecOrig.push_back(Pixel(i*7));
}
std::vector<Pixel> vecRead;
saveToBin(&vecOrig);
readBin(&vecRead);
std::cout << "starting length : " << vecOrig.size() << " , read length : " << vecRead.size() << std::endl;
for (int i = 0; i < std::min(vecOrig.size(), vecRead.size()); i++) {
std::cout << vecOrig[i].data << " -> " << vecRead[i].data << std::endl;
}
}
binFile.open("test.bin", std::ios::binary);
The output file was opened in binary mode.
binFile.open("test.bin", std::ifstream::in);
The input file was not.
This only matters on operating systems that trace their lineage to MS-DOS, and if your binary file happens to have a 0x0D byte it will be gratuously deleted, when read. Open the input file in binary mode, too.
ifstream fin;
fin.open("C:\\Users\\Zach\\Desktop\\input.txt");
if (!fin)
{
cout << "e";
}
e is printing whether I use the full pathway or just input.txt from a resource file
If the file exists, make sure that you have got the path specified correctly. Since you're running on Windows, you can verify the full path to your executable with the following code.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#define BUFSIZE 4096
std::string getExePath()
{
char result[BUFSIZE];
return std::string(result, GetModuleFileName(NULL, result, BUFSIZE));
}
int main()
{
std::ifstream infile("input.txt");
if (infile.is_open())
{
std::cout << "Success!" << std::endl;
infile.close();
}
else
{
std::cout << "Failed to open input.txt!" << std::endl;
std::cout << "Executable path is ->" << getExePath() << "<-" << std::endl;
}
return 0;
}
This will allow you to verify that your path to the input file is correct, assuming that it's collocated with your executable.
You need to direct output into the ifstream object by using fin << "string"; and not directing to standard out via cout.
I have a vector of strings of 2 folder names vector <myClass> vec_fileNames; which I filled by reading from a fileNames.txt which contains 2 lines:
First
Second
ifstream inFile("c:/file names.txt");
if(!inFile)
{
cout << "File Not Found!\n";
inFile.close();
}
else
{
string line;
myClass class;
while (getline(inFile, line))
{
class.setFileName(line);
vec_fileNames.push_back(class);
}
So, at this point my vec_fileName[0].getFileName = First and vec_fileName[1].getFileName = second
Now I wanted to open files inside the folders who's names are in the vector in a loop so I did this:
for(int i = 0; i < vec_fileNames.size(); i++)
{
string fileName = vec_fileNames[i].getFileName();
ifstream inFile("C:/Program Folder\\" + fileName + "goalFile.txt");
if(!inFile)
{
cout << "File Not Found!\n";
inFile.close();
}
else
{
while (getline(inFile, line))
{
//do something
}
}
So far everything is good except for the file not being opened. Is this even something that can be done in c++ or is there an error in the way I'm opening the file?
I created the same folder structure as you have:
C:\
Program Folder
First
goalFile.txt
Second
goalFile.txt
And ran the following simple code. Node that I don't store the filenames in a class, but directly into a vector.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std; // I'm no fan of this, but you obviously used it.
void loadFileNames(vector<string>& vec_fileNames)
{
ifstream inFile("c:\\file names.txt");
if(!inFile.is_open())
{
cout << "File Not Found!\n";
return;
// inFile.close(); -- no need to close, it is not open!
}
else
{
string line;
while (getline(inFile, line))
{
cout << line << endl;
vec_fileNames.push_back(line);
}
}
}
void openFiles(vector<string>& vec_fileNames)
{
for(int i = 0; i < vec_fileNames.size(); i++)
{
string fileName = vec_fileNames[i];
string path("C:\\Program Folder\\" + fileName + "\\goalFile.txt");
ifstream inFile(path.c_str());
if(!inFile.is_open())
{
cout << "File" << vec_fileNames[i] << "Not Found!" << endl;
}
else
{
cout << "opened file in folder " << vec_fileNames[i] << endl << endl;
string line;
while (getline(inFile, line))
{
cout << line << endl;
}
cout << endl;
}
}
}
int main(int argc, char* argv[])
{
vector<string> fileNames;
loadFileNames(fileNames);
openFiles(fileNames);
return 0;
}
That works, and produces the output:
First
Second
opened file in folder First
First goal file 1
First goal file 2
opened file in folder Second
Second goalfile 1
Second goalfile 2
The lines First goal file 1, etc. are the contents of the two files.
This question already has an answer here:
How to append to a file with fstream fstream::app flag seems not to work
(1 answer)
Closed 8 years ago.
I try to append file in C++. At start file doesn't exists. After operations there is only one line in file instead of five (5 calls of this method). It looks like file is creating, next every write operation file is cleaned out and new string is added.
void storeUIDL(char *uidl) {
fstream uidlFile(uidlFilename, fstream::app | fstream::ate);
if (uidlFile.is_open()) {
uidlFile << uidl;
uidlFile.close();
} else {
cout << "Cannot open file";
}
}
I tried with fstream::in ,fstream::out. How to append string correctly in this file?
Thank you in advance.
edit:
Here is wider point of view:
for (int i = 0; i < items; i++) {
MailInfo info = mails[i];
cout << "Downloading UIDL for email " << info.index << endl;
char *uidl = new char[100];
memset(uidl, 0, 100);
uidl = servicePOP3.UIDL(info.index);
if (uidl != NULL) {
if (existsUIDL(uidl) == false) {
cout << "Downloading mail with index " << info.index << endl;
char *content = servicePOP3.RETR(info);
/// save mail to file
string filename = string("mail_" + string(uidl) + ".eml");
saveBufferToFile(content, filename.c_str());
storeUIDL(uidl);
sleep(1);
} else {
cout << "Mail already exists." << endl;
}
} else {
cout << "UIDL for email " << info.index << " does not exists";
}
memset(uidl, 0, 100);
sleep(1);
}
This works.. std::fstream::in | std::fstream::out | std::fstream::app .
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
char filename[ ] = "Text1.txt";
fstream uidlFile(filename, std::fstream::in | std::fstream::out | std::fstream::app);
if (uidlFile.is_open())
{
uidlFile << filename<<"\n---\n";
uidlFile.close();
}
else
{
cout << "Cannot open file";
}
return 0;
}
It looks like this question has been answered over yonder.
Give this a shot:
fstream uidFile(uidFilename, fstream::out | fstream:: app | fstream::ate);
Edit:
I wrote this code and compiled it in Visual Studio 2012 on Windows 7 x64. It works perfectly for me. It looks like the other answer worked for you, but please let me know if this does as well.
#include <iostream>
#include <fstream>
using namespace std;
void save(char * string)
{
fstream myFile("test.txt", fstream::out | fstream::app);
if(myFile.is_open())
{
myFile.write(string, 100);
myFile << "\n";
}
else
{
cout << "Error writing to file";
}
}
int main()
{
char string[100] = {};
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 100; j++)
{
string[j] = i + 48; //48 is the ASCII value for zero
}
save(string);
}
cin >> string[0]; //Pause
return 0;
}
I am trying a reasonably simple program to test binary input/output. I am basically writing a file with a header (string) and some data (doubles). The code is as follows:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
int main() {
typedef std::ostream_iterator<double> oi_t;
typedef std::istream_iterator<double> ii_t;
std::ofstream ofs("data.bin", std::ios::in);
//-If file doesn't exist, create a new one now
if(!ofs) {
ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
}
else {
ofs.close();
ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
}
//-Write a header consisting of length of grid subdomain and its name
///*
const std::string grid = "Header";
unsigned int olen = grid.size();
ofs.write(reinterpret_cast<const char*>(&olen), sizeof(olen));
ofs.write(grid.c_str(), olen);
//*/
//-Now write the data
///*
std::vector<double> data_out;
//std::vector<std::pair<int, int> > cell_ids;
for(int i=0; i<100; ++i) {
data_out.push_back(5.0*double(i) + 100.0);
}
ofs << std::setprecision(4);
std::copy(data_out.begin(), data_out.end(), oi_t(ofs, " "));
//*/
ofs.close();
//-Now read the binary file; first header then data
std::ifstream ifs("data.bin", std::ios::binary);
///*
unsigned int ilen;
ifs.read(reinterpret_cast<char*>(&ilen), sizeof(ilen));
std::string header;
if(ilen > 0) {
char* buf = new char[ilen];
ifs.read(buf,ilen);
header.append(buf,ilen);
delete[] buf;
}
std::cout << "Read header: " << header << "\n";
//*/
///*
std::vector<double> data_in;
ii_t ii(ifs);
std::copy(ii, ii_t(), std::back_inserter(data_in));
std::cout << "Read data size: " << data_in.size() << "\n";
//*/
ifs.close();
//-Check the result
///*
for(int i=0; i < data_out.size(); ++i) {
std::cout << "Testing input/output element #" << i << " : "
<< data_out[i] << " " << data_in[i] << "\n";
}
std::cout << "Element sizes: " << data_out.size() << " " << data_in.size() <<
"\n";
//*/
return 0;
}
The problem is that when I try to write and read (and then print) both the header and the data it fails (I confirmed that it doesn't read the data then, but displays the header correctly). But when I comment out one of the write sections (header and/or data), it displays that part correctly indicating the read worked. I am sure I am not doing the read properly. Perhaps I am missing the usage of seekg somewhere.
The code runs fine for me. However you never check if the file is successfully opened for writing, so it could be silently failing on your system. After you open ofs you should add
if (!ofs) {
std::cout << "Could not open file for writing" << std::endl;
return 1;
}
And the same thing after you open ifs
if (!ifs) {
std::cout << "Could not open file for reading" << std::endl;
return 1;
}
Or something along those lines. Also I do not understand why you check if the file exists first since you do the same whether it exists or not.
This should work
#include <iostream>
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdint>
int main() {
ifstream fin;
fin.open("input.dat", std::ios::binary | std::ios::in);
if (!fin) {
cerr << "Cannot open file " << "input.dat" << endl;
exit(1);
}
uint8_t input_byte;
while (fin >> input_byte) {
cout << "got byte " << input_byte << endl;
}
return 0;
}