Программа останавливается на строке count = countFile(file);
Почему?
Код:
#include <iostream>
#include <fstream>
using namespace std;
int countFile(ifstream& file)
{
int count=0;
while (!file.eof())
count++;
return count;
}
int readFile(ifstream& file, int *a)
{
int i = 0;
while (!file.eof())
{
file >> a[i];
i++;
}
return *a;
}
int main()
{
ifstream file("f.txt");
ofstream fileout("g.txt");
int count;
count = countFile(file);
if (count % 2 == 1)
{
cout << "Нечётное кол-во чисел! Измените кол-во чисел в файле." << endl;
system("pause");
return 0;
}
int *a = new int[count];
*a = readFile(file, a);
for (int j = 0; j <count; j++) {
cout << a[j] << " ";
}
file.close();
fileout.close();
system("pause");
return 0;
}
И как достичь конца файла, ничего не читая? Цикл
while (!file.eof()) count++;
крутится бесконечно, создавая впечатление "остановки"...
Update
Если я правильно понял, что должна делать ваша программа, то вот ее вариант:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file("f.txt");
ofstream fileout("g.txt");
int n, count = 0;
while(file>>n)
{
++count;
fileout << n;
}
if (count % 2 == 1)
{
cout << "Нечётное кол-во чисел! Измените кол-во чисел в файле." << endl;
fileout.close();
_unlink("g.txt");
}
system("pause");
}
Примерно так:
Код:
int main()
{
ifstream in; // Поток in будем использовать для чтения
ofstream out; // Поток out будем использовать для записи
out.open("output.txt");
in.open("input.txt");
char a[100];
int i,n,s;
char tmp;
n=-1;
while (!in.eof()) {
n++;
in>>tmp;
if(!in.eof())
a[n] = tmp;
}
s=0;
for (i=0;i<n;i++){
// if (a[i]='8') s+=2;
//if (a[i]='6') s++;
//if (a[i]='9') s++;
out <<a[i];
}
//out << s <<endl;
in.close();
out.close();
return 0;
}
Оборудование для ресторана: новинки профессиональной кухонной техники
Частный дом престарелых в Киеве: комфорт, забота и профессиональный уход
У меня есть 4 виджета : startWidget, mainWidget, student widget, teacherWidgetВ startWidget я выбираю который открыть student widget или teacherWidget и на заднем фоне у меня открыт mainWidget