2016-10-24 16 views
0

現在、ランダムにファイルのバンドルを生成し、それらを互いに接続し、ゲームのような迷路を作成するプログラムを実行しています。 関数に渡すためのファイルパスの配列を作成しようとしていますので、配列を生成することができます。起こっているのは、配列を生成していますが、最初の要素(filepath [0])を空白のままにしておくことです。私のせいにする。しかし、ブレークポイントを設定すると、配列の他のセクションはすべて正常です。最初の要素ではありません。私が書いたので、それは約9ヶ月をされているとCと私は、高度な変更する関数に文字列配列を渡す

であなたのすべてに感謝し、私のポインタしゃっくりがどこから来ているか不明だ。ここのコードは、これまでの配列が渡されている

#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <string.h> 
#include <unistd.h> 
#include <time.h> 
#include <stdbool.h> 

void create_files(char (*filepath[7])); 

int main(){ 
    time_t t; 
    char *filepath[7]; 
    srand((unsigned) time(&t)); 
    int i = 0; 
    for (i = 0; i < 7; i++) 
     filepath[i] = malloc(60); 


    create_files(filepath); 
    for (i = 0; i < 7; i++) 
     free(filepath[i]); 
    return 0; 
} 

void create_files(char (*filepath[7])){ 
    int i = 0, pid = getpid(),q = 0,random, r=7; 
    char procid[20]; 
    sprintf(procid, "%d", pid); 
    char directory[80] = "./dringb.rooms."; 

    strcat(directory,procid); 
    int newdir = mkdir(directory, 0777); 

    for (q = 0; q < 7; q++) 
     filepath[q] = directory; 

    char *bigrooms[10] ={"/Bridge.txt","/Gate.txt","/Hallway.txt", 
     "/Dungeon.txt","/Galley.txt","/Throne.txt","/Boss.txt", "/Lab.txt", 
     "/Torture.txt", "/Courtyard.txt"}; 
    bool redflag = false; 
    char *rooms[7]; 
    q = 0; 

    while (q != 7){ //grabs the rooms at random from the set of bigrooms 
     random = rand()%10; 
     for(i = 0; i < 7; i++){ 
      if (rooms[i] == bigrooms[random]) 
       redflag = true; 
     } 
     if (redflag == false){ 
      rooms[q] = bigrooms[random]; 
      redflag = false; 
      q++; 
     } 
     else 
      redflag = false; 
    } 

    char **dest = (char **)malloc(r * sizeof(char *)); 
    for (i=0; i<r; i++) 
     dest[i] = (char *)malloc(8 * sizeof(rooms)); //allocates each room a new space 

    for (i = 0; i < 7; i++){ 
     strcat(dest[i], directory); 
     strcat(dest[i],rooms[i]); 
     filepath[i] = dest[i]; //creates directory path for each room.txt 
    } 


    int usedrooms[4]; 
    for (i = 0; i < 7; i++){ 
     FILE *f = fopen(filepath[i], "w"); 
     fputs("Roomname: ", f); 
     fputs(rooms[i],f); 
     fputs("\n",f); 
     fclose(f); 
    } 

    for (i = 0; i < 7; i++){ 
     FILE *f = fopen(filepath[i], "a+"); 
     for (q = 0; q < 4; q++) 
      usedrooms[q] = 100; 
     int roomrand, q = 0, z = 0, connrooms = 3; 
     bool greenflag = true, retry = false; 

     roomrand = rand() %2; 
     if (roomrand == 1) 
      connrooms = 4; 

     while (q != connrooms){ //prevents from having a connection to same room 
      do{ 
       retry = false; 
       roomrand = rand() % 7; 
       for(z = 0; z < 4; z++){ 
        if (roomrand == usedrooms[z]) 
         retry = true; 
       } 
      }while(roomrand == i || retry == true); //prevents from having a connection to same room 
      bool found = false; 
      char buffer[100]; 
      rewind(f); 
      while(fscanf(f,"%s", buffer) == 1){ 
       if (strcmp(buffer,rooms[roomrand]) == 0)//prevents a double connecting room from being added 
        greenflag = false; 
      } 
      if(greenflag == true){ 
       usedrooms[q] = roomrand; 
       fputs("Connecting Room: ", f); 
       fputs(rooms[roomrand],f); 
       fputs("\n",f); 
      } 

      fclose(f); 
      greenflag = true; 
      found = false; 
      FILE *f2 = fopen(filepath[roomrand],"a+"); 
      rewind(f2); 
      while(fscanf(f2,"%s", buffer) == 1){ 
       if (strcmp(buffer,rooms[i]) == 0) //prevents a double connecting room from being added 
        found = true; 
      } 
      if (found == false){ 
       fputs("Connecting Room: ",f2); 
       fputs(rooms[i],f2); 
       fputs("\n",f2); 
      } 
      fclose(f2); 
      fopen(filepath[i],"a+"); 
      found = false; 
      q++; 
     } 
     q = 0; 
     fclose(f); 
    } 

    int usedroomtype[7]; 
    int roomrand; 
    for (i = 0; i < 7; i++) 
     usedroomtype[i] = 100; 

    for (i = 0; i < 7;i++){ 
     do{ 
      redflag = false; 
      roomrand = rand() % 7; 
      for (q = 0; q < 7; q++) 
       if (roomrand == usedroomtype[q]) 
        redflag = true; 
     } while (redflag == true); 
     usedroomtype[i] = roomrand; 
     FILE *fp = fopen(filepath[roomrand], "a+"); 
     if (i == 0) 
      fputs("Room Type: Start Room", fp); 
     else if (i == 6) 
      fputs("Room Type: End Room",fp); 
     else 
      fputs ("Room Type: Mid Room",fp); 
     fclose(fp); 
    } 

} 
+1

