Hi,
I am sorry to labour this point, but I cannot understand why I cannot get a count of lines in a text file
Code:
void check_activity(FILE ** p_fp)
{
int diff = 0;
int result = 0;
int countA = 0, countB = 0;
int ch;
printf("before this month, last month, difference percentage %d %d %d %d\n", countA, countB, diff, result);
if(!(p_fp[OUT_FILE]))
printf("OUT_FILE is not open !!");
/*
rewind(p_fp[OUT_FILE]);
*/
fseek(p_fp[OUT_FILE], 0, SEEK_SET);
while((ch = fgetc(p_fp[OUT_FILE])) != EOF)
if(ch == '\n')
countA++;
printf("first count of OUT_FILE [%d]\n", countA);
while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
if(ch == '\n')
countB++;
/* establish which is the largest file */
if(countA > countB)
{
diff = countA - countB;
}
else
{
diff = countB - countA;
}
/* get the percentage to test */
result = countA / 10;
if(diff > result)
cirp_process(p_fp);
printf("after this month, last month, difference percentage %d %d %d %d\n", countA, countB, diff, result);
}
/* test run */
$ cirp_process september.SND august.SND
before this month, last month, difference percentage 0 0 0 0
first count of OUT_FILE [0]
YES
after this month, last month, difference percentage 0 10 10 0
$
I have swapped the order of the files as arguments, I have swapped the order of calling the parameter definitions, I have swapped the cut-n-paste while routines round the other way, I have tried rewind and fseek and I have tested to see that the file really is open.
I have also made a small function with one routine and sent the two files to it just to see, and still get the same result.
Code:
int get_count(FILE ** fp, int text_file)
{
int ch, count = 0;
while((ch = fgetc(fp[text_file])) != EOF)
if(ch == '\n')
count++;
return count;
}
/* test run */
$ cirp_process september.SND august.SND
before this month, last month, difference percentage 0 0 0 0
count of OUT_FILE [0]
count of LAST_FILE [10]
after this month, last month, difference percentage 0 10 0 0
$
However, what I do not know is this...
If I start a process and open some files, say two files, one already exists and the other doesn't, and I read the one that does, and write to the one that doesn't, do I have to close that new file and then re-open it before I can work on it ?
If that is the case, it is going to make the program a bit messy, as at present all files are opened at the start and closed at the end, and I would have to do a separate process just for this file.
tia,