Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <sys/types.h>
- #include <type_traits>
- using namespace std;
- int main() {
- // Задача 1. Подсчитать количество слов в чётных строках текста, учитывая, что
- // исходная информация хранится в текстовом файле.
- // Check amount of rows
- ifstream line("../textfile");
- string our_file_in_str;
- __int16_t amount_of_lines = 0,
- spaces = 0; // Tiny variable for checking lines and spaces
- if (line.is_open()) {
- while (getline(line, our_file_in_str)) {
- ++amount_of_lines;
- }
- cout << "Amount of lines: " << amount_of_lines << endl;
- line.close();
- }
- ifstream input_file("../textfile");
- if (input_file.is_open()) {
- cout << "File is opened!\n";
- for (int i = 0; i < amount_of_lines; i++) {
- if (i % 2 == 1) // Bcz, 1 == second row in the file
- {
- getline(input_file, our_file_in_str);
- for (auto i : our_file_in_str) // Checking for spaces (amount of spaces
- // + 1 = amount of words)
- {
- if (i == ' ') {
- spaces++;
- }
- }
- } else {
- getline(input_file, our_file_in_str); // Skip the string
- // cout << our_file_in_str << endl;
- }
- }
- cout << "Amount of spaces: " << spaces << "\n";
- input_file.close();
- } else
- {
- cout << "I can't find any file!" << endl;
- }
- //Задача 2. Написать программу, которая считывает текст из файла и выводит его
- //на экран, меняя местами каждые два соседних слова.
- // 1. Считать текст из файла
- // 2. Преобразовать текст из файла в строковый массив
- // 3. Вывести с помощью цикла for
- ifstream file("test.cpp");
- if (file.is_open()) {
- string word, word2;
- int index_of_word = 1;
- while (file >> word2) {
- if (index_of_word % 2 == 0) {
- cout << word2 << " " << word << " ";
- } else {
- word = word2;
- }
- index_of_word++;
- }
- file.close();
- }
- // if (file.is_open())
- //{
- // string str, str2;
- // int word_count;
- // while (file >> str)
- //{
- // cout << str << " ";
- //}
- // file.close();
- //}
- else {
- cout << "I can't find your file!\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment