Ok i have done malloc for the currentContents and the arrays, and also added another array to read the strings from the text file into. I have also made a few other changes to the program.
how ever when i print the array, every value is the same. (human). So it is reading the first value and inserting it into every part of the array.
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <stdbool.h>
void *threadMethod(void *threadNumber)
{
//cout << "Hello from thread: " + (int)threadNumber;
printf("Hello from thread: %d \n", (int)threadNumber);
//_exit(0);
}
int main(int argc, char* argv[])
{
FILE *initBufferFile;
int numOfThreads = atoi(argv[1]);
//int numOfThreads = 3;
int check;
printf("Number of threads = %d \n", numOfThreads);
pthread_t clientArray[numOfThreads];
int i;
//create all client threads.
for(i = 0; i < numOfThreads; i++)
{
printf("Creating thread: %d \n", i);
check = pthread_create(&clientArray[i], NULL, threadMethod, (void *)i);
if(check != 0)
{
printf("Error code for creating thread: %d is %d", i, check);
_exit(-1);
}
}
//join all the threads
for(i = 0; i < numOfThreads; i++)
{
pthread_join(clientArray[i], NULL);
}
//initialise memory buffer
initBufferFile = fopen("init_buffer_pages.dat", "rt");
if(initBufferFile == 0)
{
printf( "Could not open file\n" );
}
int x;
char* emptyArray;
char fileInputBuffer[4100];
i = 0;
int currentNumber;
char* currentContents;
//currentContents = malloc(100);
bool looping = true;
bool readingLine = true;
//while(fgets(fileInputBuffer, 4100, initBufferFile) != NULL)
char* test;
//Find size of memory buffer
int maxPageNumber = 0;
while(looping)
if (fgets(fileInputBuffer, 4100, initBufferFile) == NULL)
{
looping = false;
}
else
{
if(sscanf( fileInputBuffer, "%s %s", &test, ¤tContents) == 2 )
{
currentNumber = (int)test;
currentNumber = currentNumber - 48;
if(currentNumber > maxPageNumber)
{
maxPageNumber = currentNumber;
}
}
}
char* memoryBuffer[maxPageNumber - 1];
printf("array[%d] has been made", maxPageNumber - 1);
//reset the file, and read in initial pages
initBufferFile = fopen("init_buffer_pages.dat", "rt");
looping = true;
memoryBuffer[0] = malloc(100);
memoryBuffer[1] = malloc(100);
memoryBuffer[2] = malloc(100);
memoryBuffer[3] = malloc(100);
memoryBuffer[4] = malloc(100);
char* testArray[5];
int loopNum = 0;
testArray[0] = malloc(100);
testArray[1] = malloc(100);
testArray[2] = malloc(100);
testArray[3] = malloc(100);
testArray[4] = malloc(100);
while(looping)
{
//currentContents = 0;
currentContents = malloc(100);
memoryBuffer[loopNum] = malloc(100);
//currentContents = "";
if (fgets(fileInputBuffer, 4100, initBufferFile) == NULL)
{
looping = false;
}
else
{
//if(sscanf( fileInputBuffer, "%s %s", &test, ¤tContents) == 2 )
if(sscanf( fileInputBuffer, "%s %s", &test, testArray[loopNum]) == 2 )
{
currentNumber = (int)test;
currentNumber = currentNumber - 48;
printf("LOOP(%d) CURRENT#(%d) TESTARRAY(%s)\n",loopNum, currentNumber, testArray[loopNum]);
memoryBuffer[currentNumber] = testArray[loopNum];
// printf("[%d](%s)\n", currentNumber, ¤tContents);
// printf("contnerdS::: %s \n", ¤tContents);
//memoryBuffer[currentNumber] = (char *)malloc(sizeof(char*));
// memoryBuffer[currentNumber] = currentContents;
// printf("TEST: %s ",&memoryBuffer[0]);
}
}
loopNum++;
}
printf("Printing Array\n-------------");
for(i = 0; i < maxPageNumber; i++)
{
printf("[%d] %s\n", i,memoryBuffer[0]);
}
//free(currentContents);
return 0;
}