Вывести из текстового файла “input.txt” в файл “output.txt” только четные по счету строки
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int main()
{
int fl = 1;
FILE* f1;
fopen_s(&f1, "input.txt", "r");
char* str = new char[255];
FILE* f2;
fopen_s(&f2, "output.txt", "w");
if (!f2) {
cout << "error 2" << endl;
return 0;
}
if (!f1) {
cout << "error 1" << endl;
return 0;
}
while (!feof(f1)) {
fgets(str,255,f1);
for(int i = 0; i < strlen(str); i++)
if (str[i] == '\0') {
fl++;
if (fl % 2==0) {
fputs(str, f2);
}
}
}
fclose(f1);
return 0;}
не знаю как поделить строки на четные и нечетные
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Раз у вас С++, то и используйте средства С++.
int main()
{
ifstream in("input.txt");
ofstream out("output.txt");
in >> noskipws;
for(char c, even = 0; in >> c; )
{
if (even) out << c;
if (c == '\n') even = 1 - even;
}
}
Проверки, что файлы открыты, допишите сами.