我刚刚开始学习 C 语言,在结构和输入/输出流理解方面存在问题。我正在尝试编写一个代码,该代码将读取创建的文本文件中的现有信息,将其输入到由并集 union 的结构中,然后将其复制到通过代码文本文件新创建的结构中。我还尝试使这些信息出现在输出窗口中。 下面是我尝试制作的代码示例,但它给了我一个奇怪的输出,我不确定我应该在这里做什么。从文本文件中我试图读取学生 ID、姓名、学校的信息身份,获得奖学金的次数,电话,年龄,cgpa,是否现在出国留学。我的文件如下所示:
姓名/身份证/年龄/奖学金/学校统计/国外/地址/电话/cgpa
标记/20091111/20/1/是/否/市、区、街道、房屋/1234567890/3.0
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct{
char name[20];
char mail[30];
int mobile;
}PROFESSOR;
typedef struct{
char name[20];
char address[50];
char schoolstat[10];
int scholarship[5];
char abroad[10];
int phone;
int age;
int ID;
float cgpa;
}STUDENT;
typedef struct{
char type;
union {
PROFESSOR prof;
STUDENT stu;
}u;
}PERSON;
// Driver program
int main () {
FILE *infile, *ofp;
PERSON data[6];
int i;
char name[20];
char address[50];
char schoolstat[10];
int scholarship[5];
char abroad[10];
int phone;
int age;
int ID;
float cgpa;
infile = fopen ("inputfile.txt", "r");
if (infile == NULL)
{
fprintf(stderr, "\nError opening file\n");
exit (1);
}
ofp=fopen("outputfile.txt", "w");
if(ofp==NULL){
printf("output file open error!\n");
return 1;
}
data[0].type = 'p';
strcpy(data[0].u.prof.name, "Sam");
strcpy(data[0].u.prof.mail, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3645454545765b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>");
data[0].u.prof.mobile = 1234567;
data[1].type = 's';
data[2].type = 's';
data[3].type = 's';
data[4].type = 's';
data[5].type = 's';
int total = 0;
int total_age=0;
int newline=0;
int res;
char c;
int i;
while(1){
res=fscanf(infile, "%d %d %d %d %s %s %s %d %f\n", name, &ID, &age, &scholarship, &schoolstat, &abroad, &address, &phone, &cgpa);
if(res==EOF) break;
fprintf(ofp, "%d %s %d %d %s %s %s %d %f\n", ID, name, age, scholarship, schoolstat, abroad, address, phone, cgpa);
i++;
if(i==5) break;
total = total + cgpa;
total_age = total_age + age;
for(newline=0; newline<res; newline++){
if(name[c]=='\0'){
newline++;
}
}
}
for(i=0; i<6; i++){
switch(data[i].type){
case 'p':
printf("professor %s\n", data[i].u.prof.name);
printf("%s, %d\n", data[i].u.prof.mail, data[i].u.prof.mobile);
break;
case 's':
printf("student %s %d %d %d %s %s %s %d %f\n", data[i].u.stu.name, data[i].u.stu.age, data[i].u.stu.ID, data[i].u.stu.scholarship, data[i].u.stu.schoolstat, data[i].u.stu.abroad,
data[i].u.stu.address, data[i].u.stu.phone, data[i].u.stu.cgpa);
break;
}
printf("\n");
}
printf("Number of lines: %d", newline);
// close file
fclose (infile);
fclose(ofp);
return 0;
}
感谢任何帮助!
现在输出如下: 山姆教授 [email protected],1234567
学生 0 0 6486936 0 0.000000
学生 0 0 6487068 0 0.000000
学生 0 0 6487200 0 0.000000
学生 0 0 6487332 0 0.000000
行数:0
最佳答案
格式使用'/'
作为分隔符。使用 "%width[^/]"
读取字符串标记。
使用“%n”
记录扫描的偏移量(如果扫描到了那么远)。
成功fopen()
读取行
#define LINE_MAX 135 // Longest expected line
char line[LINE_MAX*2]; // Be generous and read even more
if (fgets(line, sizeof line, ofp)) {
现在解析它
STUDENT st = { 0 };
int n = 0;
// Format broken up to help illustrate parsing
sscanf(line, " %19[^/]" "/" "%d " "/"
"%d " "/" "%d " "/"
" %9[^/]" "/" " %4[^/]" "/"
" %49[^/]" "/" " %d " "/"
"%f " " %n",
st.name, &st.ID,
&st.age, &scholarship[0],
st.schoolstat, st.abroad,
st.address, &st.phone,
st.cgpa, &n);
// Check if scan completed with no extra trailing junk
if (n == 0 || line[n] != '\0') {
; // Handle bad input
} else {
; Use input
}
}
关于c - 无法从文本文件读取数据并将其放入结构和 union 以在另一个文本文件和输出窗口中获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72930083/