Reading a file into an array for fast access later
I am fairly new to C and would like to read a file into an array so that I can access the data quickly. I think that I should be using pointers (or an array of pointers) to make effecient code. I get the basic premise of pointers (variables that hold the address of another variable in memory) but have obviously missed something when it comes to working with char (string), pointers and or arrays.
To make this easy for myself I have created a text file to input as shown below:
One
Two
...
...
....
Ten
I am using fgets to get the line and then trying to save the information in the buffer to an address in an array of pointers.
I have included the code for you to ridicule but would appreciate someone letting me know what I am missing.
Thanks in advance
Code:
vuser_init()
{
long file;//used for file
char *filename="c:\\arraytest.txt";//text file containing 10 lines of text (ONE - TEN)
int counter=0;//loop counter
char inputline[100];//line in text file
char *aop[10];//array of pointers (10)
file=fopen(filename,"r");//assuming its there for this test
while(!feof(file)){//while not end of file
fget(inputline,100,file);//get text line
aop[counter]=inputline;//assign the value in inputline to the array - I think it starts to go wrong at this point!
counter++;
}
/*
I think that the problem is that the pointer holds the address to inputline and not the value.
So my output from each element in the array is always the contents of the last line retrieved by the fget.
If I try to use *aop[counter]=inputline I get an illegal types error.
*/
return 0;
}
Quzah - Remember I am new to this....
....so how do I make the space available for the strcpy to work? The lr_eval_string command appears to be working fine but if there is a something that I have still misunderstood then I need to get my head around it. Go on help the newbie...