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

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

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

Восстановить пароль

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

Ответ
 
Опции темы Поиск в этой теме
Старый 22.04.2012, 17:43   #1
mazzahaker
Пользователь
 
Регистрация: 04.12.2011
Сообщений: 23
По умолчанию Ошибка на пустом файле

В общем есть код, помогите пожалуйста убрать ошибку, что возникает при пустом входном файле, как исключить ее, скажите пожалуйста?

Код:
#include <iostream>
#include <fstream>
#include <conio.h>

struct list
{
    int information;
    list *next;
};

void add ( list *&list0, int n )
{
    struct list *pointer;
    if ( !list0 )
    {
        list0 = ( struct list * ) malloc ( sizeof ( struct list ) );
        pointer = list0;
    }
    else
    {
        pointer = list0;
        while ( pointer -> next )
            pointer = pointer -> next;
        pointer -> next = new list;
        pointer = pointer -> next;
    }
    pointer -> information    = n;
    pointer -> next = 0;
}

void print ( list *list0 )
{
    list *pointer = list0;
	while ( pointer != 0 )
    {
        std::cout << pointer -> information << " ";
        pointer = pointer -> next;
    }
	std::cout << std::endl;
}

bool is_exist( list *lst, int n )
{
	list *lst0 = lst;
	
	while ( lst0 )
	{
		if ( lst0 -> information == n )
			return 1;
		lst0 = lst0 -> next;
	}
	return 0;
}

void fillListFromFile( std::ifstream &f, list *&list0 )
{
	int number;
	while ( !f.eof())
	{
		f >> number;
		add( list0, number );
	}
}

void getResultList( list *lst1, list *lst2, list *&res )
{
	list *list1 = lst1, *list2 = lst2;
	while ( list1 )
	{
		int inf = list1 -> information;
		if ( !is_exist( lst2, inf ) && !is_exist( res, inf ) )
			add( res, list1 -> information );
		list1 = list1 -> next;
	}

	while ( list2 )
	{
		int inf = list2 -> information;
		if ( !is_exist( lst1, inf ) && !is_exist( res, inf ) )
			add( res, list2 -> information );
		list2 = list2 -> next;
	}
}

void to_file( std::ofstream &out, list *lst )
{
	list *current = lst;
	while ( current )
	{
		out << current -> information << " ";
		current = current -> next;
	}
}

int main()
{
    setlocale ( LC_ALL, "Russian" );

	list *firstList = 0, *secondList = 0, *resultList = 0;
	std::ifstream first( "first.txt" ), second( "second.txt" );
	std::ofstream result( "result.txt" );

	fillListFromFile( first, firstList );
	std::cout << "Первый список:   ";
	print( firstList ); 
	
	fillListFromFile( second, secondList );
	std::cout << "Второй список:   ";
	print( secondList );

	getResultList( firstList, secondList, resultList );

	std::cout << "Итоговый список: ";
	print( resultList );
	
	to_file( result, resultList );

	first.close();
	second.close();
	result.close();

	getch();
    return 0;
}
mazzahaker вне форума Ответить с цитированием
Ответ


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



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Ошибка в файле)) vetalio PHP 4 15.12.2011 11:04
Ошибка в файле rc fredwriter Помощь студентам 1 31.10.2011 01:15
filesize возвращает ноль, при не пустом файле legendary Общие вопросы Delphi 5 01.02.2011 19:32
Ошибка в .bat файле DreamCrusher Помощь студентам 0 21.12.2009 15:25
как в пустом textarea зафиксировать позицию курсора на первой позиции ? slips HTML и CSS 5 23.10.2009 19:47