文件读写

ttssrs 2011-03-19 09:15:53
我的文件是这样的:
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.
请问如何获得每行最后的normal,并判断它是否在某个集合中?
...全文
181 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 delphiwcdj 的回复:]
希望对你有帮助,按时结贴哦
[/Quote]
我的问题还没有解决耶,怎么判断最后那个normal处于哪个集合呀?
delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
希望对你有帮助,按时结贴哦
delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
改为:注意最多的一行元素,比如下面的10

char *attackType[][10]={
{"normal"},
{"ip sweep","nmap","portsweep","satan","saint","mscan"},
{"back","land","neptune","pod","smurf","teardrop","apache2","mailbomb","udpstorm","processtable"},// 10
{"perl","rootkit","loadmodule","buf_overflow","httptunned","ps","sqlttack","xterm"}};
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 delphiwcdj 的回复:]
引用 11 楼 ttssrs 的回复:

好像没有明白我的意思,我现在遇到的问题是result是指向normal中的“n”的指针,但是我希望获得normal而不是n。

引用 9 楼 newfarmerchi 的回复:
int temp=0;
或是定义成static,或放在while循环的外面(之上)

result = strtok(line, ","); //read the……
[/Quote]

谢谢指点,上面的问题解决了。下面这个定义报too many initializers错,这个怎么改呢?
char *attackType[]={{"normal"},
{"ip sweep","nmap","portsweep","satan","saint","mscan"},
{"back","land","neptune","pod","smurf","teardrop","apache2","mailbomb","udpstorm","processtable"},
{"perl","rootkit","loadmodule","buf_overflow","httptunned","ps","sqlttack","xterm"},
{"ftp-write","guess_passwd","multihop","phf","imap","spy","warezclient","waremaster","named","xsnoop","xlock","sendmail","worm","snmpgetattack","snmpguess"}};
newfarmerchi 2011-03-19
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <string.h>
int main ()
{
char str[100]="00,0.00,0.45,0.01,normal.";
char *p;
p=strtok(str,",");

while (p!=NULL)
{
if (!strcmp(p,"normal."))
{
printf("%s\n", p);
strcpy(p,"normal");
printf("%s\n",p);
}
p=strtok(NULL,",");
}

return 0;
}



newfarmerchi 2011-03-19
  • 打赏
  • 举报
回复
normal.这是normal的格式,有一个. strcmp(result,"normal")
就不对了,可否strcmp(result,"normal."),如果有,再对normal.
进行处理。
delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
偷懒点的办法用正则匹配

#include <iostream>
#include <string>
#include <regex>
#include <cassert>
using namespace std;

int main()
{
string str = "每一行内容";
tr1::regex rx("normal");

// 使用 regex_search

//assert( regex_search(str.begin(), str.end(), rx) );
if (regex_search(str.begin(), str.end(), rx))
{
cout<<"find it!"<<endl;// ok,说明此行有normal
}
else
{
cout<<"not find!"<<endl;
}
}

delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ttssrs 的回复:]

好像没有明白我的意思,我现在遇到的问题是result是指向normal中的“n”的指针,但是我希望获得normal而不是n。

引用 9 楼 newfarmerchi 的回复:
int temp=0;
或是定义成static,或放在while循环的外面(之上)
[/Quote]
result = strtok(line, ","); //read the first attribute
假设result现在指向n并将其保存到p1,如果后面还有字符,就再使用strtok寻找",",然后将其保存到p2,normal=strncpy(dst,p1,p2-p1+1);
大概就这个意思
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
好像没有明白我的意思,我现在遇到的问题是result是指向normal中的“n”的指针,但是我希望获得normal而不是n。

[Quote=引用 9 楼 newfarmerchi 的回复:]
int temp=0;
或是定义成static,或放在while循环的外面(之上)
[/Quote]
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
try过了,不对……
[Quote=引用 7 楼 newfarmerchi 的回复:]
C/C++ code

//just try !
while (fgets( line, buff_size, input ) != NULL)
{
int temp=0;
result = strtok(line, ","); //read the first attribute
temp++;
while( result != NULL )
……
[/Quote]
newfarmerchi 2011-03-19
  • 打赏
  • 举报
回复
int temp=0;
或是定义成static,或放在while循环的外面(之上)
newfarmerchi 2011-03-19
  • 打赏
  • 举报
回复

//just try !
while (fgets( line, buff_size, input ) != NULL)
{
int temp=0;
result = strtok(line, ","); //read the first attribute
temp++;
while( result != NULL )
{
if (!strcmp(result,"normal"))
{
printf("%s is in the %d line\n",result, temp);
break;
}
result = strtok(NULL, ",");
}

}

ttssrs 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 delphiwcdj 的回复:]
能否再详细描述下你的需求,举个例子啥的
[/Quote]


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

