Odpowiedź :
Odpowiedź:
#include <fstream>
#include <iostream>
#include <string>
void example() {
// Przykładowy plik z macierzą j
std::ofstream file{"data2.txt"};
file << "1 2 3 4 5" << std::endl;
file << "6 7 8 9 0";
file.close();
}
void example2() {
// liczby w kolumnie
std::ofstream file{"data3.txt"};
file << "1" << std::endl;
file << "2" << std::endl;
file << "3" << std::endl;
file << "4" << std::endl;
file << "5" << std::endl;
file << "6" << std::endl;
file << "7" << std::endl;
file << "8" << std::endl;
file << "9" << std::endl;
file << "10" << std::endl;
file.close();
}
int main() {
// przykładowe pliki z danymi do odczytu
example();
example2();
// zad 4
std::ifstream file{"data2.txt"};
std::string line;
if (file.is_open()) {
std::cout << file.rdbuf();
}
file.close();
std::cout << std::endl;
std::cout << std::endl;
// zad 5
int buff;
int arr[10];
int i = 0;
file.open("data3.txt");
if (file.is_open()) {
while (file >> buff) {
arr[i] = buff;
std::cout << "arr[" << i << "]= " << arr[i] << std ::endl;
i++;
}
}
file.close();
// zad 6
std::string path;
std::cout << "Podaj nazwe pliku: "; // np. data2.txt albo data3.txt
std::cin >> path;
file.open(path);
if (file.is_open()) {
std::cout << file.rdbuf();
} else {
std::cout << "Nie można otworzyć" << std::endl;
return 0;
}
std::cout << std::endl;
return 0;
}
Może być ?