'free(filepath);'が間違っています。 'filepath'は自動ストレージに置かれています。あなたは決して' malloc'することはありません。 – yano

+1

メモリを 'malloc'すると*初期化されません。したがって、メモリを最初に初期化しない限り、 'strcat'は動作しません。 'strcat(dest [i]、directory)'行は 'strcpy(dest [i]、directory)'でなければなりません。 – user3386109

+0

'dest [i] =(char *)malloc(8 * sizeof(rooms));行は私にとっては右のようではありません。 $ 80は 'directory'変数のために選んだサイズなので、' dest [i] = malloc(80); 'のように思えます。 – user3386109

答えて

0

です正しく問題は、データが破損していることです。

for (q = 0; q < 7; q++) 
    filepath[q] = directory; 

これは無効です。 strcpy(filepath[q], directory);tempが割り当てられていないので、たとえばchar *temp = filepath[q]に設定しても問題ありません。ただしfilepath[q]は既に割り当てられています。値を変更するには、strcpyを使用します。

その後、同様のエラーが

char **dest = (char **)malloc(r * sizeof(char *)); 
for (i=0; i<r; i++) 
    dest[i] = (char *)malloc(8 * sizeof(rooms)); 

for (i = 0; i < 7; i++){ 
    strcat(dest[i], directory); 
    strcat(dest[i],rooms[i]); 
    filepath[i] = dest[i]; //creates directory path for each room.txt 
} 

2つのことにあります。まず、destは初期化されていません。初期化されていない文字列で常にstrcpyで始まり、strcatを使用してください。次に、strcpyを使用して、前述のようにfilepath[i]の値を変更します。 destは実際には必要ありません。コメントで述べたように、あなただけのfilepath

for (i = 0; i < 7; i++) 
{ 
    strcpy(filepath[i], directory); 
    strcat(filepath[i], rooms[i]); 
} 

に直接コピーすることができ、filepathの割り当ては大きくする必要があります。 directoryは最大80バイトです。roomは最大10バイトなので、filepathは最大90バイトです。他の場所でもいくつかの値が初期化されません

for (i = 0; i < 7; i++) 
    filepath[i] = malloc(90); 

、たとえば char *rooms[7];

sprintf(directory, "./dringb.rooms.%d", getpid()); 

例:あなたはすでにsprintfを使用している

int pid = getpid(); 
char procid[20]; 
sprintf(procid, "%d", pid); 
char directory[80] = "./dringb.rooms."; 
strcat(directory,procid); 

次のように、あなたがこれを簡素化することができます:

