i am getting a segmentation fault error in this piece of code..
node.c has some contents.
my intension is to read the contents of a file into an array then..
dynamically allocate memory to a character pointer to store contents of
that file.. i am doing this because the contents of the file may change
for each "read" operation. and i will use this poiter(dynamically
allocated) for sendto() call.
please help..
thnx in advance.
Xoinki.
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- #include <curses.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/socket.h>
- #include <resolv.h>
- #include <netdb.h>
- #include <netinet/in.h>
- struct packet
- {
- char arr1[188];
- };
- struct packet pckt;
- int main()
- {
- FILE *fp;
- char arr[90];
- char *ptr;
- char *ptr2;
- int i=0;
- struct sockaddr_in addr;
- struct hostent *hname;
- struct protoent *proto;
- int val=1,sd;
- int a=0;
- proto = getprotobyname("OSPFIGP");
- hname = gethostbyname("192.168.15.1");
- bzero(&addr, sizeof(addr));
- addr.sin_family = hname->h_addrtype;
- addr.sin_port = 0;
- addr.sin_addr.s_addr = *(long*)hname->h_addr;
- sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
- if ( sd < 0 )
- {
- perror("socket");
- return;
- }
- fp=fopen("node.c","r");
- while(!feof(fp))
- {
- if((arr[a++]=getc(fp))=='\n')
- {
- printf("\\n encountered\n");
- }
- //
- printf("the conetebts are %s\n",arr);
- //a=strcmp(arr,"127.0.0.1");
- }
- ptr2=arr;
- printf("\n\n hi ther\n");
- strcpy(pckt.arr1,arr);
- printf("\n\nnooo\n");
- fclose(fp);
- while(pckt.arr1[i]!='\0')
- {
- printf("%c",pckt.arr1[i]);
- i++;
- }
- printf("\nthe outside of loop are\n %s\nlent
- %d\n",pckt.arr1,strlen(arr));
- ptr=(char *)realloc(ptr,(strlen(arr)+1));
- if(ptr==NULL)
- printf("\nits friggin NULL \n");
- else
- {
- printf("inside else");
- a=0;
- i=0;
- do
- {
- ptr[a++]=arr[i++];
- }while(a!=strlen(arr));
- i=0;
- while(ptr[i]!='\0')
- {
- printf("\nthe last printy are\n %s\n",ptr[i]);
- i++;
- }
- }
- free(ptr);
- return 0;
- }