Pointer in a .txt file?
-
Hi The following information is in a .txt file. I nead to read through it, pick out certain parts and send the information to a DB. Memory SM, location 1 Name: John Doe Number: +12345 Memory SM, location 2 Name: Jane Doe Number: +54321 Memory SM, location 3 Name: Jack Doe Number: +112233 ... Here is the code I use to get the info I need from the first 3 lines: /***********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30], bufLine3[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); fgets(bufLine3, 30, stream); fclose( stream ); char *pNext1 = &bufLine1 [21]; char *pNext2 = &bufLine2 [9]; char *pNext3 = &bufLine3 [11]; strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); strcpy(bufLine3, pNext3); //information sent to database /***********************************************/ Is there a way that I can put this in a loop and then start reading the 4th, 5th & 6th line and send their details off to the DB. And the go on to the 7th, 8th & 9th etc? I persume it's something like a pointer in a text file but I'm not sure how to do this. Thanks for any help. Alan
-
Hi The following information is in a .txt file. I nead to read through it, pick out certain parts and send the information to a DB. Memory SM, location 1 Name: John Doe Number: +12345 Memory SM, location 2 Name: Jane Doe Number: +54321 Memory SM, location 3 Name: Jack Doe Number: +112233 ... Here is the code I use to get the info I need from the first 3 lines: /***********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30], bufLine3[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); fgets(bufLine3, 30, stream); fclose( stream ); char *pNext1 = &bufLine1 [21]; char *pNext2 = &bufLine2 [9]; char *pNext3 = &bufLine3 [11]; strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); strcpy(bufLine3, pNext3); //information sent to database /***********************************************/ Is there a way that I can put this in a loop and then start reading the 4th, 5th & 6th line and send their details off to the DB. And the go on to the 7th, 8th & 9th etc? I persume it's something like a pointer in a text file but I'm not sure how to do this. Thanks for any help. Alan
Yes, just don't close the stream and do a while(EOF)! Cheers Al
-
Hi The following information is in a .txt file. I nead to read through it, pick out certain parts and send the information to a DB. Memory SM, location 1 Name: John Doe Number: +12345 Memory SM, location 2 Name: Jane Doe Number: +54321 Memory SM, location 3 Name: Jack Doe Number: +112233 ... Here is the code I use to get the info I need from the first 3 lines: /***********************************************/ FILE *stream; stream = fopen( "number", "r" ); char bufLine1[30], bufLine2[30], bufLine3[30]; fgets(bufLine1, 30, stream); fgets(bufLine2, 30, stream); fgets(bufLine3, 30, stream); fclose( stream ); char *pNext1 = &bufLine1 [21]; char *pNext2 = &bufLine2 [9]; char *pNext3 = &bufLine3 [11]; strcpy(bufLine1, pNext1); strcpy(bufLine2, pNext2); strcpy(bufLine3, pNext3); //information sent to database /***********************************************/ Is there a way that I can put this in a loop and then start reading the 4th, 5th & 6th line and send their details off to the DB. And the go on to the 7th, 8th & 9th etc? I persume it's something like a pointer in a text file but I'm not sure how to do this. Thanks for any help. Alan
1. whether the length of bufLine1 is more than 22, if not, the pointer(pNext1) maybe is out of control 2. you can use substr function