Программа: Пример Статический массив строк, считывание из файла.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <fstream> using namespace std; const int N = 15; int read_from_file(const char* fileName, char words[][N], int &lines); void print_words(const char words[][N], const int &lines); int main() { char words[N][N]; int lineCount; if (!read_from_file("Text.txt", words, lineCount)) print_words(words, lineCount); else cout << "file not found" << endl; system("pause"); return 0; } int read_from_file(const char* fileName, char words[][N], int &lines) { ifstream file(fileName); int symbol = 0; lines = 0; char ch = 0; if (!file.fail()) { while (!file.eof()) { ch = file.get(); if (ch == 32) { (*(words + lines++))[symbol] = '\0'; symbol = 0; continue; } (*(words + lines))[symbol++] = ch; } (*(words + lines++))[symbol] = '\0'; file.close(); } else return 1; return 0; } void print_words(const char words[][N], const int &lines) { for (int i = 0; i < lines; i++) { for (int j = 0; words[i][j]; j++) cout << words[i][j]; cout << endl; } } |