Troubleshooting File I/O in Dev-C++

  • Context: Comp Sci 
  • Thread starter Thread starter Dembadon
  • Start date Start date
  • Tags Tags
    File Troubleshooting
Click For Summary
SUMMARY

The discussion focuses on troubleshooting file input/output (I/O) issues in a C++ program using Dev-C++. The user encountered problems with reading data from a text file named "testfile.txt" into a 2D array. Key issues identified include a mislabeled reference parameter for the size of the 2D array in the uploadData function and an unnecessary second loop in the displayData function that caused duplicate output. These corrections are essential for successful data processing and display.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with file handling using fstream
  • Knowledge of 2D arrays in C++
  • Basic debugging techniques in C++
NEXT STEPS
  • Review C++ file handling with ifstream and ofstream
  • Learn about reference parameters in C++ functions
  • Study the use of loops and conditionals in C++ for data processing
  • Explore debugging tools available in Dev-C++ for effective troubleshooting
USEFUL FOR

C++ developers, students learning file I/O operations, and anyone looking to improve their debugging skills in C++ programs.

Dembadon
Gold Member
Messages
660
Reaction score
88

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:
Physics news on Phys.org
Just figured out what was going on. My reference parameter for the size of my 2D array is mislabeled. :redface:

Edit: Also just realized the 2nd for loop in my display function is completely superfluous and causing double output. :redface:2
 
Last edited:

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K