空结构已写入文件,想知道我是否正确编写结构。 [英] Empty structure is been written to file, want to know if I am writing the strucutre correctly.

查看:59
本文介绍了空结构已写入文件,想知道我是否正确编写结构。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

空结构已写入文件,因为当我尝试显示其内容时它会给出空格。



我尝试了什么:



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();
	}
}

推荐答案

只有你可以检查它是否写得正确。我们只看到一些代码不足以给你一个有意义的答案。特别是,因为你没有显示写入文件的代码。



然而,有一些代码行使代码不太可能代码正在做你的事情期待:



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))

多次使用。 t1.y 可能是 int (因为你将的返回值分配给了Int() )。但 isdigit()检查传递的字符(视为 int )是否为数字。对于从0x30 tro 0x39('0'到'9')的值,这将是真的。因此当 b4-> displayText()字符串包含48到57时满足条件。这可能不是你想要的。



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.


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();
	}
}


这篇关于空结构已写入文件,想知道我是否正确编写结构。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