简体   繁体   English

C ++从ifstream拆分字符串并将它们放在单独的数组中

[英]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. 我正在用C ++编写程序,以从文本文件(日期和当天的高/低温度)中获取输入,将日期和温度分成两个单独的数组。 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. 我尝试使用getline()和.get使用其他方法,但是我需要将字符串保留为STRINGS,而不是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. 我已经使用vector和strtock调查并阅读了类似问题的答案,只有一个问题:我还是很新,而且研究的越多,我就越困惑。

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. ;))! 抱歉,我很容易被所有解决C ++问题的不同方法所淹没(这就是我非常喜欢使用它的原因。))!

Sample from text: 文字样本:

  • 10/12/2007 56 87 2007年10月12日56 87
  • 10/13/2007 66 77 2007年10月13日66 77
  • 10/14/2007 65 69 2007年10月14日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. 您可以轻松地将std :: cin替换为std :: ifstream,并使用循环内的数据执行任何所需的操作。

#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. 这是由std::cinoperator>> ,该operator>>可以读取遇到的第一个空格(制表符,空格或换行符)并将值存储在正确的操作数内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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