简单的读取文件的每行内容

最近在做音频处理的东西,分类svm后得到如下的文件

1

.......
1

1

的这样一个文件,每行只有-1或1,现为统计分类器的准确性,需要统计出1和-1的个数

程序开始如下

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

int main()
{
	FILE *fp;
	char *buf;
	char file[] = "/home/zf/zf/2013-11-18/out_V.txt";
	extern int errno;
	int N_audio = 0;
	int V_audio = 0;
	fp = fopen(file,"r+");
	if(fp==NULL)
	{
		printf("cannot open this file!\n");
		printf("errno: %d\n",errno);
		printf("ERR:   %s\n",strerror(errno));
	}	
	else
	{
		printf("%s is opened!\n",file);
	}
	while(fgets(buf,10,fp)!=NULL)
	{
		//printf("OK");
		printf("%s",buf);
		if(strcmp(buf,"1")==1)
			N_audio++;
		else
			V_audio++;
	}
	//printf("%s\n",buf);
	printf("N_audio = %d\nV_audio = %d\n",N_audio,V_audio);

	return 0;
	
	
}

运行结果永远提示我segmental fault

最后才发现自己犯了个错误

没有给字符指针赋初值就使用,导致错误发生

加上这句话

memset(buf,0,sizeof(buf));

问题没有了

在我印象中字符指针可以直接使用的为什么要赋初值,看来还是对指针了解不清楚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值