Time stamp from text file

Hello everyone and thank you for your time. I recently have been trying to create a C++ program that can pull a information from a text file. The information being the time stamp part in the text file. My text file is called "log.txt" and I cannot get my program to pull just the time stamp portion from my file. There are multiple time stamps on this file as well and I am trying to get all of them. I want the program to read out "timestamp="2014-07-08T18:13:40.837Z."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inFile;
	string tempString;
	ofstream outFile;

	inFile.open ("log.txt");
	outFile.open ("timestamp.dat");
	while (inFile)
	{
	inFile >> tempString;
	if (tempString.find ('=') != string::npos);
	outFile << tempString;
	}


outFile << "Time Stamp:";
inFile.close();
outFile.close();

cin.get();
system("pause");
return 0;
}
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

std::vector<std::string> read_timestamps( std::istream& stm, std::string prefix = "timestamp=" )
{

    const std::size_t prefix_size = prefix.size() ;

    std::vector<std::string> result ;

    std::string line ;
    while( std::getline( stm, line) ) // for each line in the file
    {
        const auto pos = line.find(prefix) ; // search for the prefix in the line
        if( pos != std::string::npos ) // if found
        {
            const auto timestamp_start = pos + prefix_size ; // the next character after the prefix

            // create a string stream which reads from the substring starting after the prefix
            std::istringstream strstm( line.substr(timestamp_start) ) ;
            std::string timestamp ;
            if( strstm >> timestamp /* && !timestamp.empty() */ ) // if a timestamp was read
                 result.push_back(timestamp) ; // add it to the result
        }
    }

    return result ;
}

int main()
{
    std::istringstream file( "blah= blah timestamp=2014-07-08T18:13:40.837Z. blah blah\n"
                             "timestamp= 2015-07-08T18:13:40.837Z. blah=blah\n"
                             "blah blah timestamp=    2016-07-08T18:13:40.837Z.\n"
                             "blah blah does not contain timestamp blah blah\n"
                             "timestamp=2017-07-08T18:13:40.837Z.") ;
    for( std::string timestamp : read_timestamps(file) ) std::cout << timestamp << '\n' ;
}

http://coliru.stacked-crooked.com/a/a4b79e90f0f62b2e
Last edited on
Thank you for your reply JLBorges. But unfortunately, that was not quite what I was looking for. I need the program to find the word "timestamp" in a file called "log.txt." The output should have the date and time. I am trying revise the code to implement separate functions calls into this program to try and make it work more smoothly but I am having trouble.
On line 18 you have a semi-colon after your if clause, delete it :)
Thank you, Ganado. The problem is that I am not sure what to add to the code I have made to find the time stamps.
Give an example of the input file. Also, JLBorges code could easily be adjusted, you just need to replace the "file" variable with the name of your filestream variable in the for loop.
I deleted the semi-colon like you said on line 18. I also just changed the "timestamp.dat" to "timestamp.txt." But once I run the program, only thing that shows up on the text file is "Time Stamp:" but it does not show the time stamps that I need. Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inFile;
	string tempString;
	ofstream outFile;

	inFile.open ("log.txt");
	outFile.open ("timestamp.txt");
	while (inFile)
	{
	inFile >> tempString;
	if (tempString.find ('=') != string::npos)
	outFile << tempString;
	}


outFile << "Time Stamp:";
inFile.close();
outFile.close();

cin.get();
system("pause");
return 0;
}
Here is a snippet of the "log.txt" file:
-<Condition>

<Normal name="S1servo" sequence="118799290" timestamp="2014-06-10T20:34:15.909Z" dataItemId="C_27" type="LOAD"/>

</Condition>

</ComponentStream>

I just need the "timestamp="2014-06-10T20:34:15.909Z" to be the output into my timestamp.txt file. I tried to alter it different ways, but sometimes the output will end up being:


"Time Stamp:
timestamp="2014-06-10T20:34:15.909Z" dataItemId="C_27" type="LOAD"/>

</Condition>

</ComponentStream>"
I meant show the text the actual input file contains. Also, make sure your input file is even being open correctly by adding
1
2
3
4
if (!inFile)
{
    cout << "Error: Can't open in file" << endl;
}

before your while loop.

Edit: Saw your latest post
Instead of using find('='), use find("timestamp").

Also you can use break; to stop the computation early.
1
2
3
4
5
if (tempString.length() > 9 && tempString.find ("timestamp") != string::npos)
{
	outFile << tempString;
        break;
}
Last edited on
I tried the find("timestamp") you suggested earlier. But after the time it shows:
dataItemId="C_27" type="LOAD"/>

</Condition>

</ComponentStream>" in the text file.

