easyx库怎么读取文件中j1,k1,H5这种数据
我想从文件中读取数据进行绘图,但是j1,k1,H5这种数据不知道要怎么读出,我写了这个一读就会乱码#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;
}
希望有人能帮我看了一下要怎么写,能在代码上直接改最好 如果j1,k1,H5等数据在文件中是以文本格式存储的话,可以使用类似于您已经写的代码来读取这些数据。但是,如果这些数据是以二进制格式存储的话,您需要使用以下函数来读取它们:
std::ifstream file("D://cwenjian//lizhu.txt", std::ios::binary);
float j1, k1, H5;
file.read(reinterpret_cast<char*>(&j1), sizeof(j1));
file.read(reinterpret_cast<char*>(&k1), sizeof(k1));
file.read(reinterpret_cast<char*>(&H5), sizeof(H5));
在这种情况下,您需要将文件标记为二进制模式(即使用`ios::binary`标志)。另外,您可以使用`read`函数来读取一个字节序列,而使用`reinterpret_cast`将其转换为`float`类型。
希望这可以帮助到您。 本帖最后由 大苏打dd 于 2023-4-13 20:59 编辑
歌者文明清理员 发表于 2023-4-13 20:49
如果j1,k1,H5等数据在文件中是以文本格式存储的话,可以使用类似于您已经写的代码来读取这些数据。但是, ...
文件中是以文本形式存储的 用的float存储数据,但是我这个代码读出来的数据是异常值
j1: -1.07374e+008
k1 -1.07374e+008
会是这样,本来读j k的时候代码写成这样就可以读出来
std::stringstream s(line);
s>> eq1 >> eq2 >> j >> comma >> eq1 >> eq2 >> k;
但是改成j1 k1 H5这种类型时就不行了,是因为std::stringstream ss(line);这里面的ss不能对应j1 k1 H5这种字符吗 大苏打dd 发表于 2023-4-13 20:56
文件中是以文本形式存储的 用的float存储数据,但是我这个代码读出来的数据是异常值
j1: -1.07374e+008
...
那我就不知道了{:10_277:} 歌者文明清理员 发表于 2023-4-13 20:57
那我就不知道了
s的时候可以读j k,ss的时候j1 k1就不行了{:5_99:} 根据你提供的代码,我认为你可能需要修改文件读取部分,以正确读取j1、k1和H5这样的数据。请尝试以下修改后的代码:
#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 = 0, k1 = 0, H5 = 0;
char ch;
std::string line;
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 >> ch >> H5;
}
}
}
// 画矩形
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;
}
这段代码修改了文件读取部分,以便可以正确地解析j1、k1和H5这样的数据。请注意,我假设你的输入文件中的数据格式类似于:
j1 50, k1 100, H5 150
如果你的文件格式不同,请相应地修改解析逻辑。
isdkz 发表于 2023-4-13 21:05
根据你提供的代码,我认为你可能需要修改文件读取部分,以正确读取j1、k1和H5这样的数据。请尝试以下修改后 ...
有两个问题,一个是文件的格式是j1=50,k1=100,H5=150这种格式的,我没想出来要怎么改,第二是我发现您采用的是ch=H来进行判断是否是H5,但是文件中还会存在H4,H3这种类型的数据,这样会不会导致出现问题 大苏打dd 发表于 2023-4-13 21:12
有两个问题,一个是文件的格式是j1=50,k1=100,H5=150这种格式的,我没想出来要怎么改,第二是我发现您采 ...
谢谢您的反馈。针对您提到的两个问题,我做了一些修改。这是更新后的代码:
#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 = 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;
}
这段代码针对您提到的两个问题进行了修改:
[*]考虑到文件格式是j1=50,k1=100,H5=150,我将代码中的ss >> ch >> j1;等类似语句更改为ss >> ch >> j1;,以便正确地跳过等号(=)。
[*]为了避免H4、H3等数据与H5混淆,我添加了一个变量int num;。现在,代码首先读取'H'字符,然后读取一个整数num。如果num等于5,那么就读取H5的值。对于其他的H值(如H4、H3等),您可以在else语句中进行处理。在这里,我选择使用ss.ignore()来忽略当前行中直到逗号(,)为止的其他字符。
如果您有其他问题,请随时告诉我。
isdkz 发表于 2023-4-13 21:18
谢谢您的反馈。针对您提到的两个问题,我做了一些修改。这是更新后的代码:
代码有错误,不能正常运行
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : error C2039: 'numeric_limits' : is not a member of 'std'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : error C2065: 'numeric_limits' : undeclared identifier
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : error C2275: 'std::streamsize' : illegal use of this type as an expression
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : warning C4003: not enough actual parameters for macro 'max'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : error C2589: '(' : illegal token on right side of '::'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : error C2059: syntax error : '::' 这是出现的错误 我看应该都是在您发的代码的第35行出现的问题,希望您能帮我看一下 大苏打dd 发表于 2023-4-13 21:25
代码有错误,不能正常运行
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(35) : er ...
抱歉给您带来了困扰。根据您的错误信息,问题是由于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;
}
isdkz 发表于 2023-4-13 21:28
抱歉给您带来了困扰。根据您的错误信息,问题是由于std::numeric_limits没有正确包含。请在代码顶部添加# ...
添加之后运行了一下还是有问题,
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(36) : warning C4003: not enough actual parameters for macro 'max'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(36) : error C2589: '(' : illegal token on right side of '::'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(36) : error C2143: syntax error : missing ')' before '::'
D:\vc6.0\Microsoft Visual Studio\MyProjects\读文件2\hanshu.cpp(36) : error C2059: syntax error : ')'
执行 cl.exe 时出错.
还是在35行出的问题 本帖最后由 大苏打dd 于 2023-4-13 22:06 编辑
isdkz 发表于 2023-4-13 21:28
抱歉给您带来了困扰。根据您的错误信息,问题是由于std::numeric_limits没有正确包含。请在代码顶部添加# ...
您好,我又想到一些问题,就是数据中还存在No,hh这种类型,像这种能否直接在代码中把if(num==5)改为if(ch=='o')这样,还有一个问题是我发现
else if (ch == 'B') {
ss >> num;
if (num == 1) {
ss >> ch >> B1;
这一段代码好像没有生效,我看打印出的数据B1=0 但是B1实际上等于40
#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, k1, B1,B4;
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 == 'B') {
ss >> num;
if (num == 1) {
ss >> ch >> B1;
} else if(num==4){
// 处理其他的H值,例如H4、H3等
ss>>ch>>B4;
}
}
}
}
// 画矩形
rectangle(10, 10, 10 + j1, 10 + k1);
rectangle(10+B1, 10, 10 + j1+B1, 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;
}
这是现在完整的代码 往文件里放数据肯定是有一定顺序的,比如 P1,P2,P3,H1,H2,H3
那么在读取的时候肯定也是要按这个顺序读取出来,所以不要管你的程序里有没有H897, M456,只要按顺序读出来,该是谁的就是谁的
保存数据的时候如果顺序不固定,那将来要读的时候就要做很多分析,数据多了以后分析数据这部分的代码量是要有多大??????????
页:
[1]