fsscanf与fprintf使用心得

本文分享了在项目中使用fprintf写入文件和fscanf读取文件时的经验,强调了数据格式匹配的重要性。由于fscanf对数据格式的严格要求,遇到空格或换行会停止读取,可能导致读取错误。为解决这个问题,作者建议使用fgets配合sscanf,通过自定义标志符来更灵活地解析文件内容。

      项目忙的最近一个多月都没有写博客了;

       前两天在做项目时遇到要一个结构体中的内容格式化的写进一个文件中然后再格式化的读出来,就可以使用fprintf和fscanf,那么有个问题就是fprintf写文件和fscanf读文件时的数据格式必须完全匹配,如果有不相符的地方,那么就会失败。而且fscanf对数据的要求很严格,他读到空格或是换行时就会失败,所以如果将一个空的字符串写进一个文件中,那么在用fcanf这个字符串时就会将这个空格代表的字符串后面以及下一个空格之前的内容给读出来,所以就会读的移位了,导致程序异常;

       如果你用逗号等作为标志符来区分的话,遇到有空格的字符串的话还是会出现问题;所以总结就是fcanf真难用;
 想了个办法就是用fgets将一行的内容读到一个buf中,然后再用sscanf来进行解析,这样你可以在写文件时加上标志符,在解析文件时根据这些标志符来解析进不同的变量中就可以了;

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

#define SNMP_TRAP_FILE "trapserver.txt"


typedef struct
{
   int busyindex;
   unsigned char traplistipv4Addr[20];
   int traplistPort;
   int version_snmp;
   unsigned char community[256];
   int timeOut;
   int retries;

}trapServerList_t;

trapServerList_t trapServerList[3];


