1

I am reading in times from a list in a text file, with format in the following manner

1      00:03.56
2      00:06.12

I need to take in every other line, and do some analysis on this, but, when I read it in and try to use the "std::stof()" function to convert this to a usable form, it hangs on the very first conversion (10+ minutes at a single core on a 4 core i7). The relevant code is attached.

while (tf.is_open()) {
i++;
//std::cout << i << std::endl;
std::getline(tf, full);
std::stringstream tbreak;
tbreak.str(full);
int k = 0;
std::string milsec;
while (std::getline(tbreak, time, ' '))
{
//  std::cout << k << std::endl;
    if (k % 2 == 0) {
        k++;
        continue;
    }
    std::stringstream hrminsec;
    hrminsec.str(time);

    int m = 0;
    while (std::getline(hrminsec, brokentime, ':'))
    {
        //std::cout << brokentime << std::endl;

        if (m % 2 == 0)
        {
            m++;
            continue;
        }
        //if (m % 2 == 1) time = brokentime;
        //if (m % 3 == 2) milsec =brokentime;
        if (brokentime == "time" || brokentime == "times")
        {
            m++;
            continue;
        }
        //std::cout << brokentime << std::endl;
        std::stringstream further;
        int n = 0;
        further.str(brokentime);
        std::string temp;
        while (std::getline(further, temp, '.'))
        {
            //std::cout << temp << std::endl;
            if (n % 2 == 0) time = temp;
            time.erase(0, time.find_first_not_of('0'));
            //std::cout<<time <<std::endl;
            if (n % 2 == 1)milsec = temp;
            n++;
            if (i % 2 == 0) {
                float temp1 = std::stof(time);
                std::cout << temp1 << std::endl;
                float temp2 = std::stof(milsec);
                temp1 = temp1 + 0.01*temp2;
                int j = i - 2;
                t[j] = temp1;
                std::cout << j << std::endl;
            }
        }


        m++;
    }
    k++;
}

This compiles fine, and most of the sub loops were to get the compilation correct. I am using Visual Studio 15 on Windows 10 if that matters.

  • How can you say that it is hung at `stof`? – Shiv Dec 17 '17 at 08:12
  • Please post full code so that it is easier to compile and debug code so that others can test it easily. – Shiv Dec 17 '17 at 08:13
  • there is a lot of while loops here doing getline. you said you were reading from one file ? i would expect one while loop reading each line then deciding what to do with the line. – AndersK Dec 17 '17 at 08:21
  • I have added it to a github directory here (https://github.com/silas-gross/Milikan_data_analysis) – Silas K Grossberndt Dec 17 '17 at 22:56
  • Sample data file has been added and updates have been made, the issue arose from placing the "if" loop with the stof one layer to deep, it was hanging on a null defined string. Now just having issues with an stof argument – Silas K Grossberndt Dec 18 '17 at 00:58

1 Answers1

0

That "hang" is almost surely* an (unhandled) exception thrown by stof. And that exception is very likely the result of an empty string read from the input, or one that really doesn't look like a float.

The exact, specific cause that derails the input readings is hard to tell, but that's where you should be looking. I can see you've debugged time, but also check if milsec had been correctly read, too.


* I've had this exact same problem myself. But don't ask, why the occasional silent hang is the way of "handling" unhandled exceptions in MSVC (also in debug builds).

Sz.
  • 3,342
  • 1
  • 30
  • 43