gpt4 book ai didi

c - 分段故障归档 sleep 功能

转载 作者:行者123 更新时间:2023-11-30 21:16:44 25 4
gpt4 key购买 nike

大家好,我遇到了一个问题。我正在使用 sleep 功能。我有一个文件,5 秒后打开,读取所有数据并显示它。它运行了几次,给出了段错误,我不明白为什么会发生。我认为程序文件没有任何问题,否则它一定热运行了谢谢 请帮助我
这是代码

#include<stdio.h>
#include <sys/stat.h>
#include<time.h>
#include<string.h>
void retrive()
{
FILE *fs;
char ch;
const int size =26;
const int size1 =3;
const int size2 =6;
char sourceip[size];char destip[size];char sourceport[size2];char destport[size2];
char sessionstart[size];char sessionend[size];char payload[size2];char sessionnum[size2];
while(-1)
{
fs = fopen ( "processpacketlib.txt","r" ) ;
if ( fs == NULL )
{
puts ( "Cannot open source file" ) ;
return 0;
}
int j;
int cnt =0;
printf("no of entries %d" , (noofentries("processpacketlib.txt")+1)/3);

//while(cnt<(noofentries("processpacketlib.txt")/3))
for(j=0;j<10;j++)
///ch= fgetc(fs);
//while(ch!=EOF)
{
int i=0;
printf("j = %d" ,j);
ch= fgetc(fs);
sourceip[i]=ch;
i++;
while ( ch!='\t')
{

ch = fgetc ( fs ) ;
sourceip[i]=ch;
i++;
}
sourceip[--i]='\0';
i=0;
//================================================

ch = fgetc ( fs ) ;
destip[i]=ch;
i++;
while ( ch!='\t' )
{

ch = fgetc ( fs ) ;
destip[i]=ch;

i++;
}

destip[--i]='\0';
i=0;
//=================================================

ch = fgetc ( fs ) ;
sourceport[i]=ch;
//printf("eh %c",eh[0]) ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
sourceport[i]=ch;
//printf("eh %c",eh[1]) ;
i++;
}
sourceport[--i]='\0';
i=0;
//=================================================

ch = fgetc ( fs ) ;
destport[i]=ch;
//printf("np %c",np[0]) ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
destport[i]=ch;
//printf("np %c",np[1]) ;
i++;
}
destport[--i]='\0';
i=0;
//===============================================

ch = fgetc ( fs ) ;
payload[i]=ch;
//printf("np %c",np[0]) ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
payload[i]=ch;
//printf("np %c",np[1]) ;
i++;
}
payload[--i]='\0';
i=0;

//==================================================

//==================================================

ch = fgetc ( fs ) ;
sessionnum[i]=ch;
//printf("np %c",np[0]) ;
i++;
while (ch!='\t')
{
ch = fgetc ( fs ) ;
sessionnum[i]=ch;
//printf("np %c",np[1]) ;
i++;
}
sessionnum[--i]='\0';
i=0;


//=================================================
ch = fgetc ( fs ) ;
sessionstart[i]=ch;
//printf("np %c",np[0]) ;
i++;
while (ch!='\n')
{
ch = fgetc ( fs ) ;
sessionstart[i]=ch;
//printf("np %c",np[1]) ;
i++;
}
sessionstart[--i]='\0';
i=0;
//======================================================

ch = fgetc ( fs ) ;
sessionend[i]=ch;
//printf("np %c",np[0]) ;
i++;
while (ch!='\n')
{
ch = fgetc ( fs ) ;
sessionend[i]=ch;
//printf("np %c",np[1]) ;
i++;
}
sessionend[--i]='\0';

ch = fgetc ( fs ) ;
//----------------------------------------------------*/
csvwrite(sourceip,destip,sourceport,destport,sessionstart,sessionend,payload,sessionnum);

printf("\nsip %s" , sourceip);
printf("\n dip %s" , destip);
printf("\nsource %s",sourceport);
printf("\ndest %s",destport);
printf("\nt pay %s",payload);
printf("\nt hdr %s",sessionnum);
printf("\nsession strat %s",sessionstart);
printf("\nsession end %s",sessionend);

printf("\n");

cnt++;
}// while ends
fclose(fs);
sleep(1);
}// outer infinite while ends

}
//==================================================================================