Here is the output text file:
timestamp="2014-07-08T18:13:40.837Z"timestamp="2014-07-08T18:14:16.468Z"timestamp="2014-04-01T16:14:49.365Z"timestamp="2014-06-10T20:34:15.909Z"timestamp="2014-07-08T18:14:18.558Z"timestamp="2014-07-08T18:14:18.558Z"timestamp="2014-06-10T20:34:15.909Z"timestamp="2014-06-

I remember awhile back I learned something about using "setprecision" but that was like 2 years ago.
Last edited on
This is the input file:

-<MTConnectStreams xsi:schemaLocation="urn:mtconnect.org:MTConnectStreams:1.2 http://www.mtconnect.org/schemas/MTConnectStreams_1.2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:mtconnect.org:MTConnectStreams:1.2" xmlns:m="urn:mtconnect.org:MTConnectStreams:1.2">

<Header lastSequence="121794221" firstSequence="121269934" nextSequence="121794222" bufferSize="524288" version="1.2.0.23" instanceId="1396368889" sender="lab-PC" creationTime="2014-07-08T18:14:18Z"/>


-<Streams>


-<DeviceStream uuid="mori_nvd_1500" name="mori_nvd_1500">


-<ComponentStream name="C" componentId="C_23" component="Rotary">


-<Samples>

<SpindleSpeed name="S1speed" sequence="121792789" timestamp="2014-07-08T18:13:40.837Z" dataItemId="C_24">2500</SpindleSpeed>

<Load name="S1load" sequence="121794143" timestamp="2014-07-08T18:14:16.468Z" dataItemId="C_25">27</Load>

</Samples>


-<Events>

<RotaryMode sequence="3" timestamp="2014-04-01T16:14:49.365Z" dataItemId="C_26">SPINDLE</RotaryMode>

</Events>


-<Condition>

<Normal name="S1servo" sequence="118799290" timestamp="2014-06-10T20:34:15.909Z" dataItemId="C_27" type="LOAD"/>

</Condition>

</ComponentStream>


-<ComponentStream name="X" componentId="X_5" component="Linear">


-<Samples>

<Position name="Xact" sequence="121794218" timestamp="2014-07-08T18:14:18.558Z" dataItemId="X_6" subType="ACTUAL">-8.28686</Position>

<Load name="Xload" sequence="121794219" timestamp="2014-07-08T18:14:18.558Z" dataItemId="X_7">71</Load>

</Samples>


-<Condition>

<Normal name="Xservo" sequence="118799283" timestamp="2014-06-10T20:34:15.909Z" dataItemId="X_10" type="LOAD"/>

<Normal name="Xtravel" sequence="118799281" timestamp="2014-06-10T20:34:15.909Z" dataItemId="X_8" type="POSITION"/>

<Normal name="Xoverheat" sequence="118799282" timestamp="2014-06-10T20:34:15.909Z" dataItemId="X_9" type="TEMPERATURE"/>

</Condition>

</ComponentStream>


-<ComponentStream name="Y" componentId="Y_11" component="Linear">


-<Samples>

<Position name="Yact" sequence="121793629" timestamp="2014-07-08T18:14:02.724Z" dataItemId="Y_12" subType="ACTUAL">-16.8377</Position>

<Load name="Yload" sequence="121794220" timestamp="2014-07-08T18:14:18.558Z" dataItemId="Y_13">9</Load>

</Samples>


-<Condition>

<Normal name="Ytravel" sequence="118799284" timestamp="2014-06-10T20:34:15.909Z" dataItemId="Y_14" type="POSITION"/>

<Normal name="Yoverheat" sequence="118799285" timestamp="2014-06-10T20:34:15.909Z" dataItemId="Y_15" type="TEMPERATURE"/>

<Normal name="Yservo" sequence="118799286" timestamp="2014-06-10T20:34:15.909Z" dataItemId="Y_16" type="LOAD"/>

</Condition>

</ComponentStream>
Last edited on
Well, I'm at a loss then. If you saw my edit, I added in tempString.length() > 9 && to the if clause, see if that helps.
Thank you Ganado. I have revised it to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream inFile;
	string tempString;
	ofstream outFile;

	inFile.open ("log.txt");
	outFile.open ("timestamp.txt");

	if (!inFile)
	{
    cout << "Error: Can't open in file" << endl;
	}

	while (inFile)
	{
	inFile >> tempString;
	if (tempString.length() > 9 && tempString.find ("timestamp") != string::npos)
	{
	outFile << tempString;
        break;
	}


outFile << "Time Stamp:";
inFile.close();
outFile.close();

cin.get();
system("pause");
return 0;
}


but for some reason it still shows everything after the first "timestamp"
Bingo, it works now. Appreciate it you two for your help. :D
Topic archived. No new replies allowed.