int main() 
{ 
    time_t t; 
    srand((unsigned) time(&t)); 

    char *filepath[7]; 
    int i = 0; 
    for (i = 0; i < 7; i++) 
     filepath[i] = malloc(90); 

    create_files(filepath); 

    for (i = 0; i < 7; i++) 
     printf("%s\n", filepath[i]); 

    for (i = 0; i < 7; i++) 
     free(filepath[i]); 

    return 0; 
} 
void create_files(char *filepath[7]) 
{ 
    int i = 0, random; 
    char directory[80]; 
    sprintf(directory, "./dringb.rooms.%d", getpid()); 
    //mkdir(directory); 
    mkdir(directory, 0777); 

    int q; 
    for (q = 0; q < 7; q++) strcpy(filepath[q], directory); 

    char *bigrooms[20] ={"/Bridge.txt","/Gate.txt","/Hallway.txt","/Dungeon.txt","/Galley.txt","/Throne.txt","/Boss.txt", "/Lab.txt","/Torture.txt", "/Courtyard.txt"}; 
    bool redflag = false; 
    char *rooms[7]; 
    for (i = 0; i < 7; i++) rooms[i] = 0; 

    q = 0; 
    while (q != 7) 
    { 
     //grabs the rooms at random from the set of bigrooms 
     random = rand()%10; 
     for(i = 0; i < 7; i++) 
     { 
      if (rooms[i] == bigrooms[random]) 
       redflag = true; 
     } 

     if (redflag == false) 
     { 
      rooms[q] = bigrooms[random]; 
      redflag = false; 
      q++; 
     } 
     else 
      redflag = false; 
    } 

    for (i = 0; i < 7; i++) 
    { 
     strcpy(filepath[i], directory); 
     strcat(filepath[i], rooms[i]); 
    } 

    int usedrooms[4]; 
    for (i = 0; i < 7; i++) 
    { 
     FILE *f = fopen(filepath[i], "w"); 
     fputs("Roomname: ", f); 
     fputs(rooms[i],f); 
     fputs("\n",f); 
     fclose(f); 
    } 

    for (i = 0; i < 7; i++) 
    { 
     FILE *f = fopen(filepath[i], "a+"); 
     for (q = 0; q < 4; q++) 
      usedrooms[q] = 100; 
     int roomrand, q = 0, z = 0, connrooms = 3; 
     bool greenflag = true, retry = false; 

     roomrand = rand() %2; 
     if (roomrand == 1) 
      connrooms = 4; 

     while (q != connrooms) 
     { //prevents from having a connection to same room 
      do 
      { 
       retry = false; 
       roomrand = rand() % 7; 
       for(z = 0; z < 4; z++) 
       { 
        if (roomrand == usedrooms[z]) 
         retry = true; 
       } 
      } 
      while(roomrand == i || retry == true); //prevents from having a connection to same room 

      bool found = false; 
      char buffer[100]; 
      rewind(f); 
      while(fscanf(f,"%s", buffer) == 1) 
      { 
       if (strcmp(buffer,rooms[roomrand]) == 0)//prevents a double connecting room from being added 
        greenflag = false; 
      } 

      if(greenflag == true) 
      { 
       usedrooms[q] = roomrand; 
       fputs("Connecting Room: ", f); 
       fputs(rooms[roomrand],f); 
       fputs("\n",f); 
      } 

      fclose(f); 
      greenflag = true; 
      found = false; 
      FILE *f2 = fopen(filepath[roomrand],"a+"); 
      rewind(f2); 

      while(fscanf(f2,"%s", buffer) == 1) 
      { 
       if (strcmp(buffer,rooms[i]) == 0) //prevents a double connecting room from being added 
        found = true; 
      } 

      if (found == false) 
      { 
       fputs("Connecting Room: ",f2); 
       fputs(rooms[i],f2); 
       fputs("\n",f2); 
      } 

      fclose(f2); 
      fopen(filepath[i],"a+"); 
      found = false; 
      q++; 
     } 
     q = 0; 
     fclose(f); 
    } 

    int usedroomtype[7]; 
    int roomrand; 
    for (i = 0; i < 7; i++) 
     usedroomtype[i] = 100; 

    for (i = 0; i < 7; i++) 
    { 
     do 
     { 
      redflag = false; 
      roomrand = rand() % 7; 
      for (q = 0; q < 7; q++) 
       if (roomrand == usedroomtype[q]) 
        redflag = true; 
     } 
     while (redflag == true); 

     usedroomtype[i] = roomrand; 
     FILE *fp = fopen(filepath[roomrand], "a+"); 
     if (i == 0) 
      fputs("Room Type: Start Room", fp); 
     else if (i == 6) 
      fputs("Room Type: End Room",fp); 
     else 
      fputs ("Room Type: Mid Room",fp); 
     fclose(fp); 
    } 
} 
+0

すごくありがとう、Cの文字列はC++やPythonからの痛みです – Dringo

関連する問題