fwrite和fread问题

sisistefanie 2012-03-12 11:26:53
我想写入和读取几个参数到一个文件里,连续输出中一个结果是
[walk] Current position is (7,3,0.857969)Next Goal is (9,6) wx is (1,1,0.857969)
我想让写入的参数是7和3,真不知道怎么弄成wx is (7,3)


while(wb_robot_step(TIME_STEP) != -1) {

odometry_track_step(&ot);
int x = wtoc_x(ot.result.x);
int y = wtoc_y(ot.result.y);
float theta=ot.result.theta;

printf("Current position is (%d,%d,%f)",x,y,theta);
run();

FILE *com = NULL;
size_t lenx = 0;
//size_t leny = 0;
com =fopen("C:/Users/WYP/Desktop/midterm/controllers/communication","w+");
if (com== NULL) {
printf("Error in opening a file..");
}
lenx = fwrite(&x, sizeof(x),1, com);
//leny = fwrite(&y, sizeof(y), 1, com);
fclose(com);
printf(" wx is (%d,%d,%f)\n",lenx);
}

...全文
83 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
IVERS0N 2012-03-13
  • 打赏
  • 举报
回复
fread和fwrite 应该用二进制方式打开文本
赵4老师 2012-03-13
  • 打赏
  • 举报
回复
不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待

fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了
Franklin_007 2012-03-13
  • 打赏
  • 举报
回复
printf(" wx is (%d,%d,%f)\n",lenx);
你这怎么只有lenx一个参数,应该有三个参数吧!这个编译器不报错误,但是肯定影响输出结果。
以后细心一些
lida2003 2012-03-13
  • 打赏
  • 举报
回复
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);

DESCRIPTION
The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