Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- int main()
- {
- const int WRONG_INPUT_FILE = 1;
- const int WRONG_OUTPUT_FILE = 2;
- const int INPUT_FILE_EMPTY = 3;
- std::ifstream input("input.txt");
- if(!input)
- {
- std::cout << "Wrong input file.\n";
- return WRONG_INPUT_FILE;
- }
- if(input.peek() == std::ifstream::traits_type::eof()) // Ez a videóban nem szerepelt, de így vizsgálhatjuk meg, hogy üres-e a fájl.
- {
- std::cout << "The input file is empty.\n";
- return INPUT_FILE_EMPTY;
- }
- int max;
- input >> max; // Fájlban lévő első érték beolvasása max-ba.
- while(!input.eof()) // Használjuk inkább ezt a sima (input) helyett: azt nézi, hogy nem értünk-e a fájl végére: eof = end of file
- {
- int actual;
- input >> actual;
- if(actual > max) // Végigolvasva a fájlt, mindig frissítjük max értékét, ha az aktuálisan olvasott érték nagyobb annál.
- {
- max = actual;
- }
- }
- std::ofstream output("output.txt");
- if(!output)
- {
- std::cout << "Wrong output file.\n";
- return WRONG_OUTPUT_FILE;
- }
- output << "The max value is: " << max << "\n"; // Végül maxot beleírjuk egy output fájlba.
- std::cout << "The program was successfull. Result is in output.txt.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment