Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t使用循环在文本文件中写入文本_C_Loops_Pointers_Struct_Pointer Arithmetic - Fatal编程技术网

Can';t使用循环在文本文件中写入文本

Can';t使用循环在文本文件中写入文本,c,loops,pointers,struct,pointer-arithmetic,C,Loops,Pointers,Struct,Pointer Arithmetic,我试图使用循环将文本写入文件,但当我使用(ogrenci+I)而不是(ogrenci+0)时,我在txt文件中得到了一些奇怪的数字和文本 这样写时(ogrenci+0)工作正常。我做错了什么 我在另一个函数中使用指向struct的指针 这就是问题所在 问题 假设您得到了下面的结构 typedef struct StudentMark { char name[20]; char surname[20]; int midterm; int final; }STUDENT_MARK; 1-)写下一个

我试图使用循环将文本写入文件,但当我使用
(ogrenci+I)
而不是
(ogrenci+0)
时,我在txt文件中得到了一些奇怪的数字和文本

这样写时(ogrenci+0)工作正常。我做错了什么

我在另一个函数中使用指向struct的指针

这就是问题所在 问题

假设您得到了下面的结构

typedef struct StudentMark {
char name[20];
char surname[20];
int midterm;
int final;
}STUDENT_MARK;
1-)写下一个包含

a-)将用户输入的姓名和考试分数输入到 动态地 分配的学生分数结构(您的函数必须检查输入的有效性 即 输入的标记必须介于[0..100]之间

b-)将输入的有效结构写入文件的函数 命名为 标记您的学生ID.txt

2-)编写一个包含

a-)一个函数,用于读取名为marks\u YOUR\u STUDENT\u ID.txt的文件,该文件包含 学生分数结构的数据

b-)计算每个学生考试成绩平均值并写出结果的函数 在屏幕上显示为

“学生姓名姓氏的期中成绩是期中成绩,期末成绩是。” 最终,他/她的平均成绩为“平均”

void girme (int studentnum){
int i;
studentnum = 2;
STUDENT_MARK *ogrenci;

ogrenci = (STUDENT_MARK*) malloc(studentnum * sizeof(STUDENT_MARK));
if(ogrenci == NULL) { exit(1); }

for(i=0;i<studentnum;i++)
{

printf("Enter the student's name, surname, midterm and final respectively: \n");
scanf("%s %s %d %d",(ogrenci+i)->name, (ogrenci+i)->surname, &(ogrenci+i)->midterm, &(ogrenci+i)->final);

if((ogrenci+i)->midterm > 100 || (ogrenci+i)->midterm < 0 || (ogrenci+i)->final > 100 || (ogrenci+i)->final < 0)
    {
        printf("midterm or final can not be higher than 100 or lower than 0 \n");
        exit(1);
    }
}

}

