Troubleshooting File I/O in Dev-C++

In summary: D array manipulation and file I/OIn summary, The conversation discusses the process of creating a program to practice file input/output and working with 2D arrays. It mentions creating a .txt file with sample data and running into issues with the program not producing any output. The code for the program is also provided, with a correction made regarding a mislabeled reference parameter and a superfluous for loop. The conversation ultimately focuses on the topic of manipulating 2D arrays and working with file I/O.
  • #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:
Physics news on Phys.org
  • #2
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:

FAQ: Troubleshooting File I/O in Dev-C++

Why am I getting an error when trying to open a file in Dev-C++?

There are several possible reasons for this error. First, make sure that you are using the correct file path and name. If the file is located in a different directory, you may need to provide the full path. Additionally, check that the file is not already open in another program or that it has not been deleted. Finally, make sure that you have the correct permissions to access the file.

How do I read from a file in Dev-C++?

To read from a file in Dev-C++, you will need to use the ifstream object. This object allows you to open a file for reading and provides methods for reading data from the file, such as getline() and get(). Remember to close the file when you are finished reading from it.

How can I write to a file in Dev-C++?

To write to a file in Dev-C++, you will need to use the ofstream object. This object allows you to open a file for writing and provides methods for writing data to the file, such as write() and put(). Remember to close the file when you are finished writing to it.

What should I do if I encounter an error while reading or writing to a file?

If you encounter an error while reading or writing to a file, you should use the fail() method to check the status of the file stream. This will allow you to determine the cause of the error and handle it accordingly. Additionally, make sure to check for any errors when opening the file and handle them appropriately.

Can I use relative file paths in Dev-C++?

Yes, you can use relative file paths in Dev-C++. Relative file paths are relative to the current working directory of the program. However, it is always a good idea to provide the full file path to avoid any potential errors.

Similar threads

Replies
1
Views
1K
Replies
2
Views
2K
Replies
4
Views
3K
Replies
2
Views
4K
Replies
15
Views
2K
Replies
2
Views
1K
Replies
23
Views
7K
Replies
3
Views
2K
Back
Top