简体   繁体   中英

C++ Splitting strings from ifstream and placing them in seperate arrays

I'm writing a program in C++ to take input from a text file (dates and the high/low temp of that day), split the dates and temps into two separate arrays. I have the process down; however, I can't seem to split the strings appropriately. I've tried different methods with getline() and .get, but I need to keep the strings as STRINGS, not an array of chars. I've looked into and read the answers for similar questions using vectors and strtock and there's only one problem: I'm still fairly new, and the more I look into them, the more confused I get.

If I am to use that method in solving my problem, I just need to be pointed in the right direction of how to apply it. Apologies for my noobishness, it's just easy to get overwhelmed with all of the different ways to solve one problem with C++ (which is the reason I enjoy using it so much. ;))!

Sample from text:

  • 10/12/2007 56 87
  • 10/13/2007 66 77
  • 10/14/2007 65 69

etc.

The dates need to be stored in one array, and the temps (both high and low) in another.

Here's what I have (unfinished, but for reference nonetheless)

int main()

//Open file to be read
ifstream textTemperatures;
textTemperatures.open("temps1.txt");
//Initialize arrays.
const int DAYS_ARRAY_SIZE = 32,
          TEMPS_ARRAY_SIZE = 65;
string daysArray[DAYS_ARRAY_SIZE];
int tempsArray[TEMPS_ARRAY_SIZE];
int count = 0;

while(count < DAYS_ARRAY_SIZE && !textTemperatures.eof())
{   
    getline(textTemperatures, daysArray[count]);
    cout << daysArray[count] << endl;
    count++;
}   

Thanks everyone.

Try the following

#include <iostream>
#include <fstream>
#include <sstream>

//... 

std::ifstream textTemperatures( "temps1.txt" );

const int DAYS_ARRAY_SIZE = 32;


std::string daysArray[DAYS_ARRAY_SIZE] = {};
int tempsArray[2 * DAYS_ARRAY_SIZE] = {};

int count = 0;

std::string line;
while ( count < DAYS_ARRAY_SIZE && std::getline( textTemperatures, line ) )
{
   std::istringstream is( line );
   is >> daysArray[count];
   is >> tempsArray[2 * count];
   is >> tempsArray[2 * count + 1];
}   

Here is a simple program that read the formatted input. You can easily replace std::cin with your std::ifstream and do whatever you want with the data inside the loop.

#include <iostream>
#include <string>
#include <vector>

int main ()
{
    std::vector<std::string> dates;
    std::vector<int> temperatures;
    std::string date;
    int low, high;

    while ((std::cin >> date >> low >> high))
    {
        dates.push_back(date);
        temperatures.push_back(low);
        temperatures.push_back(high);
    }
}

The magic here is done by std::cin 's operator>> which reads up to the first whitespace encountered (tabulation, space or newline) and stores the value inside the right operand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM