Bellow is the piece of code that causes Segmentation Fault:
(in Linux Segmentation Fault is usually due to illegal memory access)

else if(intData==3)
{
FILE *fp;
int ch;
if((fp = fopen("file.txt","rb"))==NULL) {
printf("Cannot open Source file.\n");
exit(1);
}
while((ch = (int)fgetc( fp )) != EOF) {
printf("%s",(char*)ch);
send(client, (char*)ch, 1,0);
}
fclose(fp);
//send(client, ret, len,0);

}

Can anybody tell me why is it causing Segmentation Fault??

kvprajapati commented: Stop cross posting. -3

Recommended Answers

All 2 Replies

see cross-post in C forum.

I think that the problem appears when you are casting the char to int (if the character is '\n' or ' ' that is impossible, plus your comparing an int with EOF) try using the fscanf function.
fscanf (fp,"%d",ch);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.