I'm reading and writing (append mode) into a file, and something very strange happens. The first time I run it, it reads the file perfectly, and appends the new line perfectly. The second time, it gets stuck reading the file.
What am I doing wrong?
The reading:
fptr = fopen("log.txt", "r");
fseek(fptr, -70, SEEK_END);
for(i = 0; i < 11; i++)
{
fscanf(fptr, "%d, ", llast + i);
printf("%d ", llast[i]);
}
/* read the last review */
for(i = 0; i < 11; i++)
{
fscanf(fptr, "%d, ", last + i);
printf("%d ", last[i]);
}
fclose(fptr);
The appending:
fptr = fopen("log.txt", "a");
for(i = 0; i < 11; i++)
{
fprintf(fptr, "%d, ", *(new + i));
}
fprintf(fptr, "\n");
fclose(fptr);
The log.txt
file last two lines contain:
7, 4, 0, 10, 2, 8, 9, 5, 6, 3, 1,
3, 7, 5, 6, 9, 2, 4, 10, 0, 1, 8,
The Full Code:
#include <stdio.h>
#include <stdlib.h> /* for the rand function */
int main()
{
int last[11] = {0};
int llast[11] = {0};
int new1[11] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int i = 0, j = 0;
int rev = 0;
int flag = 0;
int big_flag = 0;
FILE *fptr;
printf("\nThe last review:\n");
PrintRev();
printf("\n\n");
/* open the log file of the reviews, and load the last two into arrays */
fptr = fopen("log.txt", "r");
/* read the review before the last review*/
fseek(fptr, -70, SEEK_END);
for(i = 0; i < 11; i++)
{
fscanf(fptr, "%d, ", llast + i);
printf("%d ", llast[i]);
}
/* read the last review */
for(i = 0; i < 11; i++)
{
fscanf(fptr, "%d, ", last + i);
printf("%d ", last[i]);
}
fclose(fptr);
/* create a new arrangement of reviewing */
/* the big flag is used for the extreme case when the first 10 reviewers are assigned
but a problem occurs in the last remaining option for the last reviewer -
so instead of running into a dead end of endless-loop, if there is a problem,
we start over again ... */
while(!big_flag)
{
for(i = 0; i < 10; i++)
{
flag = 0;
/* the small flag is used to make sure the first 10 reviewers follow the rules */
while(!flag)
{
rev = (rand() % 11);
if((rev != i) && /* you can't review yourself */
(rev != last[i]) && /* you can't review the person you did last time */
(rev != llast[i]) && /* let's try without the last-last time */
(rev != (i - 1)) && (rev != (i + 1)) && /* you can't review your neighbour */
(new1[rev] != i)) /* you can't review the person reviewing you */
{
flag = 1;
/* a reviewer can't review more than one person... so we check if he wasn't
chosen before */
for(j = 0; j < i; j++)
{
if(rev == new1[j])
{
flag = 0;
}
}
}
}
new1[i] = rev;
}
/* find the only remaining id of reviewer */
rev = 55; /* sum of 0,1, ... 10 */
for(i = 0; i < 10; i++)
{
rev -= new1[i];
}
/* check if the remaining id is valid */
if((rev != i) &&
(rev != last[i]) &&
(rev != llast[i]) && /* let's try without the last-last time */
(rev != (i - 1)) &&
(new1[rev] != i))
/* if so, appoint it, and raise the big flag to exit the loop */
{
new1[10] = rev;
big_flag = 1;
}
}
/* append the new arrangement to file */
fptr = fopen("log.txt", "a");
for(i = 0; i < 11; i++)
{
fprintf(fptr, "%d, ", *(new1 + i));
}
fclose(fptr);
return 0;
}