Hi,
my code is written in proC and it is in UNIX(AIX).I have written a small code for writing data into a binary file,but while writing my program is giving core dump.
Here Is my code----
fpWriteFile = fopen(WriteFileName,"wb+");
CHAR *recvgen;
recvgen = (char *)malloc(sizeof(char)*NSE_MAX_PACKET_RECV_SIZE);
fwrite (recvgen,NSE_MAX_PACKET_RECV_SIZE,1,fpWriteFile );
Please check whether fpWriteFile and fpWriteFileare valid pointers. Most of the time these are the pain points.
You have not initialized recvgen. I mean, there is no value in that.
PS:- use code tags in your code please.
i am receiving data from socket ...Please find the codes for that...
memset(recvgen,' ',NSE_MAX_PACKET_RECV_SIZE);
recv_bytes = recvfrom(sockfd1,recvgen,NSE_MAX_PACKET_RECV_SIZE,0,(struct sockaddr *)&serv_addr,&clilen)
memset(recvgen,'\0',NSE_MAX_PACKET_RECV_SIZE);
debug it and see what exactly are the values of each variable and exactly from which statement are you getting error. use printf for this.
i did the following code change...
memset(recvgen,'\0',NSE_MAX_PACKET_RECV_SIZE);
still same problem,i debug using dbx ...its showing segmentation fault at fwrite.
also sizeof(recvgen) is 4 where i have defined NSE_MAX_PACKET_RECV_SIZE=1050
I'm guessing the problem is either in WriteFileName or fwriting to a closed stream due to improper verification if fopen() fails.
The following code works under Linux
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define NSE_MAX_PACKET_RECV_SIZE 1050
int
main(int argc, char *argv[])
{
FILE *fpWriteFile;
char *recvgen;
char WriteFileName[1024];
if (argc != 2)
exit(1);
strncpy(WriteFileName, argv[1], 1024);
if (strlen(argv[1]) >= 1024)
WriteFileName[1023] = '\0';
fpWriteFile = fopen(WriteFileName,"wb+");
if (fpWriteFile == NULL)
exit(1);
recvgen = (char *)malloc(sizeof(char)*NSE_MAX_PACKET_RECV_SIZE);
if (recvgen == NULL)
exit(1);
fwrite (recvgen,NSE_MAX_PACKET_RECV_SIZE,1,fpWriteFile);
exit (0);
}
cc -o file file.c
./file `perl -e 'print "A" x 10'` && echo $? -> OK: 0
./file `perl -e 'print "A" x 1024'` ; echo $? -> NOT OK (failing with filename too long): 1
You won't have a segfault now, please try to just debug it with printf()s and find out the problem.
Please post the code that is causing problems.