Hello all.
I am trying to learn C and have a PHP background.
What I am looking to do (to learn C some more) is to read a file line by line and return what items in the list are missing.
Example lines are:
Store 1
Store 2
Store 3
Store 12
Store 13
Store 19
Store 20
I would like to have the application return a list such as
Store 4
Store 5
Store 6
...
Store 14
Store 15
...
Here is what I have thus far and it currently thinks "next" in "*getMissingStores" is 123456789. This program was looking a lot better as far as structure is concerned, but I have been just trying to get it to work so it may look a bit jumbled.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STORE_FILE "c:/stores.txt"
char *strip_newline(char *szString){
if(szString[strlen(szString)-1] == '\n')
szString[strlen(szString)-1] = '\0';
return szString;
}
int getDigits(char *szString){
char *tmp = strchr(szString, ' ');
char *tmp2;
if(tmp == NULL) return -1;
tmp++;
if(tmp != NULL){
tmp2 = tmp;
tmp2[strlen(tmp2)+1] = '\0';
return atoi(tmp2);
} else {
return -1;
}
}
char *getMissingStores(char *szStoreNums){
int next = getDigits(szStoreNums + 1);
int curr = getDigits(szStoreNums);
do {
while(curr != next && curr < next){
printf("Store %d is missing.\n", ++curr);
printf("Curr: %d -> Next: %d", curr, next);
}
next = atoi(++szStoreNums);
} while(szStoreNums != NULL);
return;
}
int main(int argc, char **argv){
FILE *storeFile = fopen(STORE_FILE, "r");
/** create space for the store names **/
char storeList[200][51];
if(storeFile == 0){
printf("Could not open file %s for reading.\n", STORE_FILE);
system("pause");
return -1;
} else {
/** begin to read the file **/
int i = 0;
while(fgets(storeList[i], 1024, storeFile) != 0){
strip_newline(storeList[i]);
printf("Read in: %s\n", storeList[i]);
printf("Digits are: %d\n\n", getDigits(storeList[i]));
i++;
}
char *szMissingStores = getMissingStores((char *)storeList);
printf("Total stores on file: %d\n", i);
}
system("pause");
return 0;
}
Any pointers would be appreciated and I am not against recoding the entire thing.