/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #define ROOMS_FILE = "rooms.txt" #include char *nameRoom(int i) { char *bufferA, *bufferB, character, *roomDescription[MAX_ROOMS]; int bufferSize = DEFAULT_BUFFER_SIZE, roomNumber = 0, bufferStrlen; FILE *rooms; rooms = fopen(ROOMS_FILE, "r"); bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1); do { if (strlen(bufferA) >= (bufferSize - 1)) { bufferB = bufferA; bufferSize *= 2; bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1); strcpy(bufferA,bufferB); free(bufferB); } if (((character = fgetc(rooms)) != EOF) && (character != DELIMETER)) { bufferStrlen = strlen(bufferA); bufferA[bufferStrlen] = character; bufferA[bufferStrlen + 1] = ' '; } else { roomDescription[roomNumber] = malloc(sizeof(bufferA + 1) * sizeof(char)); roomDescription[roomNumber] = bufferA; bufferSize = DEFAULT_BUFFER_SIZE; bufferA = memset(malloc(bufferSize * sizeof(char)),' ',1); roomNumber++; if (roomNumber >= MAX_ROOMS) return roomDescription[i]; } } while (!ferror(rooms)); return roomDescription[i]; } int main(int argc, char **argv) { //test case char *filename = "rooms.txt"; char **room_descriptions; int room_count,i; room_descriptions = malloc(MAX_ROOMS * sizeof(void)); //room_count = read_room_descriptions(filename,room_descriptions); //for (i = 0;i < room_count;i++) { // printf("%sn",room_descriptions[i]); // free(room_descriptions[i]); //} for (i=0;i < 5;i++) { room_descriptions[i] = nameRoom(i); printf("%sn",room_descriptions[i]); } return 0; }