In the following code; in the loop the value of ncrC-> count varies because the pointer does not restart when using free (s) and does not locate as it is due to loop the string: "Counter=". Please help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXUSE 1000
#define TAGSIZE 4
#define MAXBUF 100
struct ncrcode /* Structure of ncrcode */
{
int count;
};
int main()
{
int q = 1, i = 0, x=0, a=0;
char ch;
FILE * fp;
struct ncrcode *ncrC;
ncrC = (struct ncrcode *) malloc(sizeof(*ncrC)); /* gets room for the new node */
while (q)
{
/* This .txt file contains a file reference to another file */
fp = fopen("nd0_1.txt", "r");
char *s = (char *)malloc(sizeof(char)*20);
/* 1 - Get COUNTER getnodecounter() */
while( (ch = fgetc(fp)) != EOF)
{
s[i++] = ch;
if (strcmp(s,"Counter = ") == 0)
{
printf("\nPLAKAPLAKA%d", a++);
break;
}
}
i = 0;
printf("\n First\n%d\n",ncrC->count = 0);
while( (ch = fgetc(fp)) != '\n')
{
printf("%c\n", ch);
ncrC->count = (ch - 48) + ( ncrC->count * 10);
}
printf("\n STRINNNN \n%d\n",ncrC->count);
free(s);
fclose(fp);
}
return (0);