|  | 
 
| 
我想从文件中读取数据进行绘图,但是j1,k1,H5这种数据不知道要怎么读出,我写了这个一读就会乱码
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 复制代码#include <graphics.h>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <conio.h>
int main()
{
    // 初始化图形模式
    initgraph(640, 480, SHOWCONSOLE);
    // 打开文件
    std::ifstream file("D://cwenjian//lizhu.txt");
    // 读取文件中的数据
    float j1,k1;
    char eq1, eq2, comma;
    std::string line;
    std::getline(file, line);
    std::stringstream ss(line);
    ss >> eq1 >> eq2 >> j1 >> comma >> eq1 >> eq2 >> k1  ;
    // 画矩形
    rectangle(10, 10, 10 + j1, 10 + k1);
    // 关闭文件
    file.close();
    std::cout << "j1: " << j1 << std::endl;
    std::cout << "k1 " << k1<< std::endl;
   // std::cout << "B1" << B1<< std::endl;
    // 等待用户按键
    _getch();
    // 关闭图形模式
    closegraph();
    return 0;
}
希望有人能帮我看了一下要怎么写,能在代码上直接改最好
 
抱歉给您带来了困扰。根据您的错误信息,问题是由于std::numeric_limits没有正确包含。请在代码顶部添加#include <limits>,以包含std::numeric_limits的定义。
 这是修改后的代码:
 
 复制代码#include <graphics.h>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <conio.h>
#include <limits> // 添加这一行
int main()
{
    // 初始化图形模式
    initgraph(640, 480, SHOWCONSOLE);
    // 打开文件
    std::ifstream file("D://cwenjian//lizhu.txt");
    // 读取文件中的数据
    float j1 = 0, k1 = 0, H5 = 0;
    char ch;
    std::string line;
    int num;
    while (std::getline(file, line)) {
        std::stringstream ss(line);
        while (ss >> ch) {
            if (ch == 'j') {
                ss >> ch >> j1;
            } else if (ch == 'k') {
                ss >> ch >> k1;
            } else if (ch == 'H') {
                ss >> num;
                if (num == 5) {
                    ss >> ch >> H5;
                } else {
                    // 处理其他的H值,例如H4、H3等
                    ss.ignore(std::numeric_limits<std::streamsize>::max(), ',');
                }
            }
        }
    }
    // 画矩形
    rectangle(10, 10, 10 + j1, 10 + k1);
    // 关闭文件
    file.close();
    std::cout << "j1: " << j1 << std::endl;
    std::cout << "k1: " << k1 << std::endl;
    std::cout << "H5: " << H5 << std::endl;
    // 等待用户按键
    _getch();
    // 关闭图形模式
    closegraph();
    return 0;
}
 | 
 |