Reading file using fscanf into array
Hi all,
I'm sure that I'm missing something silly, but either I'm not reading the file into the array correctly or I'm not outputting it correctly. I can see my program looping from my printf "Data scanned:" output to the screen, but I don't think I've properly populated my array "hold".
Any suggestions?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 10
#define MAX_LENGTH 10
void *read_files(void *cnt);
int main()
{
int cnt;
pthread_t p_thread[NUM_THREADS];
char file[10] = "file1";
for(cnt = 0; cnt < NUM_THREADS; cnt++)
/* Returns 0 on successful creation of the thread */
if(pthread_create(&p_thread[cnt], NULL, read_files, (void *)cnt) != 0)
{
fprintf(stderr, "Error creating the thread");
}
for(cnt = 0; cnt < NUM_THREADS; cnt++)
{
/* Waits for p_thread[cnt] to finish. */
pthread_join(p_thread[cnt], NULL);
}
fprintf(stdout, "All threads completed.\n");
return 0;
}
void *read_files(void *cnt)
{
size_t c = 0; /* Counter variable */
int d = 1;
int n = 0;
char hold[100];
char temp[10];
char filename[10] = "file";
FILE *datain; /* File to be opened */
snprintf(filename, MAX_LENGTH, "%s%d", filename, cnt);
printf("Filename = %s \n", filename);
/* Attempt to open file */
datain = fopen(filename, "r");
while (!feof(datain))
{
fscanf(datain, "%s", &hold[n]); /* Not working */
printf("Data scanned: %s\n", hold[n]); /* Not outputting hold var */
n++;
}
/* Close the files */
fclose(datain);
}