.二进制文件读取:用记事本查看c:\score.dat,自己设计程序查看生成的成绩单
时间: 2024-02-29 16:56:45 浏览: 24
二进制文件是以二进制形式存储数据的,不能用记事本查看。我们可以写程序读取二进制文件,然后输出成绩单。
以下是代码实现:
```c
#include <stdio.h>
// 成绩结构体
typedef struct {
int id;
int chinese;
int math;
} Score;
int main() {
// 创建文件指针
FILE *fp;
fp = fopen("c:\\score.dat", "rb");
if (fp == NULL) {
printf("File open error!\n");
return 1;
}
// 读取文件中的成绩并输出
Score score;
printf("id\tchinese\tmath\n");
while (fread(&score, sizeof(Score), 1, fp) == 1) {
printf("%d\t%d\t%d\n", score.id, score.chinese, score.math);
}
// 关闭文件
fclose(fp);
return 0;
}
```
以上代码会读取C盘根目录下的二进制文件score.dat,并输出成绩单。输出的格式是成绩单号、语文分数、数学分数,用制表符分隔开。
相关问题
二进制文件写入:建立一个成绩结构体,包括:成绩单号,语文分数、数学分数;随机生成10人成绩写入文件c:\score.dat
好的,下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 成绩结构体
typedef struct {
int id;
int chinese;
int math;
} Score;
int main() {
// 随机数种子
srand((unsigned)time(NULL));
// 创建文件指针
FILE *fp;
fp = fopen("c:\\score.dat", "wb");
if (fp == NULL) {
printf("File open error!\n");
exit(1);
}
// 随机生成10个成绩并写入文件
for (int i = 1; i <= 10; i++) {
Score score;
score.id = i;
score.chinese = rand() % 101;
score.math = rand() % 101;
fwrite(&score, sizeof(Score), 1, fp);
}
// 关闭文件
fclose(fp);
return 0;
}
```
以上代码会在C盘根目录下创建一个二进制文件score.dat,并将10个随机生成的成绩写入其中。每个成绩包括成绩单号、语文分数和数学分数,都是随机生成的。
selenium.common.exceptions.WebDriver异常:消息:未知错误:找不到Chrome二进制文件
这个错误通常发生在使用Selenium时,Selenium无法找到Chrome浏览器的二进制文件。要解决这个问题,您可以尝试以下步骤:
1. 确认您已经安装了Chrome浏览器,并且它已经在您的系统中正确安装和配置。
2. 确认您正在使用的Selenium版本与您的Chrome浏览器版本兼容。可以在Selenium官网上查看版本兼容性。
3. 确认您的Chrome浏览器的二进制文件路径是否正确。可以使用以下代码获取Chrome浏览器的二进制文件路径:
```python
from selenium import webdriver
chrome_path = webdriver.Chrome(executable_path='<path_to_chromedriver>')
```
在这里,`<path_to_chromedriver>`应该是Chrome浏览器的二进制文件路径。
4. 如果仍然无法找到Chrome浏览器的二进制文件,则可以尝试指定Chrome浏览器的二进制文件路径。可以使用以下代码指定Chrome浏览器的二进制文件路径:
```python
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.binary_location = '<chrome_binary_path>'
chrome_path = webdriver.Chrome(executable_path='<path_to_chromedriver>', chrome_options=chrome_options)
```
在这里,`<chrome_binary_path>`应该是Chrome浏览器的二进制文件路径。