使用fscanf(C)将字符串从文本文件保存到结构
示例文本文件:使用fscanf(C)将字符串从文本文件保存到结构,c,C,示例文本文件: 234765 PETER 867574 SMITH 我试图从文本文件中获取id和字符串,并将其保存到结构中。id可以保存,但字符串不能保存 typedef struct student { int id[DATA_SIZE]; char *student[DATA_SIZE]; }studentinfo; studentinfo list; struct student *create_space(int size) { struct stud
234765 PETER
867574 SMITH
我试图从文本文件中获取id和字符串,并将其保存到结构中。id可以保存,但字符串不能保存
typedef struct student
{
int id[DATA_SIZE];
char *student[DATA_SIZE];
}studentinfo;
studentinfo list;
struct student *create_space(int size)
{
struct student *tmp = (struct student*)malloc(size*sizeof(struct student));
return(tmp);
}
struct student * readData(struct student*pointer,studentinfo v)
{
int count =0;
int tmpid;
char str[256];
FILE* in_file;
in_file = fopen("studentlist.txt","r");
while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
{
fscanf(in_file,"%s",v.student[count]);
//printf("%s\n",str );
v.id[count]=tmpid;
count++;
}
pointer =&v;
return pointer;
}
int main()
{
struct student *data;
struct student *sdata;
data = create_space(1);
sdata = readData(data,list);
//printf("%s\n",sdata->student[2] );
}
typedef结构学生
{
int id[数据大小];
字符*学生[数据大小];
}学生信息;
学生信息列表;
结构学生*创建_空间(整数大小)
{
struct student*tmp=(struct student*)malloc(size*sizeof(struct student));
返回(tmp);
}
结构学生*读取数据(结构学生*指针,学生信息v)
{
整数计数=0;
int-tmpid;
char-str[256];
文件*在_文件中;
in_file=fopen(“studentlist.txt”、“r”);
while(fscanf(在_文件中,“%d”&tmpid)!=EOF&&countstudent[2]);
}
它们有几个问题:
- 读取格式化输入,并返回读取的项数
这一行:
while(fscanf(in_file,"%d",&tmpid)!= EOF && count<DATA_SIZE)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_SIZE 256
typedef struct {
int id[DATA_SIZE];
char *student[DATA_SIZE];
} studentinfo_t;
int main(void) {
FILE *in_file;
studentinfo_t list;
char str[DATA_SIZE];
size_t count = 0;
in_file = fopen("studentlist.txt", "r");
if (!in_file) {
fprintf(stderr, "%s\n", "Error reading file");
exit(EXIT_FAILURE);
}
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
list.student[count] = malloc(strlen(str)+1);
if (!list.student[count]) {
printf("Cannot allocate string\n");
exit(EXIT_FAILURE);
}
strcpy(list.student[count], str);
count++;
}
for (size_t i = 0; i < count; i++) {
printf("%d %s\n", list.id[i], list.student[i]);
}
return 0;
}
#包括
#包括
#包括
#定义数据大小256
类型定义结构{
int id[数据大小];
字符*学生[数据大小];
}学生信息;
内部主(空){
文件*在_文件中;
学生信息列表;
char str[数据大小];
大小\u t计数=0;
in_file=fopen(“studentlist.txt”、“r”);
如果(!在_文件中){
fprintf(stderr,“%s\n”,“读取文件时出错”);
退出(退出失败);
}
而(计数<数据大小和&fscanf(在文件中,%d%255s,&list.id[count],str)==2){
学生人数=malloc(strlen(str)+1);
如果(!list.student[计数]){
printf(“无法分配字符串\n”);
退出(退出失败);
}
strcpy(list.student[count],str);
计数++;
}
对于(大小i=0;i
char*学生[数据大小]代码>-->char学生[数据大小][姓名大小]代码>指针=&v;返回指针代码>:v
是局部变量。因此无法使用此返回值。另外,pointer
不需要作为此函数的参数。然后如何将从文本中获得的字符串值分配给struct student[DATA_SIZE][NAME_SIZE]?如果v
是指针,fscanf(在_文件中,“%s”,v->student[count])代码>。如果v
不是指针,fscanf(在_文件中,“%s”,v.student[count])我还有一个问题,为什么我不能声明一个变量char str[32];在函数内并执行fscanf(在_文件中,“%s”,str);,然后v.student[count]=str;。这给了我一个错误。为什么呢?
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
/* allocate space for one student */
list.student[count] = malloc(strlen(str)+1);
if (!list.student[count]) {
printf("Cannot allocate string\n");
exit(EXIT_FAILURE);
}
/* copy it into array */
strcpy(list.student[count], str);
count++;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_SIZE 256
typedef struct {
int id[DATA_SIZE];
char *student[DATA_SIZE];
} studentinfo_t;
int main(void) {
FILE *in_file;
studentinfo_t list;
char str[DATA_SIZE];
size_t count = 0;
in_file = fopen("studentlist.txt", "r");
if (!in_file) {
fprintf(stderr, "%s\n", "Error reading file");
exit(EXIT_FAILURE);
}
while (count < DATA_SIZE && fscanf(in_file, "%d %255s", &list.id[count], str) == 2) {
list.student[count] = malloc(strlen(str)+1);
if (!list.student[count]) {
printf("Cannot allocate string\n");
exit(EXIT_FAILURE);
}
strcpy(list.student[count], str);
count++;
}
for (size_t i = 0; i < count; i++) {
printf("%d %s\n", list.id[i], list.student[i]);
}
return 0;
}