Hello
I am writing a program that is supposed to read from an input txt file values into a 2d array.
The file has about 13500 lines of 75 values each, separated by spaces. at the end of each line there is no space, just '\n'
I need to make some operations with the values in the file, so I will have to read the file into a 2d array.
The thing is, because of the end of line not having a space character, I am having alot of trouble reading the values into the array.
Can anyone help me?
Code:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <time.h>
#include <fstream>
#include <cstdlib>
#include <istream>
#include <iomanip>
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define SCREEN_HEIGHT 50
fstream datafile; /*Original data file*/
ofstream outputfile;
string infilename,outfilename,A[13316][75],line;
int option,menuin,menuout,menualg,number_of_lines,col;
double matrix[13316][75],sum=0;
void clrscr()
{
int i;
for ( i = 0; i < SCREEN_HEIGHT; i++ )
putchar ( '\n' );
}
int readfromfile() /*Reads the first line of the file into variable "line"*/
{
if (datafile.is_open())
{
getline (datafile,line);
datafile.close();
}
return 0;
}
int writetofile () /*Test function. Writes "line" to the output file.*/
{
cout << "\nThe line\n \n" << line << "\n\nwas copied variable line" << endl;/*Gives feedback of the copied line to user.*/
return 0;
}
string Inputfilename()
{
clrscr();
cout << "What is the name of your data input file?\n";
cin >> infilename;
datafile.open(infilename);
while(!datafile)
{
cout << "Filename invalid. Try again!\n";
cin >> infilename;
datafile.open(infilename);
}
if (datafile) /*Verificare existenta fisier*/
{
cout << "Filename saved. Press any key...\n";
getchar();
}
return infilename;
}
string Outputfilename()
{
clrscr();
cout << "What is the name of your data output file?\n";
cin >> outfilename;
outputfile.open(outfilename);
while(!outputfile)
{
cout << "Filename invalid. Try again!\n";
cin >> outfilename;
outputfile.open(outfilename);
}
if (outputfile) /*Verificare existenta fisier*/
{
cout << "Filename saved. Press any key...\n";
getchar();
}
return outfilename;
}
int linesinfile() /*Calculates the number of lines in datafile(the number of rows in matrix)*/
{
datafile.open(infilename);
cout << "Computing number of rows...\n";
number_of_lines=0;
std::string liney;
while (std::getline(datafile, liney))
{
++number_of_lines;
}
datafile.close();
std::cout << "Number of rows is: " << number_of_lines <<"\n";
return number_of_lines;
}
int columns() /*Calculates the number of columns in datafile*/
{
datafile.open(infilename);
cout << "Computing number of columns...\n";
col=0;
for (int i=0;i<=line.size();i++)
if (line[i]==' ')
col++;
col = col++;
datafile.close();
cout << "Number of columns is: " << col <<"\n";
return col;
}
int Algorythm(int N, int M)
{
clrscr();
datafile.open(infilename);
if(!datafile) //Always test the file open.
{
cout << "Error opening output file" << endl;
system("pause");
}
cout << "Reading file to a string matrix...\n";
while(!datafile.eof())
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
getline(datafile,A[R][C],' ');
}
cout << A[0][74];
/*cout << "Converting string matrix to a double matrix...\n";
for(int R=0;R<N;R++)
for(int C=0;C<M-1;C++)
matrix[R][C] = atof(A[R][C].c_str());
for(int i=0;i<M-1;i++)
cout << A[0][i] << "\n";
cout << A[1][0];
getchar();
cout << "Writing the matrix as double in the output\n";
outputfile.open(outfilename);
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
matrix[R][C] = stod( A[R][C] );
outputfile << matrix[R][C] << ' ';
}
outputfile.close();*/
return 0;
}
int main()
{
cout << "This program reads the 0.3 step data from an input file and generates an output file with 3 step data.\nPress any key to start...\n";
Inputfilename();
Outputfilename();
readfromfile();
columns();
linesinfile();
Algorythm(number_of_lines,col);
getchar();
return 0;
}