easyx从txt文件读取数据
时间: 2023-09-23 14:12:28 浏览: 154
可以通过以下代码,使用EasyX从txt文件中读取数据:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> data;
string line;
ifstream file("file.txt");
if (file.is_open())
{
while (getline(file, line))
{
data.push_back(line);
}
file.close();
}
else
{
cout << "Unable to open file" << endl;
return 0;
}
// 使用读取到的数据进行操作
return 0;
}
```
记得把file.txt替换为你要读取的txt文件的路径。
相关问题
用c语言和easyx写一个从文件读取数据,用表格输出的程序
好的,以下是一个用 C 语言和 EasyX 写的从文件读取数据并用表格输出的程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define ROWS 10
#define COLS 3
#define CELL_WIDTH 100
#define CELL_HEIGHT 30
int main()
{
FILE *fp;
char filename[100];
int data[ROWS][COLS];
int i, j;
// 打开文件
printf("请输入文件名:");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", filename);
return 1;
}
// 读取数据
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
fscanf(fp, "%d", &data[i][j]);
}
}
// 关闭文件
fclose(fp);
// 初始化图形界面
initgraph(CELL_WIDTH * COLS, CELL_HEIGHT * ROWS);
// 绘制表格
setbkcolor(WHITE);
cleardevice();
setlinestyle(PS_SOLID, 1);
settextstyle(DEFAULT_FONT, 0, 15);
for (i = 0; i <= ROWS; i++) {
line(0, i * CELL_HEIGHT, COLS * CELL_WIDTH, i * CELL_HEIGHT);
}
for (j = 0; j <= COLS; j++) {
line(j * CELL_WIDTH, 0, j * CELL_WIDTH, ROWS * CELL_HEIGHT);
}
// 输出数据
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
char str[100];
sprintf(str, "%d", data[i][j]);
outtextxy(j * CELL_WIDTH + 5, i * CELL_HEIGHT + 5, str);
}
}
// 等待用户关闭窗口
getch();
// 关闭图形界面
closegraph();
return 0;
}
```
这个程序会提示用户输入文件名,然后从文件中读取数据,最后在图形界面中用表格输出数据。每个单元格的宽度为 100 像素,高度为 30 像素。程序中使用了 EasyX 图形库来绘制图形界面。
easyx读取坐标点文件绘制图形
可以通过以下步骤使用EasyX读取坐标点文件并绘制图形:
1. 打开文件并读取坐标点数据,例如txt文件中每行格式为"x y",可以使用ifstream和getline函数来读取数据。
```c++
ifstream f("data.txt");
string line;
while (getline(f, line))
{
istringstream iss(line);
int x, y;
iss >> x >> y;
// 存储坐标数据
}
f.close();
```
2. 创建绘图窗口并设置绘图参数,例如背景色、坐标轴等。
```c++
initgraph(width, height);
setbkcolor(WHITE);
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 1);
settextstyle(16, 0, _T("宋体"));
```
3. 绘制坐标轴及其他辅助线,例如x轴和y轴。
```c++
line(0, height / 2, width, height / 2); // x轴
line(width / 2, 0, width / 2, height); // y轴
```
4. 遍历坐标点数据并将其转换为屏幕坐标,然后使用line函数或者circle函数绘制图形。
```c++
for (int i = 0; i < points.size(); i++)
{
int x = points[i].x + width / 2;
int y = height / 2 - points[i].y;
circle(x, y, 2);
}
```
5. 关闭绘图窗口。
```c++
closegraph();
```
完整代码如下:
```c++
#include <graphics.h>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
struct Point
{
int x;
int y;
};
int main()
{
int width = 600;
int height = 600;
vector<Point> points;
ifstream f("data.txt");
string line;
while (getline(f, line))
{
istringstream iss(line);
int x, y;
iss >> x >> y;
points.push_back({ x, y });
}
f.close();
initgraph(width, height);
setbkcolor(WHITE);
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 1);
settextstyle(16, 0, _T("宋体"));
line(0, height / 2, width, height / 2);
line(width / 2, 0, width / 2, height);
for (int i = 0; i < points.size(); i++)
{
int x = points[i].x + width / 2;
int y = height / 2 - points[i].y;
circle(x, y, 2);
}
closegraph();
return 0;
}
```