void yazma (int studentnum){

int i;
STUDENT_MARK *ogrenci;
FILE *dosya;
dosya = fopen("marks_190704033.txt","w");
if{ (dosya == NULL)
    {
    printf("Could not open file");
    exit(1); 
    }
else
{

    for(i=0;i<studentnum;i++)
        {
            fprintf(dosya, "%s %s %d %d", (ogrenci+0)->name, (ogrenci+0)- 
>surname, (ogrenci+0)->midterm, (ogrenci+0)->final);
    }


}
fclose(dosya);
}


int main()
{

int n =2;
girme(n);
yazma(n);


return 0;
}
void girme(int studentnum){
int i;
studentnum=2;
学生马克*奥格伦奇;
ogrenci=(学生分数*)malloc(学生分数*sizeof(学生分数));
如果(ogrenci==NULL){exit(1);}
对于(i=0;在南,(ogrenci+i)->姓氏,&(ogrenci+i)->期中,&(ogrenci+i)->期末考试);
如果((ogrenci+i)->期中>100 | | | |(ogrenci+i)->期中<0 | | |(ogrenci+i)->期末>100 | |(ogrenci+i)->期末<0)
{
printf(“期中或期末考试不能高于100或低于0\n”);
出口(1);
}
}
}
void yazma(int studentnum){
int i;
学生马克*奥格伦奇;
文件*dosya;
dosya=fopen(“marks_190704033.txt”,“w”);
如果{(dosya==NULL)
{
printf(“无法打开文件”);
出口(1);
}
其他的
{
对于(i=0;iname,(ogrenci+0)——
>姓氏,(奥格伦西+0)->期中,(奥格伦西+0)->期末;
}
}
fclose(dosya);
}
int main()
{
int n=2;
girme(n);
亚兹马(n);
返回0;
}
ogrenci
是一个单个指针,指向结构
STUDENT\u标记
,指向为
struct STUDENT\u标记
的多个对象分配的空间

使用时,f.e.:

(ogrenci+i)->name
for
循环中,尝试访问不存在的结构对象的非现有结构指针

注意:编译器不会将分配的空间与多个指针相关联


如果您想使用(
ogrenci+i
)等指针算术,您需要将
ogrenci
定义为指向
学生标记的指针数组:

int studentnum = 5;
STUDENT_MARK *ogrenci[studentnum];            
int studentnum = 5;
STUDENT_MARK **ogrenci;

ogrenci = malloc(sizeof(*ogrenci) * studentnum);
*ogrenci = malloc(sizeof(**ogrenci) * studentnum);
并通过现有结构对象的地址初始化每个指针,每个结构对象单独分配了空间,例如:

int studentnum = 5;
STUDENT_MARK *ogrenci[studentnum];

for(int i = 0; i < studentnum; i++)
{
    ogrenci[i] = malloc(sizeof(*ogrenci));
}

“像这样书写(
ogrenci+0
)时,它工作正常。”

但是,它与
0
一起“工作”,因为
ogrenci+0=ogrenci
。与
ogrenci
没有区别


旁注:正如您可能已经看到的,我省略了
malloc
中返回指针的强制转换。这是因为它是不必要的,并且可能会“增加混乱”到您的代码中:

ogrenci
是一个单个指针,指向结构
STUDENT\u标记
,指向为
struct STUDENT\u标记
的多个对象分配的空间

使用时,f.e.:

(ogrenci+i)->name
for
循环中,尝试访问不存在的结构对象的非现有结构指针

注意:编译器不会将分配的空间与多个指针相关联


如果您想使用(
ogrenci+i
)等指针算术,您需要将
ogrenci
定义为指向
学生标记的指针数组:

int studentnum = 5;
STUDENT_MARK *ogrenci[studentnum];            
int studentnum = 5;
STUDENT_MARK **ogrenci;

ogrenci = malloc(sizeof(*ogrenci) * studentnum);
*ogrenci = malloc(sizeof(**ogrenci) * studentnum);
并通过现有结构对象的地址初始化每个指针,每个结构对象单独分配了空间,例如:

int studentnum = 5;
STUDENT_MARK *ogrenci[studentnum];

for(int i = 0; i < studentnum; i++)
{
    ogrenci[i] = malloc(sizeof(*ogrenci));
}

“像这样书写(
ogrenci+0
)时,它工作正常。”

但是,它与
0
一起“工作”,因为
ogrenci+0=ogrenci
。与
ogrenci
没有区别



旁注:正如您可能已经看到的,我省略了
malloc
中返回指针的强制转换。这是因为它是不必要的,并且可能会给代码“添加混乱”:fprintf(“错误”)

总是错误的。错误消息属于stderr,而不是stdout。与
fopen
相关的错误应包括使用的路径和故障原因。例如
if((f=fopen(path,mode))==NULL){perror(path);…
如果我们假设ogrenci指向正确初始化数组的第一个成员,其中所有成员都已正确初始化,那么您拥有的应该可以工作。由于您拥有的不工作,您的初始化可能不正确。我希望您也将studentnum初始化为合理的值。如果您不这样做正确初始化该值,您可能会得到不可预测的结果。例如,如果系统将该值初始化为0…您的循环将永远不会运行。您所说的“I attent pointer to struct in a nother function”是什么意思?获取指针?分配指针?尝试获取指针?我无法更正该部分。我猜“我在另一个函数中加入指向结构的指针”的意思是“我在另一个函数中设置指向结构的指针”。我怀疑这个“不同的函数”出现问题,无法正常工作。请编辑您的问题并包括此功能。谢谢。
fprintf(“错误”)
总是错误的。错误消息属于stderr,而不是stdout。与
fopen
相关的错误应包括使用的路径和失败的原因。例如
if((f=fopen))(pa)