Hi there,

I am having a problem with the following code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int findRows( string line );

int main()
{
    string line;
    int rows = findRows( line );

    cout << "Number of rows: " << rows << endl;

}

int findRows( string line )
{
    int Rows = 0;

    ifstream inData;                        // Create new input file stream object
    inData.open( "input.dat" );

    if ( !inData )
        cout << "Error in opening file" << endl;;
        return 0;

    while( getline( inData, line ) )        // While there is still a line, add number of rows
    {
        Rows++;
        cout << line << endl;
    }

    return Rows;
}

The file doesn't seem to open, but displays the error message. However, it works if I move the above function code into main. How do I fix this?

Thanks :)

Recommended Answers

All 10 Replies

> int findRows( string line ) : Why are you passing a string as argument, you can also declare a variable inside your function instead ...

> return 0; : I would return -1, as a file can also contain 0 lines :) ...

>There's some unreachable code in your function:

if ( !inData )
        cout << "Error in opening file" << endl;;
        return 0; // this doesn't belong to the if statement (it will always execute)

change it to

if ( !inData )
{
        cout << "Error in opening file" << endl;;
        return 0;
}

(if you want to execute more than one function when an expression in an if-statement is true, you should always use a compound statement ( {} ) ...
Your program should work now :) ...

commented: You are always very helpful :) +36

Thanks for this very excellent reply!

Can I ask something else...

Say I have a number of dates and times in a file, for example:

2008/11/22 14:59 ... t1
2008/11/22 15:05 ... t2
2008/11/22 15:12 ... t3
2008/11/22 15:18 (the max time difference is 6-7 mins)
...

These values can then be read into an array/vector, separating year, month, day and time (somehow...). Now, what I want is the difference between successive time intervals (in seconds) for a calculation, i.e. t2 - t1, t3 - t1 and so on. This presents two problems:

1) How could time (in the form of HH: SS above) be represented in the array, so that they can be subtracted? How do you subtract when
1.1) For example, 09 - 05 gives an octal error, and
1.2) If the clock transitions from a 50-something to a "single digit" the difference is a huge negative number. (eg. 05 - 59 = -54). What now?

I can't use parsing also.

Thanks again :)

EDIT: It's still giving an error message, even with braces... What now?

EDIT: It's still giving an error message, even with braces... What now?

You must have done something wrong as your program compiles and runs fine with me :) ...

Oh ... I meant that the program runs but displays the error message, "Error in opening file." Howcome? I got it to work, moving it to main.

Oh ... I meant that the program runs but displays the error message, "Error in opening file." Howcome? I got it to work, moving it to main.

I've already explained that in one of my previous posts :) ...

>>1) How could time (in the form of HH: SS above) be represented in the array, so >>that they can be subtracted? How do you subtract when

Don't put the dates/times in an array at all.

Convert the time read from each line into seconds, then all you have to do is subtract the seconds. Something similar to time_t variables.

One way to do that is to populate a struct tm structure with the date/time, then call mktime() to get the time_t object. You will have to parse the line read from the file
Example:

#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;

int main()
{
    struct tm time;
    time_t t1, t2, t3;
    char c;

    // populate first time
    std::string t = "2008/11/22 14:59"; // assume this is read from file
    stringstream str(t);
    memset(&time, 0, sizeof(struct tm));
    str >> time.tm_year >> c; // get year and / character
    str >> time.tm_mon >> c;
    str >> time.tm_mday;
    str >> time.tm_hour >> c;
    str >> time.tm_min >> c;
    // subtract 1 from month so that it is between 0 and 11
    time.tm_mon -= 1; 
    // substract 1900 from year
    time.tm_year -= 1900;
    t1 = mktime(&time);

// now to the same with the other times t2 and t3
}
commented: You're always great in simplifying things :) +3

:D I did change it though...

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int findRows( string line );

int main()
{
    string line;
    int rows = findRows( line );

    cout << "Number of rows: " << rows << endl;

}

// Function to calculate the number of rows in the data
int findRows( string line )
{
    int Rows = 0;

    ifstream inData;                        // Create new input file stream object
    inData.open( "input.dat" );

    if ( !inData )
    {
        cout << "Error in opening file" << endl;
    }

    // Find the number of rows in the data
    while( getline( inData, line ) )        // While there is still a line, add number of rows
    {
        Rows++;
        cout << line << endl;
    }

    return Rows;
}

:D I did change it though...

Change

if ( !inData )
{
     cout << "Error in opening file" << endl;
}

to

if ( !inData )
{
     cout << "Error in opening file" << endl;
     return -1;
}

That data file may not be in the directory (folder) you think it is. Move it to the same directory where the *.exe executable program is located.

:$ Sorry... you're right. Wrong directory:-/ . Thanks for the help with the time problem also.

Much appreciated...

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.