int main(void)
{
 
    FILE	*fptr;
    int k = 0;
    int ret;
    trapServerList_t trapInfo;
    char tmp_buf[500] = "\0";
    
    printf("in %s(%d):gogogo!\n", __FUNCTION__, __LINE__);
    
    memset(&(trapInfo.busyindex), 0, sizeof(trapServerList_t));
    
    memset(trapServerList, 0, sizeof(trapServerList_t)*3);

    printf("in %s(%d):gogogo!\n", __FUNCTION__, __LINE__);
    
            trapServerList[0].busyindex = 1;
            memcpy(trapServerList[0].traplistipv4Addr, "192.168.2.32", strlen("192.168.2.32"));
			trapServerList[0].traplistPort=162;
			trapServerList[0].version_snmp=1;
			memcpy(trapServerList[0].community, "netcore", strlen("netcore"));
			trapServerList[0].timeOut=5;
			trapServerList[0].retries=0;

              trapServerList[1].busyindex = 1;
              memcpy(trapServerList[1].traplistipv4Addr, "192.168.2.132", strlen("192.168.2.132"));
			trapServerList[1].traplistPort=162;
			trapServerList[1].version_snmp=1;
			memcpy(trapServerList[1].community, "netcoreo", strlen("netcore0"));
			trapServerList[1].timeOut=5;
			trapServerList[1].retries=0;
			
			trapServerList[2].busyindex = 1;
              memcpy(trapServerList[2].traplistipv4Addr, "", strlen(""));
			trapServerList[2].traplistPort=162;
			trapServerList[2].version_snmp=1;
			memcpy(trapServerList[2].community, "netcoreo", strlen("netcore0"));
			trapServerList[2].timeOut=5;
			trapServerList[2].retries=0;

    fptr = fopen(SNMP_TRAP_FILE, "wr");


	if (fptr == NULL)
	{
		printf("***********Cannot Open file %s for write!**************\n", SNMP_TRAP_FILE);
        return;
	}

    for(k=0; k<3; k++)
    {
       // if(1==snmpmodel_printf_dbg)
			//{
                printf("in %s(%d):trapServerList[%d].busyindex[%d]\n", __FUNCTION__, __LINE__, k, trapServerList[k].busyindex);
                printf("in %s(%d):trapServerList[%d].traplistipv4Addr[%s]\n", __FUNCTION__, __LINE__, k, trapServerList[k].traplistipv4Addr);
                printf("in %s(%d):trapServerList[%d].traplistPort[%d]\n", __FUNCTION__, __LINE__, k, trapServerList[k].traplistPort);
                printf("in %s(%d):trapServerList[%d].version_snmp[%d]\n", __FUNCTION__, __LINE__, k, trapServerList[k].version_snmp);
                printf("in %s(%d):trapServerList[%d].community[%s]\n", __FUNCTION__, __LINE__, k, trapServerList[k].community);
                printf("in %s(%d):trapServerList[%d].timeOut[%d]\n", __FUNCTION__, __LINE__, k, trapServerList[k].timeOut);
                printf("in %s(%d):trapServerList[%d].retries[%d]\n", __FUNCTION__, __LINE__, k, trapServerList[k].retries);
			//}
        ret = fprintf(fptr,"%d ,%s ,%u ,%u ,%s ,%u ,%u\n",
                    trapServerList[k].busyindex,
					trapServerList[k].traplistipv4Addr,
					trapServerList[k].traplistPort,
					trapServerList[k].version_snmp,
					trapServerList[k].community,
					trapServerList[k].timeOut,
					trapServerList[k].retries);
					printf("in %s(%d):ret[%d]\n", __FUNCTION__, __LINE__, ret);
	
	/*ret = fscanf(fptr,"%d %s %u %u %s %u %u\n",
                    &trapInfo.busyindex,
					trapInfo.traplistipv4Addr,
					&trapInfo.traplistPort,
					&trapInfo.version_snmp,
					trapInfo.community,
					&trapInfo.timeOut,
					&trapInfo.retries);
                    printf("in %s(%d):ret[%d]\n", __FUNCTION__, __LINE__, ret);
                    printf("in %s(%d):trapInfor.busyindex[%d]\n", __FUNCTION__, __LINE__, trapInfo.busyindex);
                    printf("in %s(%d):trapInfor.traplistipv4Addr[%s]\n", __FUNCTION__, __LINE__, trapInfo.traplistipv4Addr);
                    printf("in %s(%d):trapInfor.traplistPort[%d]\n", __FUNCTION__, __LINE__, trapInfo.traplistPort);
                    printf("in %s(%d):trapInfor.version_snmp[%d]\n", __FUNCTION__, __LINE__, trapInfo.version_snmp);
                    printf("in %s(%d):trapInfor.community[%s]\n", __FUNCTION__, __LINE__, trapInfo.community);
                    printf("in %s(%d):trapInfor.timeOut[%d]\n", __FUNCTION__, __LINE__, trapInfo.timeOut);
                    printf("in %s(%d):trapInfor.retries[%d]\n", __FUNCTION__, __LINE__, trapInfo.retries);*/
                                    

    }
    
    fclose(fptr);
    
    
    fptr = fopen(SNMP_TRAP_FILE, "r");


	if (fptr == NULL)
	{
		printf("***********Cannot Open file %s for write!**************\n", SNMP_TRAP_FILE);
        return;
	}
    
    for(k=0; k<3; k++)
    {   
    	     memset(&(trapInfo.busyindex), 0, sizeof(trapServerList_t));
    	      ret = fscanf(fptr,"%d ,%s ,%u ,%u ,%s ,%u ,%u\n",
                    &trapInfo.busyindex,
					trapInfo.traplistipv4Addr,
					&trapInfo.traplistPort,
					&trapInfo.version_snmp,
					trapInfo.community,
					&trapInfo.timeOut,
					&trapInfo.retries);
                    printf("in %s(%d):ret[%d]\n", __FUNCTION__, __LINE__, ret);
                    printf("in %s(%d):trapInfor.busyindex[%d]\n", __FUNCTION__, __LINE__, trapInfo.busyindex);
                    printf("in %s(%d):trapInfor.traplistipv4Addr[%s]\n", __FUNCTION__, __LINE__, trapInfo.traplistipv4Addr);
                    printf("in %s(%d):trapInfor.traplistPort[%d]\n", __FUNCTION__, __LINE__, trapInfo.traplistPort);
                    printf("in %s(%d):trapInfor.version_snmp[%d]\n", __FUNCTION__, __LINE__, trapInfo.version_snmp);
                    printf("in %s(%d):trapInfor.community[%s]\n", __FUNCTION__, __LINE__, trapInfo.community);
                    printf("in %s(%d):trapInfor.timeOut[%d]\n", __FUNCTION__, __LINE__, trapInfo.timeOut);
                    printf("in %s(%d):trapInfor.retries[%d]\n", __FUNCTION__, __LINE__, trapInfo.retries);
    	}
    	
    	 fclose(fptr);
    
    /*fptr = fopen(SNMP_TRAP_FILE, "r");


	if (fptr == NULL)
	{
		printf("***********Cannot Open file %s for write!**************\n", SNMP_TRAP_FILE);
        return;
	}
    
    while((fgets(tmp_buf,500,fptr))!=NULL)

                   {
                   	
                   	memset(&(trapInfo.busyindex), 0, sizeof(trapServerList_t));

                       printf("in %s(%d):tmp_buf[%s]\n", __FUNCTION__, __LINE__, tmp_buf);
                       sscanf(tmp_buf, "%d,%[^,],%d,%d,%[^,],%d,%d", 
                       &trapInfo.busyindex,
					trapInfo.traplistipv4Addr,
					&trapInfo.traplistPort,
					&trapInfo.version_snmp,
					trapInfo.community,
					&trapInfo.timeOut,
					&trapInfo.retries);
					
                    printf("in %s(%d):trapInfor.busyindex[%d]\n", __FUNCTION__, __LINE__, trapInfo.busyindex);
                    printf("in %s(%d):trapInfor.traplistipv4Addr[%s]\n", __FUNCTION__, __LINE__, trapInfo.traplistipv4Addr);
                    printf("in %s(%d):trapInfor.traplistPort[%d]\n", __FUNCTION__, __LINE__, trapInfo.traplistPort);
                    printf("in %s(%d):trapInfor.version_snmp[%d]\n", __FUNCTION__, __LINE__, trapInfo.version_snmp);
                    printf("in %s(%d):trapInfor.community[%s]\n", __FUNCTION__, __LINE__, trapInfo.community);
                    printf("in %s(%d):trapInfor.timeOut[%d]\n", __FUNCTION__, __LINE__, trapInfo.timeOut);
                    printf("in %s(%d):trapInfor.retries[%d]\n", __FUNCTION__, __LINE__, trapInfo.retries);

                   }

    fclose(fptr);*/
    
    return 0;


}
这是我的一个测试程序,有些乱;不明白为什么将fscanf放在for里面就读失败了,但是放在外面就可以读成功,很是纳闷;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值