Fread_s保存特定单词?

时间:2016-02-04 20:41:52

标签: c file fread

我必须完成这个练习: 我必须从通用文本文件中读取,因此组成: [Nameperson] [space] [age],我必须在结构中保存名称(char)和年龄(unsigned int)。

我的问题是: 我不明白如何划分名称和年龄,也就是说,如果我以这种方式使用fread_s

  

fread_s(pp-> name,256,1,256,f);

该程序将我保存为名称es。

  

Pippo 25iiiiiiiiiiiiiiiiiiiiiiiiii ...

我知道fread_s函数保持读取的最后一个字母的位置,但我不知道如何使用这个对我有利 。 这是我的代码

#include <stdio.h>
#include <string.h>

struct person{
    char name[256];
    unsigned int age;
};

void person_read(FILE *f, struct person* pp) {
    fread_s(pp->name, 256, 1, 256, f);
}

感谢。

1 个答案:

答案 0 :(得分:1)

您使用了错误的功能,需要struct person person; if (person_read(file, &person) != 0) do_somethin_read_error(); else fprintf(stderr, "%s has %d years\n", person.name, person.age);

person->name

像这样使用

person->name

您的代码生成该输出的原因是因为您正在读取文件中的所有字节(或仅256 )到printf(),因为start() function start() { var Engine = Matter.Engine, World = Matter.World, Bodies = Matter.Bodies; Constraint = Matter.Constraint; Render = Matter.Render; var engine = Engine.create(document.body); var body = Bodies.rectangle(200, 20, 200, 40); body.density = 15; var wheelA = Bodies.circle(50,100,40); var wheelB = Bodies.circle(350,100,40); wheelA.density = 10; wheelB.density = 10; var axisAA = Constraint.create({bodyB:wheelA,bodyA:body,pointA:{x:-90,y:-15},pointB:{x:0,y:0},stiffness:0.8}, { isStatic: true }); var axisAB = Constraint.create({bodyB:wheelA,bodyA:body,pointA:{x:-10,y:15},pointB:{x:0,y:0},stiffness:0.2}); var axisBA = Constraint.create({bodyB:wheelB,bodyA:body,pointA:{x:90,y:-15},pointB:{x:0,y:0},stiffness:0.8}); var axisBB = Constraint.create({bodyB:wheelB,bodyA:body,pointA:{x:10,y:15},pointB:{x:0,y:0},stiffness:0.2}); var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true }); World.add(engine.world, [body, wheelA, wheelB, axisAA, axisAB, axisBA, axisBB, ground]); document.addEventListener('keydown', function(event) { if(event.keyCode == 39) { Matter.Body.setAngularVelocity(wheelA,0.1); Matter.Body.setAngularVelocity(wheelB,0.1); } else if(event.keyCode == 37) { Matter.Body.setAngularVelocity(wheelA,-0.1); Matter.Body.setAngularVelocity(wheelB,-0.1); } }); Engine.run(engine); } 不是< em> null 终止,将其传递给open(url)会产生您看到的输出,并且是*未定义的行为**。