linux系统在往文件写入之后,读出来后面却又乱码原因
来源:互联网 发布:苹果手机网络无法连接 编辑:程序博客网 时间:2024/05/18 03:23
===================================================================================================================================
linux系统下文件写入,读取出现乱码原因
前一段时间做好了FL2440开发板eeprom的驱动,今天在编写往eeprom中读写的测试程序,可以往里面写入,但是在读出来的时候会出现乱码问题。
有问题的代码
#include <stdio.h>#include <string.h>#include <sys/ioctl.h>#include <stdlib.h>#include <fcntl.h>#include <sys/io.h>#include <getopt.h>/******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/int main (int argc, char **argv){ int fd, ret, opt; char a_data[5]; fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_RDWR); while((opt=getopt(argc,argv,"rw:"))!=-1) { switch(opt) { case 'r': lseek(fd,0,SEEK_SET); ret = read(fd,a_data,5); printf("the information we read from eeprom is %s !\n",a_data); break; case 'w': strcpy(a_data,optarg); lseek(fd,0,SEEK_SET); ret = write(fd,a_data,5); break; } } return 0;} /* ----- End of main() ----- */
有问题的结果:
>:./write -w 12345
>:./write -r
the information we read from eeprom is 12345 @
>:./write -r
the information we read from eeprom is 12345?@
****************************************************************************************************************
出现问题的原因:
1.是没有将读的缓存a_data先memset设置为0
2.在字符串的输出时以“\0”为结束标志的,我在字符串里面装5个字符,就要申请6个字符的空间,最后一个放置“\0”
****************************************************************************************************************
修改之后的程序
#include <stdio.h>#include <string.h>#include <sys/ioctl.h>#include <stdlib.h>#include <fcntl.h>#include <sys/io.h>#include <getopt.h>#include <memory.h>/******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/int main (int argc, char **argv){ int fd, ret, opt; char a_data[6]; memset(a_data,0,6); fd = open("/sys/devices/platform/s3c2440-i2c/i2c-0/0-0050/eeprom",O_RDWR); while((opt=getopt(argc,argv,"rw:"))!=-1) { switch(opt) { case 'r': lseek(fd,0,SEEK_SET); ret = read(fd,a_data,5); printf("the information we read from eeprom is %s !\n",a_data); break; case 'w': strcpy(a_data,optarg); lseek(fd,0,SEEK_SET); ret = write(fd,a_data,5); break; } } return 0;} /* ----- End of main() ----- */
测试的结果:
>:./write -w 12345
>:./write -r
the information we read from eeprom is 12345 !
- linux系统在往文件写入之后,读出来后面却又乱码原因
- 将字符串写入文件并读出来
- 解决linux下写一个文件之后立马读,读出乱码或者空白的问题
- 把结构用二进制方式写入文件,并读出来
- INF文件写入读出
- 写入、读出文件
- c#写入文件读出文件
- 文件的写入与读出
- 体验文件写入/读出--char3
- 文件的写入与读出
- CFile在写入Unicode编码文件出现乱码---原因及解决办法
- 将硬盘中两个TXT文件读出来,并写入另一个文件
- shell 讲字符串当命令将输出写入文件,并处文件中读出来
- Java写入读出MySQL乱码问题
- map写入文件及从文件读出
- Linux 打开文件并写入一段字符串,同时读出对应文件的信息--fopen()/fwrite()/fread()
- mysql文件写入和读出数据库
- CStdioFile:文件写入/读出 自动换行
- 对数据更新的认识
- 面试题
- CentOS6.5系统挂载NTFS分区的移动硬盘
- 进程和线程关系及区别
- ubuntu&windows下安装使用tftp
- linux系统在往文件写入之后,读出来后面却又乱码原因
- linux环境下安装Android SDK
- 模拟退火算法的详细理解
- 内存泄漏的检测、定位和解决经验总结
- Eclipse的基本使用
- 1.如何使用VISIO画UML图
- Swift(十八、错误处理)
- web前端关于兼容性的问题。
- C++内存泄露心得