Hi
Can someone point me in the correct direction here
I want to use fgets to read in a stream of data from a text file
I call fgets once and it reads the first line and stores it in my string.
If i call fgets again it does read the next line but it writes over the information in the array stored from the first call of fgets
How can i get it to start storing data in my array where it left off?
Thanks so much
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main( int argc, char *argv[] )
{
FILE *fptr;
int count_line=0;
char temp_array[100];
char *temp_string;
temp_string=&temp_array[0];
int x;
//*****************************************************************************************************************//
for(x=0;x<100;x++){
temp_array[x]='\0';
}
//*****************************************************************************************************************//
if (argc != 2) {
fprintf (stderr, "Error: The program is run with 1 input argument");
exit(-1);
}
// Open file and check it is open
fptr = fopen(argv[1], "r");
if (fptr == NULL) {
fprintf (stderr, "Error: Could not open file");
exit(-1);
}
//*****************************************************************************************************************//
//load the array up with first line of characters
//This is also going to handle the line count
while(!feof(fptr)){
fgets(temp_string,100,fptr);
count_line++;
}
//*****************************************************************************************************************//
for(x=0;x<10;x++){
printf("This is what is stored in array location %d ""%c""\n\n",x,temp_array[x]);
}
fprintf(stderr,"This is the count %d\n\n\n\n",count_line-1);//Gedit in linux is adding an addtional line character. The minus one accounts for that.
//different systems may behave differently.
fclose (fptr);
}