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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <fstream>
#include <istream>
#include <iomanip>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
/*Declaring unviversal variables*/
ifstream datafile ("randaex1.txt"); /*Original data file*/
ifstream testfile ("test1.txt");
ofstream outputfile; /*Output data file*/
string line,a,b,auxiliar;
stringstream ss;
int number_of_lines,col,matrix[100][15000];
string A[4][5]; /*= {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};*/
int readfromfile() /*Test function. Reads the first line of the file into variable "line"*/
{
if (datafile.is_open())
{
getline (datafile,line);
datafile.close();
}
else
cout << "Unable to open file";
return 0;
}
int writetofile () /*Test function. Writes "line" to the output file.*/
{
outputfile.open ("example.txt");
outputfile << line << "\n";
outputfile.close();
cout << "\nThe line\n \n" << line << "\n\nwas copied to example.txt" << endl;/*Gives feedback of the copied line to user.*/
return 0;
}
int sizeofline() /*Test function. Calculates the size of a line*/
{
cout << "\nThe size of line is " << line.size() << " characters.\n";
return line.size();
}
int linesinfile() /*Calculates the number of lines in datafile(the number of rows in matrix)*/
{
number_of_lines=0;
std::string liney;
std::ifstream myfile("randaex1.txt");
while (std::getline(myfile, liney))
{
++number_of_lines;
}
std::cout << "Number of lines in text file: " << number_of_lines;
system("pause");
return number_of_lines;
}
int columns() /*Calculates the number of columns in datafile*/
{
col=0;
for (int i=0;i<=line.size();i++)
if (line[i]==' ')
col++;
col = col++;
cout << col;
return col;
}
void Readm(int N, int M) /*Test function. Reads matrix*/
{
if(!datafile) //Always test the file open.
{
cout << "Error opening output file" << endl;
system("pause");
}
while(!datafile.eof())
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
getline(datafile,A[R][C],' ');
}
cout << A[1][1];
}
void Displaym(int N, int M) /*Displays matrix*/
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
cout << A[R][C];
cout << endl;
}
}
int main()
{
readfromfile();
writetofile();
sizeofline();
getchar();
linesinfile();
getchar();
columns();
getchar();
/*int cc=columns();
int rr=linesinfile();*/
Readm(13316,75);
/*Displaym(13316,75);*/
return 0;
}
|