Hello,
I've got a simple program intended to read in one column of float data from a .txt file (with a variable number of lines) into a dynamically allocated array. The array contents should then be written to the screen, to test if this was successful.

However, the code output is all gibberish (zeros or numbers like 5.60519e-045 and 1.62236e-007).

I'm pretty sure the problem is in dynamically allocating the array, since if I try using a static array for a known number of lines, the code works fine. However, since I'm running it for a large number of files with different numbers of lines I really want to get the dynamically allocated version to work.

The relevant code is provided below.

Thanks for any help you can provide.

/* This program should read in the name of a file, 
 * access the file, and read one column of float data 
 * (with a variable number of lines) into a 
 * dynamically allocated array.  The array contents
 * should then be written to the screen, as a check.
 */
 
 #include<iostream>
 #include<sstream>
 #include<fstream>
 
int main()
{   
    using namespace std;

    // Get filename from user and open file

//<snip>
    
    // Determine number of lines in file
    int nLines = 0;
    while (!data.eof())
    {
          string line;
          getline (data, line);
          nLines++;
    }
    cout << "number of lines = " << nLines << endl;
    
    // Declare dynamically allocated array
    float* array = new float[nLines];
    
    // Loop to extract data from file to array,
    // and output to screen
    
    for (int i = 0; i < nLines; i++)
    {
        data >> array[i];
        cout << array[i] << endl;
    }
    data.close();
    
    // Clean up array
    delete[] array;
    
    //end program
    cin.clear();
    cin.ignore(255, '\\n');
    cin.get();
    return 0;
}

Alas, your program now can't read a file in dynamic or static arrays.
See:

string line;
    ifstream data(fName);
    // Determine number of lines in file
    int nLines = 0;
    /* *** You count eof too (no this line but nLines++...)
    while (!data.eof())
    {
//          string line; // *** move this declaration out of the loop.
          getline (data, line);
          nLines++;
    }
    */
    while (getline(data,line)) // *** that's right counter
        ++nLines;
    cout << "number of lines = " << nLines << endl;
    
    if (nLines > 0) // *** Don't try to allocate zero size array...
    {
        // Declare dynamically allocated array
        double* array = new double[nLines]; // *** don't use float
    
        // Loop to extract data from file to array,
        // and output to screen
        data.clear();    // *** hic! data file was in bad state (eof)
        data.seekg(0); // *** rewind file! (get cursor stands on the eof)
        // ... *** Go on...

Apropos, why <sstream>? You need <string> only...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.