int main()
{
FILE *input,*output;
char*result = NULL;
int buff_size=200;
char*line = (char*)malloc(buff_size);
int i;
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);
}

if((output=fopen("e:\\b.xls","at+")) == NULL)
{
printf("Cannot open file strike any key exit!");
exit(0);
}

while (fgets( line, buff_size, input ) != NULL)
{
int temp=0;
result = strtok(line, ","); //read the first attribute
for(i=0;i<41;i++){

while( result != NULL )
{
//mapping and scaling the 41 attributs + 1 class respectively
//duration, continuous, [0, 58329]
fputs(result,output);
fputs("\t",output);
break;
} //switch end
result = strtok(NULL, ",");
}

}//while end

fclose(input);
free(line);
return 0;
}



上面是我的代码,把txt文件中的内容写入到excel里,我现在希望可以获得最后一个normal字符串,然后判断它是否在某个集合中。
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 delphiwcdj 的回复:]
能否再详细描述下你的需求,举个例子啥的
[/Quote]


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

int main()
{
FILE *input,*output;
char*result = NULL;
int buff_size=200;
char*line = (char*)malloc(buff_size);
int i;
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);
}

if((output=fopen("e:\\b.xls","at+")) == NULL)
{
printf("Cannot open file strike any key exit!");
exit(0);
}

while (fgets( line, buff_size, input ) != NULL)
{
int temp=0;
result = strtok(line, ","); //read the first attribute
for(i=0;i<41;i++){

while( result != NULL )
{
//mapping and scaling the 41 attributs + 1 class respectively
//duration, continuous, [0, 58329]
fputs(result,output);
fputs("\t",output);
break;
} //switch end
result = strtok(NULL, ",");
}

}//while end

fclose(input);
free(line);
return 0;
}



上面是我的代码,把txt文件中的内容写入到excel里,我现在希望可以获得最后一个normal字符串,然后判断它是否在某个集合中。
witwolf 2011-03-19
  • 打赏
  • 举报
回复
char buffer[BUFSIZE];
每次读一行到buffer中(fgets)
char *p=buffer+strlen(buffer)-//('\n','.'略过)
while(!p--!=',');
p++;
然后做一些处理就行了
delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
能否再详细描述下你的需求,举个例子啥的
ttssrs 2011-03-19
  • 打赏
  • 举报
回复
什么显示不全?从0,tcp,ftp_data……到normal是一行。

[Quote=引用 1 楼 delphiwcdj 的回复:]
显示不全?
[/Quote]
delphiwcdj 2011-03-19
  • 打赏
  • 举报
回复
显示不全?
wuyu1998 2011-03-19
  • 打赏
  • 举报
回复
strrchr() 函数查找字符在指定字符串中从后面开始的第一次出现的位置,如果成功,则返回指向该位置的指针,如果失败,则返回 false。

strrchr(buffer, ',');
找最后一个“,”。
  • 打赏
  • 举报
回复


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

int main()
{
FILE *input,*output;
char*result = NULL;
int buff_size=200;
char*line = (char*)malloc(buff_size);
char* str = "normal.";
input = fopen("kddcup.data_10_percent_corrected","r");
if(input == NULL)
{
printf("\nerror on open e:\\kddcup.data_10_percent_corrected file!");
free(line);
exit(0);
}

if((output=fopen("b.xls","at+")) == NULL)
{
printf("Cannot open file strike any key exit!");
exit(0);
}

while (fgets( line, buff_size, input ) != NULL)
{
printf("%s\n",line);
int temp=0;
int n=0;
result = line;
while(n < 42)
{
result = strtok(result, ",");
if((strcmp(result, str) == 0))
{
printf("this line have %s\n", str);
break;
}
fputs(result,output);
fputs("\t",output);
result = result + strlen(result) + 1;
n++;
}
fputs("\n",output);
result = NULL;
memset(line, 0, 200);
printf("%d\n",n);

}//while end

fclose(input);
free(line);
return 0;
}
加载更多回复(1)

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