You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++按分号分割字符串,存入char数组与double变量的实现方法

C++按分号分割字符串提取内容的实现

需求描述

我有一个格式如下的文件:

firstword;secondword;4.0

需要按;分割每行内容,将前两个单词存入char数组,将数字存入double类型变量。

在Python中只需用split(";")方法,再处理列表前两个元素,最后一个用float()转换即可,但不清楚C++的实现语法。

目前已能读取文件并将每行存入studentList字符串数组,但不知道如何从数组元素中提取单词和数字,知道需要声明新变量存储,但还未实现。不想使用vector。

现有代码如下:

#include <iomanip>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;
   
int main() {
    string studentList[4];

    ifstream file;
    file.open("input.txt");
    if(file.is_open()) {
        for (int i = 0; i < 4; i++) {
            file >> studentList[i];
        }
        file.close();
    }

    for(int i = 0; i < 4; i++) {        
        cout << studentList[i];
    }

    return 0;
}

解决方案

1. 修正文件读取逻辑

原代码中file >> studentList[i]会以空白字符作为分隔符读取内容,无法保证完整读取每行,应该用getline读取整行内容:

if(file.is_open()) {
    for (int i = 0; i < 4; i++) {
        getline(file, studentList[i]);
    }
    file.close();
}

2. 分割字符串并提取内容

利用C++ string类的find和substr方法分割;,再将提取的内容转换为目标类型:

  • 用find(';')定位分号位置,通过substr截取单词片段;
  • 用stod()(或atof())将数字字符串转为double;
  • 通过strcpy将string内容复制到char数组中。

完整修改代码

#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
   
int main() {
    string studentList[4];
    // 定义存储结果的char数组,需保证长度足够容纳单词
    char firstWord[50], secondWord[50];
    double score;

    ifstream file;
    file.open("input.txt");
    if(!file.is_open()) {
        cout << "无法打开文件" << endl;
        return 1;
    }

    // 读取整行到字符串数组
    for (int i = 0; i < 4; i++) {
        getline(file, studentList[i]);
    }
    file.close();

    // 逐行处理内容
    for(int i = 0; i < 4; i++) {        
        string line = studentList[i];
        // 定位第一个分号
        size_t pos1 = line.find(';');
        if (pos1 == string::npos) continue;
        // 提取第一个单词并复制到char数组
        string firstStr = line.substr(0, pos1);
        strcpy(firstWord, firstStr.c_str());

        // 定位第二个分号
        size_t pos2 = line.find(';', pos1 + 1);
        if (pos2 == string::npos) continue;
        // 提取第二个单词并复制到char数组
        string secondStr = line.substr(pos1 + 1, pos2 - pos1 - 1);
        strcpy(secondWord, secondStr.c_str());

        // 提取数字字符串并转为double
        string scoreStr = line.substr(pos2 + 1);
        score = stod(scoreStr);

        // 输出验证结果
        cout << "第" << i+1 << "行:" << endl;
        cout << "第一个单词:" << firstWord << endl;
        cout << "第二个单词:" << secondWord << endl;
        cout << "分数:" << fixed << setprecision(1) << score << endl;
        cout << "------------------------" << endl;
    }

    return 0;
}

关键说明

  • find(';', pos1 + 1):从第一个分号的下一位开始查找第二个分号,避免重复匹配;
  • substr(pos, length):若只传一个参数,会从该位置截取到字符串末尾;
  • stod():C++11及以上标准支持的字符串转double函数,兼容旧标准可改用atof(scoreStr.c_str());
  • char数组需提前定义足够长度,防止字符串溢出。

内容的提问来源于stack exchange,提问作者Alisha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 06:31:15