Extracting hour,min,sec from imported timestamp

Hey everyone, I have a problem. My program needs to import an array of timestamps in the form 0:00:00 from a data file and extract the hour, the minute, and second of each. Because there are ":", I imported the timestamps as strings (variable declaration is: std::string timestamp[100]). Each individual timestamp prints to the screen as strings. Now to extract each item, my thought process was to use strcpy to copy the contents of each timestamp to a character array. However, after having no luck with that, I used "timestamp[0].length()" to find the length of the string and it returned 0. Why is it doing that? If it would return "7" then the strcpy would work and i would be able to extract it. Any ideas of what is going on would be greatly appreciated.

Note: In my code below, I just tested the first string element of timestamp[100] to see if it can be copied to an accessible character array.

Here's the data file:

0:00:01
0:00:02
0:00:03
0:00:04
0:00:05
0:00:06
0:00:07
0:00:08
0:00:09
0:00:10


Here's my code:

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <string.h>
#include <cstring>
#include <conio.h>
#include <fstream>
using namespace std;


int main()
{
        
        //Variable Declarations
	string timestamp[100];
	char time[10];
	int i,status=1;


	FILE *infile;
	infile = fopen("timestamp.dat","r");


	i=0;
	printf("\n Timestamp\n");

	//Loads timestamp from data file
	while (status!=EOF)
	{
		status = fscanf(infile,"%s",&timestamp[i]);
		if (status == EOF)
			break;
		printf("\n%s",&timestamp[i]);
	}



	//Test to find length of timestamp string array
	printf("\n\n\n");
	cout<<timestamp[0].length();
	printf("\n\n\n");


	//Test to see if the timestamp string can be copied to a char string
	strcpy(time,timestamp[0].c_str());
	cout<<time;



	fclose(infile);
	printf("\n\n\n");
	system("pause");

}




Topic archived. No new replies allowed.