-1

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??

Edited by Nick Evan: Fixed formatting

Votes + Comments
Stop cross-posting!
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
1

>>printf("%s",(char*)ch);

I've told you about that before in another thread. %s says the next argument is a character array, but all you are passing is a single character. What you should use is "%c", then remove that damned typecast.

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.