Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 11.01.2014, 04:53   #1
JinAnton
Пользователь
 
Регистрация: 04.01.2011
Сообщений: 54
Восклицание fread творит что-то непонятное

Вот код:
Код:
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <locale.h>
#include <windows.h>
#include <sys/stat.h>

struct mystruct {
	float edge;
	char color[20];
	char material[20];
};

int main()
{

	int st = 0;
	printf("Enter number corresponding to the necessary functions:\n1.Overwriting a file\n2.Adding a new data file\n3.Calculation of the total amount of the selected color cubes\n");
	scanf_s("%d", &st);
	if (st != 1 && st != 2 && st != 3)
	{
		return 0;
	}
	if (st == 1)
	{
		int amount = 0;
		int b = sizeof(mystruct);
		printf("Enter amount cubes:");
		scanf_s("%d", &amount);
		FILE *pFile;
		pFile = fopen("file.dat", "wb");
		mystruct* block = new mystruct[amount];
		for (int i = 0; i<amount; i++)
		{
			printf("\nEnter the size of the edges %d block: ", i + 1);
			scanf_s("%f", &block[i].edge);
			printf("\nSpecify the color %d block: ", i + 1);
			getchar();
			gets_s(block[i].color);
			printf("\nspecify material %d block: ", i + 1);
			gets_s(block[i].material);
		}
		fwrite(&block, b, amount, pFile);
		fclose(pFile);


	}
	if (st == 2)
	{
		int newamount = 0;
		int b = sizeof(mystruct);
		printf("Enter amount new cubes:");
		scanf_s("%d", &newamount);
		FILE *pFile;
		pFile = fopen("file.dat", "ab");
		mystruct* block = new mystruct[newamount];
		for (int i = 0; i<newamount; i++)
		{
			printf("\nEnter the size of the edges %d block: ", i + 1);
			scanf_s("%f", &block[i].edge);
			printf("\nSpecify the color %d block: ", i + 1);
			getchar();
			gets_s(block[i].color);
			printf("\nspecify material %d block: ", i + 1);
			gets_s(block[i].material);
		}
		fwrite(&block, b, newamount, pFile);
		fclose(pFile);
	}
	struct stat fi;
	stat("file.dat", &fi);
	int b = sizeof(mystruct);
	int amount = fi.st_size / b;
	mystruct* cub = new mystruct[amount];
	FILE *rFile;
	rFile = fopen("file.dat", "rb");
	if (rFile == NULL)
	{
		printf("Erorr");
		return 0;
	}
	fread(&cub, b, amount, rFile);
	for(int i=0;i<amount;i++)
	{
		printf("%f %s %s\n",cub[i].edge, cub[i].color, cub[i].material);
	}
	fclose(rFile);

	system("pause");
	return 0;
}
После fread переменные amount и st(которая даже не имеет к нему никакого отношения) принимают непонятные мне значения, если до него amount = 5, а st=3, то после amount = 257454928 а st = 1994187029
Что происходит и как исправить?
JinAnton вне форума Ответить с цитированием
Старый 11.01.2014, 04:55   #2
JinAnton
Пользователь
 
Регистрация: 04.01.2011
Сообщений: 54
По умолчанию

Ой забыл написать, а редактировать не знаю как, C++, VS 2013, в VS C++ 2010 Express происходит тоже самое...
JinAnton вне форума Ответить с цитированием
Старый 11.01.2014, 10:12   #3
Son Of Pain
Участник клуба
 
Регистрация: 23.12.2010
Сообщений: 1,129
По умолчанию

Убери там знак & . Ты ж и так в указатель читаешь.
Son Of Pain вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Перегрузка базы или что то непонятное... Mr_freeman SQL, базы данных 3 05.05.2012 22:14
что-то такое непонятное... Alekseika Безопасность, Шифрование 0 07.02.2012 20:50
Что то непонятное с dev-cpp Ulysses Общие вопросы C/C++ 5 21.06.2010 09:49
Что-то непонятное с видеокартой... dimorik Компьютерное железо 5 22.12.2009 17:43
Что-то непонятное.. Busine2009 Microsoft Office Word 4 10.07.2009 21:44