Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. // read_vectors_zuhra.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <fstream>
  6. #include <iostream>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. bool check_of_reading_file(FILE *file, const char* filename_p) {
  12.     if((file=fopen(filename_p,"r")) == NULL) {
  13.         printf("Ошибка при открытии файла.\n");
  14.         return false;
  15.     }
  16.     return true;
  17. }
  18.  
  19. bool check_of_writing_file(FILE *file, const char* filename_p) {
  20.     if((file=fopen(filename_p,"w")) == NULL) {
  21.         printf("Ошибка при открытии файла.\n");
  22.         return false;
  23.     }
  24.     return true;
  25. }
  26.  
  27.  
  28. vector<vector<float>> read_data(const char* filename, int rows, int columns) {
  29.    
  30.     vector<vector<float>> data;
  31.     data.resize(rows);
  32.     for (int i = 0; i < rows; i++)
  33.         data[i].resize(columns);
  34.  
  35.  
  36.     FILE *file = NULL;
  37.     if (check_of_reading_file(file, filename)) {
  38.         file = fopen(filename, "r");
  39.         for (int i = 0; i < rows; i++) {
  40.             for (int j = 0; j < columns; j++) {
  41.                 fscanf(file, "%f", &data[i][j]);
  42.             }
  43.         }
  44.     }
  45.  
  46.     return data;
  47. }
  48.  
  49.  
  50. void print_data(const char* filename, vector<vector<float>> vec) {
  51.  
  52.     FILE *file = NULL;
  53.     if (check_of_writing_file(file, filename)) {
  54.         file = fopen(filename, "w");
  55.         for (int i = 0; i < vec.size(); i++) {
  56.             for (int j = 0; j < vec[i].size(); j++) {
  57.                 fprintf(file, "%f  ", vec[i][j]);
  58.             }
  59.             fprintf(file, "\n");
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. int main() {
  67.     vector<vector<float>> good = read_data("good.txt", 11, 11);
  68.     print_data("vyvod.txt", good);
  69.     system("pause");
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement