关于c的printf错位问题。。。
coursework2的作业要求写一个程序从一个stops.txt里面读取自行车站点的信息,在命令行输入起点和终点的经度纬度,计算出离起点最近的stops和终点的stops,最后再print出来。正确的print应该像是这个样子的:
程序代码:
F:\Introduction to Programming\bikehelper>bikehelper 121.593567 29.86417 121.589 78 29.88098 Starting from 121.593567 29.864170 Walk North-West to haoyangguangshangwu at 121.593560 29.864180 Cycle North to tiyuguanbei at 121.589600 29.879370 Walk North to your destination at 121.589780 29.880980. F:\Introduction to Programming\bikehelper>
我这一部分的代码是酱紫的:
程序代码:
... printf("Starting from %lf %lf\nWalk ",location[1],location[2]); direction(location[1],location[2],stops_info[stop_s].stop_longitude,stops_info[stop_s].stop_latitude); printf(" to %s at %lf %lf\nCycle ",stops_info[stop_s].stop_name,stops_info[stop_s].stop_longitude,stops_info[stop_s].stop_latitude); direction(stops_info[stop_s].stop_longitude,stops_info[stop_s].stop_latitude,stops_info[stop_d].stop_longitude,stops_info[stop_d].stop_latitude); printf(" to %s at %lf %lf\nWalk ",stops_info[stop_d].stop_name,stops_info[stop_d].stop_longitude,stops_info[stop_d].stop_latitude); direction(stops_info[stop_d].stop_longitude,stops_info[stop_d].stop_latitude,location[3],location[4]); printf(" to your destination at %lf %lf.\n",location[3],location[4]); exit(0); }我在我自己的WinXP下的dev下测试是完全正常的,但是在Ubuntu下测试就会问题。
于是到学校的centos下测试,学校的centos属于虚拟主机那种,在user目录下printf是完全正常,但是如果是在user/test文件夹下测试就又会出现问题。
问题就如图片中显示的一样。。。。但是如果将编译结果拷贝出来运行,又不会出现这样子的错位问题。。。
<img src="http://bucket. />
一开始考虑是不是没有加'\0'的原因,但是加上之后问题依旧存在。
源码:[local]2[/local]