```
int main()
{
int d=0;
FILE* p = fopen("stu.txt", "r");
if (p == NULL)
{
fprintf(stderr, "【文件打开失败】\n");
exit(EXIT_FAILURE);
}
fseek(p,95L,SEEK_SET);
while(fgetc(p)!=EOF)
{
fscanf(p, " %d%s%s%s%s%s%s%s%s%s", &s[d].num, s[d].name, s[d].col, s[d].cla, s[d].room, s[d].home, s[d].id, s[d].stutel, s[d].par, s[d].partel); //把数据存入结构体
d++;
}
studentnum=d;
fclose(p);
re();
return 0;
}```
原文本:
学号 姓名 学院 班级 宿舍 户籍 身份证号 学生电话 监护人 监护人电话
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
读取之后的:
学号 姓名 学院 班级 宿舍 户籍 身份证号 学生电话 监护人 监护人电话
1111111112
2222222223
3333333334
4444444445
555555555
目测你的 %d 前面多了一个空格
将fscanf()改为:fscanf(p, "%d%s%s%s%s%s%s%s%s%s\n", &s[d].num, s[d].name, s[d].col, s[d].cla, s[d].room, s[d].home, s[d].id, s[d].stutel, s[d].par, s[d].partel); 试试,格式符里添加换行符'\n'。
可能文件使用的编码方式有关,不同编码方式中文所占的字节数不同,我在Windwos系统下文件编码使用ANSI,将fseek(p,95L,SEEK_SET);里的95改为65或66好像就可以了;
如果使用UTF-8方式编码,可以将95改为94再试试。
测试代码如下:
参考链接:
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[20];
char col[50];
char cla[50];
char room[50];
char home[50];
char id[50];
char stutel[20];
char par[50];
char partel[50];
} s[50]; // https://blog.csdn.net/qq_45696288/article/details/105077016
int studentnum=0;
void re() {
printf("学号\t姓名\t学院\t班级\t宿舍\t户籍\t身份证号\t学生电话\t监护人\t监护人电话\n");
for(int i=0; i<studentnum; i++) {
printf( "%4d\t%4s\t%4s\t%4s\t%4s\t%4s\t%8s\t%8s\t%6s\t%10s\n", s[i].num, s[i].name, s[i].col, s[i].cla, s[i].room, s[i].home, s[i].id, s[i].stutel, s[i].par, s[i].partel);
}
}
int main()
{
//
int d=0;
FILE* p = fopen("stu.txt", "r");
if (p == NULL) {
fprintf(stderr, "【文件打开失败】\n");
exit(EXIT_FAILURE);
}
// https://zhuanlan.zhihu.com/p/420220710
// https://www.runoob.com/cprogramming/c-function-fseek.html
// https://blog.csdn.net/m0_46573836/article/details/113841856
// http://c.biancheng.net/view/395.html
// 文件编码使用ANSI, 这里改为65或66都可以,不知道为啥
// 如果文件编码使用UTF-8,可以试试将95改为94试试。
fseek(p,66L,SEEK_SET);
char ch;
//printf("读取的数据为:\n");
while((ch=fgetc(p))!=EOF) {
//printf("ch=%c,%d\n",ch,ch);
fscanf(p, "%d%s%s%s%s%s%s%s%s%s", &s[d].num, s[d].name, s[d].col, s[d].cla, s[d].room, s[d].home, s[d].id, s[d].stutel, s[d].par, s[d].partel); //把数据存入结构体
// printf("%d %s %s %s %s %s %s %s %s %s\n", s[d].num, s[d].name, s[d].col, s[d].cla, s[d].room, s[d].home, s[d].id, s[d].stutel, s[d].par, s[d].partel); //把数据存入结构体
d++;
}
studentnum=d;
fclose(p);
re();
return 0;
}
stu.txt(测试数据文件):
学号 姓名 学院 班级 宿舍 户籍 身份证号 学生电话 监护人 监护人电话
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
对于问题标题中的C语言读取文件后缺少一个数据的问题,可能是因为读取文件时没有正确处理换行符导致的。在读取文本文件时,每读取一行数据后需要处理换行符,否则下一次读取时可能会出现数据丢失的情况。
下面是一个具体的解决方案:
#include<stdio.h>
#include<stdlib.h>
#define MAX_LEN 100
int main() {
FILE *file;
char line[MAX_LEN];
// 打开文件
file = fopen("data.txt", "r");
if (file == NULL) {
printf("无法打开文件!\n");
return -1;
}
// 读取文件内容
while (fgets(line, MAX_LEN, file)) {
printf("%s", line);
}
// 关闭文件
fclose(file);
return 0;
}
这个程序可以读取文件的每一行数据并打印出来。你可以尝试使用这个程序来读取你的文件,看看是否能够正确地输出每一行的数据。
如果你的文件中有换行符问题,可以在读取每一行数据后使用strtok
函数来处理换行符。具体的代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LEN 100
int main() {
FILE *file;
char line[MAX_LEN];
// 打开文件
file = fopen("data.txt", "r");
if (file == NULL) {
printf("无法打开文件!\n");
return -1;
}
// 读取文件内容
while (fgets(line, MAX_LEN, file)) {
line[strcspn(line, "\n")] = '\0'; // 去除换行符
printf("%s\n", line);
}
// 关闭文件
fclose(file);
return 0;
}
这个程序会把每一行数据中的换行符去除掉,并正确地输出每一行的数据。
希望这个解决方案可以帮助到你!如果还有其他问题,请随时提问。