嘗試連接字符串時出現分段錯誤。代碼的目標是創建1000個文件,每個文件的名稱都增加hw7-1.data,hw7-2.data等,直到您到達hw7-999.data。它應該比我做的更微不足道,它的唯一原因就是目標字符串超出範圍,但我只是沒有看到它。我知道我還有一些額外的東西,但除此之外,我不明白爲什麼它會在第一個strcat上進行分割。任何想法爲t使用strcat()的分段錯誤
的輸出是:
這裏
波段故障
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
int main(void)
{
int x;
char* number;
number=malloc(10);
char* filename;
filename=malloc(100);
char* end;
end=malloc(10);
for (x=0; x<1000; x++)
{
filename="./hw7-";
sprintf(number, "%d", x);
printf("%s\n", number);
printf("Here \n");
strcat(filename,"1");
printf("Here \n");
strcat(filename,".data");
printf("Here \n");
int fd2 = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
}
}
'文件名=「./ HW7 - 」'沒有做什麼,你認爲它。 –
''filename =「./ hw7 - 」;' - >'strcpy(filename,「./hw7 - 」);' – BLUEPIXY
''''''''''''''''' – Downvoter