- #1
Dembadon
Gold Member
- 659
- 89
Homework Statement
I am trying to write a quick program that will let me experiment with some different file I/O functions. I want to practice taking data from a file, loading it into a 2D array, then performing some analysis on the data obtained from the file.
I have created a .txt file with some sample data, but I am not getting output when I run the program.
I am using Dev-C++ as my compiler.
Homework Equations
Contents of the text file, which I have named "testfile.txt":
Code:
1, 1, 23
0, 2, 22
0, 3, 55
1, 4, 10
1, 5, 3
1, 6, 44
0, 7, 23
1, 8, 25
1, 9, 22
0, 10, 26
1, 11, 27
1, 12, 99
0, 13, 62
1, 14, 45
The first value for each line indicates whether the data values that follow are good. Zero indicates bad data, while 1 indicates good data. I am calling the second data value on each line time, and the third speed.
The Attempt at a Solution
Here is my program:
Code:
// Headers ////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
using namespace std;
// Global Constants //////////////////////////////////////////////////////////
const int MAX_STR_LEN = 80;
const int MAX_ROWS = 50;
const int MAX_COLS = 2;
const int TIME = 0;
const int SPEED = 1;
const char COMMA = ',';
// Funtion Prototypes ////////////////////////////////////////////////////////
/*
name: uploadData
process: obtains good data from file: first value for each line in the file
indicates if the data is good or not. 1 = good, 0 = bad
input: data array to be created, file name, number of good data lines
output: upload success or failure, number of rows in final array (by reference)
dependencies: fstream
*/
bool uploadData( int data[][ MAX_COLS ], char fileName[], int &size );
/*
name: displayData
process: prints data that has been stored by uploadData to the screen as it appears
in the file
input: data array, size of data array, time and speed values (columns)
*/
void displayData( int data[][ MAX_COLS ], int numData, int numCols );
// Main Function //////////////////////////////////////////////////////////////
int main()
{
// initialize function/variables
char fileName[ MAX_STR_LEN ];
int data[ MAX_ROWS ][ MAX_COLS ];
int numData = 0;
// prompt user for file name
cout << "Please enter filename: ";
cin >> fileName;
// check for bad file name
if( !uploadData( data, fileName, numData ) )
{
cout << "ERROR: Data upload failed - Program aborted." << endl;
cout << "Please check the file name and try again." << endl;
system( "pause" );
return 0;
}
// output data to console
displayData( data, numData, MAX_COLS );
// hold screen for user
system( "pause" );
return 0;
}
// Supporting Functions ///////////////////////////////////////////////////////
bool uploadData( int data[][ MAX_COLS ], char fileName[], int &size )
{
// initialize function/variables
int rowIndex = 0, badSpeed, badTime, numData = 0, testVal;
bool success = false;
char dummyChar;
ifstream fin;
// clear and open file
fin.clear();
fin.open( fileName );
// test for a good filename
if( fin.good() )
{
// prime loop
fin >> testVal >> dummyChar;
// loop through the file
while( fin.good() )
{
// test for good data value
if( testVal == 1 )
{
// grab rest of line and assign to array postions
fin >> data[ rowIndex ][ TIME ] >> dummyChar;
fin >> data[ rowIndex ][ SPEED ];
// increment rowIndex
rowIndex++;
// set success flag
success = true;
}
// otherwise skip line in the file
else
{
fin >> badTime >> dummyChar >> badSpeed;
}
// attempt to read more data
fin >> testVal >> dummyChar;
}
// end loop
}
// set size
numData = rowIndex;
// close file
fin.close();
// return success
return success;
}
void displayData( int data[][ MAX_COLS ], int numData, int numCols )
{
// initialize function/variables
int rowIndex, colIndex;
// loop through the rows
for( rowIndex = 0; rowIndex < numData; rowIndex++ )
{
// loop through the columns
for( colIndex = 0; colIndex < MAX_COLS; colIndex++ )
{
// output comma-separated values to the screen
cout << data[ rowIndex ][ TIME ] << COMMA;
cout << data[ rowIndex ][ SPEED ] << endl;
}
}
}
Last edited: