Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
empty structure is been written to file as it gives blank spaces when I try to display its contents.

What I have tried:

void writetickets()//second function called after the addcache func
{
	string park1 = "Sign Sammoric/Tickets/";
	park1 += to_string(dat.year()) + "/" + to_string(dat.month()) + "/" + to_string(dat.day());
	if (!is_directory(park1.c_str()))//checks to see if the directory to write file to exists
	{

		if (create_directories(park1.c_str()))//create the directories
		{
			park1.append("/Tickets.dat");
			ofstream file(park1.c_str(), ios::out | ios::app | ios::binary);//opens the file in binary mode
			materials t1;
			if (file.is_open())//checksto se if the file is opened before fany furthe operations
			{
				auto r = material.begin();
				for (r = material.begin();r != material.end();r++)//iterates through the vector container
				{
					file.write((char *)&r, sizeof r);
				}
				file.close();
			}

		}
	}
	else
	{
		park1 += "/Tickets.dat";
		materials t1;
		ofstream file(park1.c_str(), ios::out | ios::app | ios::binary);
		if (file.is_open())
		{
			auto r = material.begin();
			for (r = material.begin();r != material.end();r++)
			{
				file.write((char *)&r, sizeof(r));
			}
			file.close();
		}
	}
}
void addcache()//function used to add to the vector<materials> container also the first func called
{
	materials t1;
	dat = QDate::currentDate();
	strncpy(t1.name1, pickapark->currentText().toStdString().c_str(), 30);
	strncpy(t1.ticketid, edit2->displayText().toStdString().c_str(), 10);
	strncpy(t1.serial, edit1->displayText().toStdString().c_str(), 10);
	l = dat.toString().toStdString();
	strncpy(t1.date, l.c_str(), 15);
	t1.y = day1->currentText().toInt();
	t1.check = 1;
	material.push_back(t1);
}
void viewtickets()
{
	string par = "Sign Sammoric/Tickets/" + to_string(boyear->currentText().toInt()) + "/" + to_string(setmonth1(bomonth->currentText())) + "/" + to_string(boday->currentText().toInt());
	if (is_directory(par.c_str()))
	{
		materials t1;
		par.append("/Tickets.dat");
		ifstream file;
		if (!material.empty());
		material.erase(material.begin(), material.end());
		file.open(par.c_str(), ios::in | ios::binary);
		if (file.is_open())
		{
			while (file.read((char *)&t1, sizeof(t1)))
				material.push_back(t1);
		}
		file.close();
	}
}
Posted
Updated 7-Apr-17 5:51am
v6
Comments
Kevin Marois 6-Apr-17 11:49am    
So what's your question?
Osikwemen 6-Apr-17 11:52am    
want to know if the structure is been written correctly and with all its contents to the file.
[no name] 6-Apr-17 18:47pm    
This is what programming is all about. You are in the drivers seat not us.
1. Learn to use your debugger and make sure the writes are occurring as you expect.
2. Download a hex file reader and check the contents of your resulting file yourself.

Only you can check if it is written correctly. We only see some code which is not enough to gave you a meaningful answer. Especially, because you did not show the code that writes to a file.

However, there are some code lines that make it rather unlikely that the code is doing what you expect:

t1.y = b4->displayText().toInt();
if (isdigit(t1.y))
Used multiple times. t1.y is probably an int (because you assign the return value of toInt()). But isdigit() checks if the passed character (treated as int) is a digit. This will be true for values from 0x30 tro 0x39 ('0' to '9'). So the condition is met when the b4->displayText() string contains "48" to "57". That is probably not what you want.

if (!material.empty());
Does not harm but is just doing nothing.
 
Share this answer
 
Comments
Osikwemen 6-Apr-17 17:13pm    
updated my code to include the write to file function,so if you could look at it again I would really appreciate.
Jochen Arndt 7-Apr-17 3:09am    
As already suggested by pwasser:
Use a hex editor to check the file content.
Your file is binary (contains an int and null terminated strings). So you can not show the content as text.
Osikwemen 7-Apr-17 11:50am    
thanks a lot I used d hex editor until I found a way to use the write function until I got what I wanted.
void writetickets()
{
	string park1 = "Sign Sammoric/Tickets/";
	park1 += to_string(dat.year()) + "/" + to_string(dat.month()) + "/" + to_string(dat.day());
	if (!is_directory(park1.c_str()))//checks to see if the directory to write file to exists
	{

		if (create_directories(park1.c_str()))//create the directories
		{
			park1.append("/Tickets.dat");
			ofstream file(park1.c_str(), ios::out | ios::app | ios::binary);//opens the file in binary mode
			materials t1;
			if (file.is_open())//checksto se if the file is opened before fany furthe operations
			{
				auto r = material.begin();
				for (r = material.begin();r != material.end();r++)//iterates through the vector container
				{
					file.write(reinterpret_cast<char *>(&(*r).name1), sizeof((*r).name1));
					file.write(reinterpret_cast<char *>(&(*r).ticketid), sizeof((*r).ticketid));
					file.write(reinterpret_cast<char *>(&(*r).serial), sizeof((*r).serial));
					file.write(reinterpret_cast<char *>(&(*r).date), sizeof((*r).date));
					file.write(reinterpret_cast<char *>(&(*r).y), sizeof((*r).y));
					file.write(reinterpret_cast<char *>(&(*r).check), sizeof((*r).check));
				}
				file.close();
			}

		}
	}
	else
	{
		park1 += "/Tickets.dat";
		materials t1;
		ofstream file(park1.c_str(), ios::out | ios::app | ios::binary);
		if (file.is_open())
		{
			auto r = material.begin();
			for (r = material.begin();r != material.end();r++)
			{
				file.write((char *)&r, sizeof(r));
			}
			file.close();
		}
	}
}
void addcache()
{
	materials t1;
	dat = QDate::currentDate();
	strncpy(t1.name1, pickapark->currentText().toStdString().c_str(), 30);
	strncpy(t1.ticketid, edit2->displayText().toStdString().c_str(), 10);
	strncpy(t1.serial, edit1->displayText().toStdString().c_str(), 10);
	l = dat.toString().toStdString();
	strncpy(t1.date, l.c_str(), 15);
	t1.y = day1->currentText().toInt();
	t1.check = 1;
	material.push_back(t1);
}
void viewtickets()
{
	string par = "Sign Sammoric/Tickets/" + to_string(boyear->currentText().toInt()) + "/" + to_string(setmonth1(bomonth->currentText())) + "/" + to_string(boday->currentText().toInt());
	if (is_directory(par.c_str()))
	{
		materials t1;
		par.append("/Tickets.dat");
		ifstream file;
		if (!material.empty());
		material.erase(material.begin(), material.end());
		file.open(par.c_str(), ios::in | ios::binary);
		if (file.is_open())
		{
			while (!file.fail())
			{
				file.read(reinterpret_cast<char *>(&t1.name1), sizeof(t1.name1));
				file.read(reinterpret_cast<char *>(&t1.ticketid), sizeof(t1.ticketid));
				file.read(reinterpret_cast<char *>(&t1.serial), sizeof(t1.serial));
				file.read(reinterpret_cast<char *>(&t1.date), sizeof(t1.date));
				file.read(reinterpret_cast<char *>(&t1.y), sizeof(t1.y));
				file.read(reinterpret_cast<char *>(&t1.check), sizeof(t1.check));
				material.push_back(t1);
			}
				
		}
		file.close();
	}
}
 
Share this answer
 
v3
Comments
Richard Deeming 7-Apr-17 11:58am    
If you want to update your question, click the green "Improve question" link at the bottom of your question.

DO NOT post your update as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900