0
fprintfにセグメンテーションフォルトが発生し続けています。すべてのリサーチで、私のsintaxと初期化はすべて問題ありません。どんな助けでも大歓迎です。 注:printf( "enter i")は、プログラムが失敗した場所とその理由を確認するために使用する行です。整数を印刷するときのfprintfのセグメンテーションフォルト
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <math.h>
#include <time.h>
#include <arpa/inet.h>
#include <stdbool.h>
void itoa(char *buf, int value);//found on stack overflow
int how_many_digits(int n)
{
int i = 0;
while(n > 10){
n = n/10;
i++;
}
return i + 1;
}
int main()
{
srand(time(NULL));
int server_port, main_socket, port;
struct sockaddr_in service;
FILE *fileptr = NULL;
main_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
do{
printf("enter 5\n");
port = rand() % 62974 + 1025;
service.sin_port = htons(port);
if(bind(main_socket, (struct sockaddr*) &service, sizeof(service)) == 0) break;
} while(1);
printf("enter 4\n");
fopen("server_port", "w");
printf("enter 6\n");
printf("%d\n", port);
fprintf(fileptr,"%d", port);
printf("enter 7\n");
fflush(fileptr);
fclose(fileptr);
printf("enter 8\n");
listen(main_socket, 1);
server_port = accept(main_socket, NULL, NULL);
return 0;
}
ラインはあなたのコードのセグメンテーション違反をした上で? – DyZ
while(fprintf(fileptr、 "%d"、port)!= 0); –
この行はもともとはfprintf(fileptr、 "%d"、port)だけだったので、私はちょっと待っていました。私はそれを編集します –