Hello there,

submissiontime length finishtime remainingtime
1031 17:11 574.1025391 MB 1050 17:30 1
1326 22:06 536.0175781 MB 1343 22:23 2
2721 45:21:00 608.1279297 MB 2741 45:41:00 3
32 0:32 575.8115234 MB 51 0:51 4
1161 19:21 652.6259766 MB 1182 19:42 5
937 15:37 288.7597656 MB 946 15:46 6
3087 51:27:00 912.9931641 MB 3117 51:57:00 7


I'm trying to read this Excel (CSV) file and display the elements, I'm only interested in displaying the elements with title above, (1st, 3rd, 5th and 7th coloumns), I managed to do this but, the final line is duplicated and starts by 0 when I use the code below, how can I get red of the last line ?:

the output:

1031 574.1025391 1050 1
1326 536.0175781 1343 2
2721 608.1279297 2741 3
32 575.8115234 51 4
1161 652.6259766 1182 5
937 288.7597656 946 6
3087 912.9931641 3117 7
0 912.9931641 3117 7


The code:

list<FileInfo*>futurelist;

void FileInput()
{

ifstream file ("test1.csv");
string value1;
string value2;
string value3;
string value4;
string value5;
string value6;
string value7;

while ( file.good() )
{
getline ( file,value1, ',' );
getline(file,value2, ',');
getline(file,value3, ',');
getline(file,value4, ',');
getline(file,value5, ',');
getline(file,value6, ',');
getline(file,value7);

FileInfo* fi = new FileInfo();
fi->submissiontime = atoi(value1.c_str());
fi->length= atof(value3.c_str());
fi->finishtime= atoi(value5.c_str());
fi->remainingtime= atoi(value7.c_str());

futurelist.push_back(fi);

}
file.close();
}

int main()
{
FileInput();
cout << futurelist.size()<<endl;

cout << "Contents: ";
list<FileInfo*>::iterator p = futurelist.begin();
while(p != futurelist.end()) {
cout<<endl;
cout << (*p)->submissiontime << " ";
cout << (*p)->length << " ";
cout << (*p)->finishtime << " ";
cout << (*p)->remainingtime << " ";
cout<<endl;
p++;
}
cout << "\n\n";


return 0;
}

First off, please use CODE tags around your code samples in the future; the forum software does not retain the code indentation, and without the formatting the code is almost unreadable.

Second, the data you posted isn't in Comma-Separated-Values (CSV) format, but in a space-separated format. This doesn't seem to be a problem with the code, but it is a bit confusing to those of us reading this and expecting commas. Your code is written to work with a CSV file, so presumably, the actual file is in fact a CSV file.

Third, why did you have a series of separately declared strings (value1, value2, etc.) rather than an array, a structure, or a collection object? Again, it isn't a problem with the program per se, but it seems like a poor design, especially when you later use a list object and thus presumably know how to use collections.

Fourth, you are using a global variable for said list, which is generally considered a poor approach. While this appears to be something of a throw-away program - frankly, I would have used Perl or something like that for a small thing like this - it's still good practice to avoid globals.

Fifth, the list is set to contain objects of type FileInfo, but that class or structure isn't defined anywhere in the given code.

Sixth, you never delete the allocated FileInfo objects. Again, it shouldn't affect the program too much, but it's sloppy coding.

Finally, as for the problem you are having (I was going to get to it eventually, yes), it is occurring because there is a final, empty line in the data file, which - because file.good() is still true - the program tries to read, but gets a fail result back on the first getline(). Because it has already gone into a failed state, the later getline() calls in that loop iteration simply don't get run, leaving the numbers from the previous iteration in them.

Thank you very much and I'm very sorry for troubling you. can you please help me on this, what do I have to do to get red of the last duplicated output line??

You'll want to add a check for file.good() after the first getline() in each iteration, or better still, set up a loop and check each one:

void FileInput()
{

    ifstream file ("test1.csv"); 
    
    while ( file.good() ) 
    {
        string value[7];
        for (int i = 0; i < 6; i++)
        {
            getline ( file,value[i], ',');
            if (file.eof() || file fail())
                break;
        }
        getline ( file,value[6]);   

        if (file.good())
        {
            FileInfo* fi = new FileInfo();
            fi->submissiontime = atoi(value1.c_str());
            fi->length= atof(value3.c_str()); 
            fi->finishtime= atoi(value5.c_str());
            fi->remainingtime= atoi(value7.c_str());

            futurelist.push_back(fi); 
        }
    }

    file.close();
}

As for troubling us, don't worry too much about it, you're new here, and still learning. No one knows what they are doing the first time they post.

Thank you very much, the code is working fine now

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.