void csvwrite(char sourceip[],char destip[],char sourceport[],char destport[],char sessionstart[],char sessionend[],char payload[],char sessionnum[])
{int retvalue;
int pos=0;
char snum[5];char sstime[20];char payload1[10000] ;
struct tm tm;
time_t t;
char s[25]="Sat Feb 19 12:53:39 2011";
if (strptime(sessionstart, "%A %b %d %H:%M:%S %Y", &tm) != NULL)
tm.tm_isdst = -1;
t = mktime(&tm);
//if (t != -1)
//printf("seconds since the Epoch: %ld\n", (long) t);
char filename[35];
sprintf(filename,"%s%s%s%s",sourceip,"_",destip,".csv");
printf("filename %s",filename);

FILE *fe = fopen(filename,"a");

struct stat st;
stat(filename, &st);
int fsize = st.st_size;
printf("before file size %d\n\n\n" ,fsize);
if(fsize==0)
{
char stringtime[15];
fprintf(fe,"%s",sessionnum);
fprintf(fe,"%s"," ");
sprintf(stringtime,"%ld",(long)t);
fprintf(fe,"%s",stringtime);
fprintf(fe,"%s"," ");
fprintf(fe,"%s",payload);
fprintf(fe,"%s","\n");
fclose(fe);
struct stat st1;
stat(filename, &st1);
int fsize1 = st1.st_size;
printf("after file size %d\n\n\n" ,fsize1);
}
//fclose(filenname);
else
{
int count =noofentries(filename);
printf("count%d",count);
int i=0;
int k=0;
int len =0;

FILE *f1 = fopen(filename,"r");
//char snum[5];char sstime[20];char payload1[100000] ; int payloadentries=0;// change pasted below
while(k<count)
{char snum[5];char sstime[20];char payload1[100000] ; int payloadentries=0;

char ch;
ch= fgetc(f1);
pos++;
len++;
// printf("\nch = %c" ,ch);
snum[i]=ch;
i++;
while (ch!=' ')
{
ch = fgetc ( f1 ) ;
pos++;
len++;
snum[i]=ch;
// printf("ch %c",ch) ;
i++;
}
snum[--i]='\0';
i=0;
//--------------------------------------------
ch= fgetc(f1);
pos++;
len++;
// printf("\nch = %c" ,ch);
sstime[i]=ch;
i++;
while (ch!=' ')
{
ch = fgetc ( f1 ) ;
pos++;
len++;
sstime[i]=ch;
// printf("ch %c",ch) ;
i++;
}
sstime[--i]='\0';
i=0;
//---------------------------------------------
ch= fgetc(f1);
pos++;
len++;
// printf("\nch = %c" ,ch);
payload1[i]=ch;
i++;
while (ch!='\n')
{
ch = fgetc ( f1) ;
pos++;
len++;
payload1[i]=ch;
// printf("ch %c",ch) ;
i++;
if(ch==' ')
{
payloadentries++;
}

}
payload1[--i]='\0';
i=0;
printf("\nsnum %s" ,snum);

printf("\nsstime %s" ,sstime);

printf("\npayload %s" ,payload1);

// retvalue=addpayload(sourceip,destip,sourceport,destport,t,payload,sessionnum,snum,sstime,payload1,payloadentries+1,pos,count,filename);
//printf("rettttttttttvalue %d" ,retvalue);
if ((retvalue==-2)||(retvalue==-1))
{
printf("\n\nBREAK\n\n ");
break;
}
k++;

}//while end
fclose(f1);
} //else end
}// function end
//====================================================================================

最佳答案

csvwrite()if(fsize==0) 条件的 else 分支不 fclose(fe) 。任何一个进程可以同时打开的文件数量是有限制的;如果调用此函数的次数足够多,就会达到限制,并且下一个 fopen() 将返回 NULL (并且将设置 errnoEMFILE - “打开的文件太多”)。

这可能是也可能不是问题,所以这里有一些更一般的建议:

  1. 使用调试器。如果你不知道怎么做,你应该学习!例如:

    $ cat test.c
    #include <stdio.h>

    int main(void)
    {
    int a = 123, b, c;
    int *p = &a;
    int *q = NULL;

    b = *p;
    c = *q;
    printf("%d %d\n", b, c);
    return 0;
    }
    $ gcc -Wall -o test test.c
    $ ./test
    Segmentation fault
    $

    不是很有帮助。但使用 -g 编译并使用 gdb 运行:

    $ gcc -g -Wall -o test test.c$ gdb ./testGNU gdb 6.8-debianCopyright (C) 2008 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "i486-linux-gnu"...(gdb) runStarting program: /tmp/test 
    ...它会显示失败的行:

    Program received signal SIGSEGV, Segmentation fault.0x080483d4 in main () at test.c:1010        c = *q;(gdb) quitThe program is running.  Exit anyway? (y or n) y$ 
  2. 在启用警告的情况下进行编译(正如我上面使用 -Wall 标志所做的那样)。编译器能够发现很多愚蠢的事情,但你必须问它。

  3. 尝试合理地缩进代码。可读的代码更容易调试。

  4. 记住检查返回值。 fopen() 尤其可能因与您的程序无关的多种原因而失败。

  5. 不要编写大量几乎相同的代码。如果您必须多次执行相同的工作,请考虑将其分解为一个单独的函数。

关于c - 分段故障归档 sleep 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5210735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com