- #1
VirObitus
- 19
- 0
Im attempting to read a .txt file, and store each line as a separate instance of an array. The code I've written causes each instance of the responses array to become the last line in the .txt file. Do I need to make a 2D array to get what I want? If so, how would I go about doing that?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void)
{
FILE* spResponses;
char* saythis[30];
char* responses[30];
int i = 0;
if ((spResponses = fopen("c:\\responses2.txt", "r")) == NULL) //open file
{
printf("Error opening input file!\n");
}
else
{
while(fgets(saythis, sizeof (saythis), spResponses) != NULL)
{
responses[i] = saythis;
i++;
}
fclose(spResponses);
}
printf("%s\n\n", responses[0]);
printf("%s\n\n", responses[1]);
printf("%s\n\n", responses[2]);
printf("%s\n\n", responses[3]);
printf("%s\n\n", responses[4]);
system("PAUSE");
return 0;
}