1

I am currently trying to read information from an .txt file and essentially store this appropriately. Data from the input file would look something like this

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
765DEF 01:01:05:59 enter 17
ABC123 01:01:06:01 enter 17
765DEF 01:01:07:00 exit 95
ABC123 01:01:08:03 exit 95

My question is that, assuming I have read "01:01:05:59" into a string, how do I parse this to store the numbers in an int variable. In addition, all I really need is the third pair of numbers in that string(from the left) and I was also wondering how to skip the first two and last pair of numbers in that string. I have read on delimiters but I'm a little confused on how to use them. The code I have so far is shown below and is basically that information to strings.

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



int main()
{
    int arr[25];


    ifstream File;
    File.open("input.txt");

    for (int a = 0; a < 25; a++)
    {
        File >> arr[a];

    }
    for (int i = 0; i < 25; i++)
    { 
        cout << arr[i] << " ";
    }

    cout << endl;

    string license, datetime;
    File >> license >> datetime; // reads in license plate and datetime information into two separte strings
    cout << license << endl << datetime;


    system("pause");
} 
erol yeniaras
  • 3,701
  • 2
  • 22
  • 40
KoolaidLips
  • 247
  • 1
  • 8
  • 20
  • 2
    Please use either Stack Overflow's search feature or Google. there are answers all over the place on how to read integers and convert strings to integers. – Captain Obvlious Apr 14 '15 at 01:14
  • possible duplicate of [How to parse a string to an int in C++?](http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) – Simon Apr 14 '15 at 01:19
  • 1
    He asks a specific case of parsing some part of string to int not the whole string. – erol yeniaras Apr 14 '15 at 01:22
  • Yes as erol said, I want only part of that string and not the whole thing. Also that example contains a string of purely numbers from what I can see and mine includes colons to account for which is why I was thinking of using delimiters. – KoolaidLips Apr 14 '15 at 01:23

3 Answers3

3

Background: If we know the start and end indices (or the length) of the sub-string we need, then we can read it by using std::string::substr.

Its usage is as follows:

#include <string>

...

std::string foo = "0123456789stack:overflow";

// start index = 4, length = 2
std::string subStr1 = foo.substr(4,2); // result = "45"

// start index = 3, end index = 5 => length = 5 - 3 + 1 = 3
std::string subStr2 = foo.substr(3,3); // result = "345"
// The first parameter is the start index whereas the second one is 
// the length of the wanted sub-string. 

// If only the start index is known:
std::string subStr2 = foo.substr(9); //  result = "9stack:overflow"
// In that case we get the rest of the string starting from the start index 9.

For more information on that please refer to: http://www.cplusplus.com/reference/string/string/substr/

Suggested solution to the OP: Since you said "all I really need is the third pair of numbers" then you need two characters starting from index 6:

std::string a  = "01:01:05:59";

std::string sub = a.substr(6, 2); // will give you "05"

then convert them using:

int number = std::stoi(sub);

These steps can be shortened to:

int number = std::stoi( a.substr(6, 2) );

Further references:

First part: http://en.cppreference.com/w/cpp/string/basic_string/substr

Second part: How to parse a string to an int in C++?

PS: if you want to use character array instead of std::string then you just can get the characters with their corresponding indices. For example: i = 6 and i = 7 in your specific case. Then, get yourArray[6]=0 and yourArray[7]=5. Then perform integer conversion on them.

Community
  • 1
  • 1
erol yeniaras
  • 3,701
  • 2
  • 22
  • 40
1

Could you do: int num = std::stoi(string.substr(6, 2);

NYB
  • 93
  • 1
  • 7
1

assuming I have read "01:01:05:59" into a string

One easy way is using streams:

#include <iostream>
#include <sstream>

int main()
{
    int n[4];
    std::istringstream iss("02:30:41:28");
    if (iss >> n[0] && iss.get() == ':' &&
        iss >> n[1] && iss.get() == ':' &&
        iss >> n[2] && iss.get() == ':' &&
        iss >> n[3] >> std::ws && iss.eof())
        std::cout << n[0] << ' ' << n[1] << ' ' << n[2] << ' ' << n[3] << '\n';
    else
        std::cerr << "parsing error\n";
}

On ideone.com

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252