首页 新闻 会员 周边

C读写文件问题

0
悬赏园豆:50 [已解决问题] 解决于 2011-03-19 08:06

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

int main()
{
 FILE *input;
 char *line;
 char *result = NULL;
 int length,i;
 int buff_size=200;

 input = fopen("e:\\kddcup.data_10_percent_corrected","r");
 if(input == NULL)
 {
  printf("\nerror on open e:\\kddcup.data_10_percent_corrected file!");
  exit(0);
 }

 while (fgets( line, buff_size, input ) != NULL) {

 result = strtok(line, ",");    //read the first attribute
 while( result != NULL )
 printf( "%s \n", result );

 result = strtok(NULL, ",");    //continue reading next attribute
 }      //while end

fclose(input);

}

文件是这样的:

0,tcp,ftp_data,SF,854,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0.00,0.00,0.00,0.00,1.00,0.00,0.00,151,119,0.42,0.47,0.41,0.02,0.00,0.00,0.45,0.01,normal.
0,tcp,ftp_data,SF,59,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,12,12,0.00,0.00,0.00,0.00,1.00,0.00,0.00,161,121,0.45,0.44,0.45,0.02,0.00,0.00,0.42,0.01,normal.
0,tcp,ftp_data,SF,854,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,4,4,0.00,0.00,0.00,0.00,1.00,0.00,0.00,171,122,0.49,0.42,0.48,0.02,0.00,0.00,0.40,0.01,normal.
请大家帮忙,谢谢啦

ttssrs的主页 ttssrs | 初学一级 | 园豆:82
提问于:2011-03-17 12:15
< >
分享
最佳答案
0

看到了两点:

1)line 未初始化,你下面直接使用了

2)while( result != NULL )
 printf( "%s \n", result );

 result = strtok(NULL, ",");    //continue reading next attribute

这里明显漏了个大括号。

修改如下:

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

int main()
{
FILE
*input;
char*result = NULL;
//int length,i;//*********************************
int buff_size=200;
char*line = (char*)malloc(buff_size);//*********************************

input
= fopen("e:\\kddcup.data_10_percent_corrected","r");
if(input == NULL)
{
printf(
"\nerror on open e:\\kddcup.data_10_percent_corrected file!");
free(line);
//*********************************
exit(0);
}

while (fgets( line, buff_size, input ) != NULL)
{
result
= strtok(line, ","); //read the first attribute
while( result != NULL )
{
//*********************************
printf( "%s \n", result );
result
= strtok(NULL, ","); //continue reading next attribute
}//*********************************
}//while end

fclose(input);
free(line);
//*********************************
return0;//*********************************
}

 修改的都加了这个//********************************* 

收获园豆:40
Rusty's code | 菜鸟二级 |园豆:410 | 2011-03-17 13:18
result = strtok(line, ","); 这里不是已经对result赋值了?
而且加了大括号也不对
ttssrs | 园豆:82 (初学一级) | 2011-03-17 13:21
非常感谢您。我要把一个文件里所有内容改变格式,然后写入到另一个文件,就是tcp等字符用数字代替,请问怎么写?while( result != NULL )吗?
ttssrs | 园豆:82 (初学一级) | 2011-03-17 14:15
你想怎么改?具体给个例子。
Rusty's code | 园豆:410 (菜鸟二级) | 2011-03-17 22:08
@Rusty's code:0,tcp,ftp_data,SF,854,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,2,2,0.00,0.00,0.00,0.00,1.00,0.00,0.00,151,119,0.42,0.47,0.41,0.02,0.00,0.00,0.45,0.01,normal.
文件内容是上面的格式,我想把normal以前的东西放进excel里,这个已经实现,下一步是取得normal.这个字符串,然后判断他是否在某个集合范围内,请问应该怎么实现?
ttssrs | 园豆:82 (初学一级) | 2011-03-19 08:10
其他回答(2)
0

 1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <stdlib.h>
  5 int main()
  6 {
  7  FILE *input;
  8  int buff_size=200;
  9  char *line = (char*)malloc(buff_size);
 10  char *result = NULL;
 11  int length,i;
 12  input = fopen("e:\\kddcup.data_10_percent_corrected","r");
 13
 14  if(input == NULL)
 15  {
 16   printf("\nerror on open e:\\kddcup.data_10_percent_corrected file!");
 17   exit(0);
 18  }
 19  while (fgets( line, buff_size, input ) != NULL)
 20  {
 21    result = strtok(line, ",");    //read the first attribute
 22    while( result != NULL )
 23    {
 24     printf( "%s \n", result );
 25     result = strtok(NULL, ",");    //continue reading next attribute
 26    }
 27  }      //while end
 28 fclose(input);
 29
 30 }

 

 

以上代码可以跑出结果

0
tcp
ftp_data
SF
854
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
2
2
0.00
0.00
0.00
0.00
1.00
0.00
0.00
151
119
0.42
0.47
0.41
0.02
0.00
0.00
0.45
0.01
normal.

0
tcp
ftp_data
SF
59
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
12
12
0.00
0.00
0.00
0.00
1.00
0.00
0.00
161
121
0.45
0.44
0.45
0.02
0.00
0.00
0.42
0.01
normal.

0
tcp
ftp_data
SF
854
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
4
4
0.00
0.00
0.00
0.00
1.00
0.00
0.00
171
122
0.49
0.42
0.48
0.02
0.00
0.00
0.40
0.01
normal.

收获园豆:5
~我是卖菜的 | 园豆:210 (菜鸟二级) | 2011-03-17 14:16
我要把一个文件里所有内容改变格式,全部用数字表示,然后写入到另一个文件,请问应该怎么弄呢?
支持(0) 反对(0) ttssrs | 园豆:82 (初学一级) | 2011-03-17 14:21
0

这个功能直接使用编辑器替换就行,如UltraEdit(正则表达式条件替换更强),除非是必须使用,最好使用工具

收获园豆:5
2012 | 园豆:21230 (高人七级) | 2011-03-17 15:08
下载UltraEdit试了一下,觉得不能满足我的需求,我要把所有都好全去掉。
支持(0) 反对(0) ttssrs | 园豆:82 (初学一级) | 2011-03-17 16:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册