From: fermineutron on
I have a code which inside of a loop opens text files for append, but
it does not allways succeed, the fopen returns NULL on a 2 and up
iterations of loop1, usually what can cause the failur of fopen?

My code schimatic is as such:

while1{
while2{

if(some condition){
open fiile for append using fopen("File.txt","a+")
write data
}
}


read and process File.txt
Delete temp files File.txt
}


all help is apreciated. note, i do fcose in all cases when output to
file stops.
Thanks ahead.

From: Richard Heathfield on
fermineutron said:

> I have a code which inside of a loop opens text files for append, but
> it does not allways succeed, the fopen returns NULL on a 2 and up
> iterations of loop1, usually what can cause the failur of fopen?

A bug in your source code, or the non-existence of a file, or a hardware
error, or a permissions problem, or any of a number of other reasons.

Your "schematic" was not of help in diagnosing your problem.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
From: fermineutron on
Here is the sourcecode, it will probably not work without netflix data,
which is a 600MB download. Its is kind off LONG, so if no one whatnt to
take a crack at it, i'll understand.

=============Begin Paste========================
#include<stdio.h>
#include<dirent.h>
#define MF 100
#define MFTP 100000
#define MAX_USERS 1500000
long NOE;




void compress(char FilePathIn[],char FilePathOut[]);
int main(void){
FILE *ff1,*ff2,*ff3, *ff4, *ff77;
struct dirent *dptr;
char s1[200], FilePath[200],FilePath1[200], uidStr[20]="303948";
char FilePathIn[200],FilePathOut[200];
char *YearText[25]={"_1890.txt","_1895.txt","_1900.txt","_1905.txt",
"_1910.txt","_1915.txt","_1920.txt","_1925.txt",
"_1930.txt","_1935.txt","_1940.txt","_1945.txt",
"_1950.txt","_1955.txt","_1960.txt","_1965.txt",
"_1970.txt","_1975.txt","_1980.txt","_1985.txt",
"_1990.txt","_1995.txt","_2000.txt","_2005.txt",
"_2010.txt"};

long int un, score, uid=303948, us=0, test=0, i=0, i1, movieID, jj,
CMY, UW;
long int FL[25][MF], yearindex, Year1=1890, tmpi,tempcounter;
long int FLi[25]={0}, *TF, TempIndex;
char buff[256];
DIR *dirp;

ff4=fopen("Users2Process.txt","r");
fscanf(ff4,"%d\n%s\n",&uid,uidStr);

while(!feof(ff4)){
i=0;
while(i<25){
i1=0;
while(i1<MF){
FL[i][i1]=0;
i1++;
}
FLi[i]=0;
i++;
}

strcpy(buff,"training_set");
if((dirp=opendir(buff))==NULL){
fprintf(stderr,"Error opening %s ",buff);
perror("dirlist");
exit(1);
}
while(dptr=readdir(dirp)){
printf("\nTEST 1\n");
strcpy(FilePath,"training_set\\");
strncat(FilePath,dptr->d_name,25);
test=0;
if((ff2=fopen(FilePath,"r"))!=NULL){
printf("\n%s\t%d",FilePath,uid);
fscanf(ff2,"%s\n",&movieID);
fscanf(ff2,"%d,%d,%s\n",&un,&score,&s1);
while(!feof(ff2)){
if(un==uid){
test=1;
printf("\n\t\t\t\t\t\tYEY\n");
us=score;
}
fscanf(ff2,"%d,%d,%s\n",&un,&score,&s1);
}
fclose(ff2);
}
if(test==1){
printf("\nTEST 2\n");
if((ff2=fopen(FilePath,"r"))!=NULL){
fscanf(ff2,"%d",&movieID);
/*Get movie year*/
CMY=1975;
if((ff3=fopen("movie_titles.txt","r"))!=NULL){
fscanf(ff3,"%d,%d,",&un,&score);
fgets(s1,150,ff3);
while(!feof(ff3)){
if(un==movieID){
CMY=score;
}
fscanf(ff3,"%d,%d,",&un,&score);
fgets(s1,150,ff3);
}
fclose(ff3);
}

printf("\nTEST 3\n");
fscanf(ff2,"%d,%d",&un,&score);
fgets(s1,150,ff2);
while(!feof(ff2)){
if(us==score){
yearindex=(CMY-Year1)/5;
TempIndex=FLi[yearindex];
FL[yearindex][TempIndex]=un;
FLi[yearindex]++;
if(FLi[yearindex]>(MF-5)){ /*Export friends*/
printf("\nTEST 3.1\n");
tempcounter=0;
strcpy(FilePath1,"friends\\Temp_");
// strncat(FilePath1,uidStr,25);
strncat(FilePath1,YearText[yearindex],25);
tmpi=0;
if((ff77=fopen(FilePath1,"a+"))!=NULL){
printf("\nTEST 3.2\n");
while(tmpi<FLi[yearindex]){
if(FL[yearindex][tmpi]>0){
fprintf(ff77,"%ld\n",FL[yearindex][tmpi]);
FL[yearindex][tmpi]=0;
}
tmpi++;
}
FLi[yearindex]=0;
fclose(ff77);
}
else{
printf("\cant open file: %s\n", FilePath1);
while(tmpi<FLi[yearindex]){
if(FL[yearindex][tmpi]>0){
FL[yearindex][tmpi]=0;
}
tmpi++;
}
FLi[yearindex]=0;
}
} /*if(FLi[yearindex]>(MF-5))*/
}/*if(us==score)*/
fscanf(ff2,"%ld,%ld,",&un,&score);
fgets(s1,150,ff2);
} /*while(!feof(ff2))*/


fclose(ff2);
}/*if((ff2=fopen(FilePath,"r"))!=NULL)*/

printf("\nTEST 5\n");
}/*(test==1)*/
}/*while(dptr=readdir(dirp))*/
closedir(dirp);


/*Compress Current user*/

for(yearindex=0;yearindex<24; yearindex++){
strcpy(FilePathIn,"friends\\Temp_");
// strncat(FilePathIn,uidStr,25);
strncat(FilePathIn,YearText[yearindex],25);
strcpy(FilePathOut,"friends\\");
strncat(FilePathOut,uidStr,25);
strncat(FilePathOut,YearText[yearindex],25);
if((ff2=fopen(FilePathIn,"r"))!=NULL){
fclose(ff2);
printf("\nCompressing %s\n",FilePathIn);
compress(FilePathIn,FilePathOut);
}
}
//}
fscanf(ff4,"%d\n%s",&uid,uidStr);
}/*while(!feof(ff4))*/

return 0;
}








void compress(char FilePathIn[],char FilePathOut[]){
FILE *ff1,*ff2;
long *uid, i, bid=0, un, uidCounter=0, *UF;
printf("\nCompress no error 1\n");
uid=malloc(MFTP*sizeof(long));
if(uid!=NULL){
while(bid<MAX_USERS){
i=0;
printf("\nCompress no error 2\n");
while(i<MFTP){
uid[i]=0;
i++;
}

if((ff1=fopen(FilePathIn,"r"))!=NULL){
ff2=fopen(FilePathOut,"w");
fscanf(ff1,"%ld",&un);
while(!feof(ff1)){
if(un>=bid && un<(bid+MFTP)){
uid[un-bid]++;

}
fscanf(ff1,"%ld",&un);
}

fclose(ff1);
i=0;
while(i<MFTP){
if(uid[i]>0 && ff2!=NULL){
// printf("\nCompress no error 6 i=%ld\n",i);
fprintf(ff2,"%ld,%ld\n",bid+i,uid[i]);
}
i++;
}

}
else{
printf("\nError opening file: %s\n",FilePathIn);
}
bid=bid+MFTP;
From: Richard Heathfield on
fermineutron said:

> Here is the sourcecode,

....which shows a remarkable determination not to check whether file-related
operations succeed, and yet to rely on the success of those operations. If
you want your debugging task to be simpler, I suggest you check the result
of every fopen, every fscanf, every fgets... the lot. That is your fastest
way forward from here.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
From: Keith Thompson on
Richard Heathfield <invalid(a)invalid.invalid> writes:
> fermineutron said:
>
>> Here is the sourcecode,
>
> ...which shows a remarkable determination not to check whether file-related
> operations succeed, and yet to rely on the success of those operations. If
> you want your debugging task to be simpler, I suggest you check the result
> of every fopen, every fscanf, every fgets... the lot. That is your fastest
> way forward from here.

In addition, you're using feof() to control your read loops.

feof() returns true only *after* an attempted read has failed.
Instead, you need to check the results of your reading functions
(fgetc(), or fgets(), or whatever).

If you're reading text data, one good approach is to read a line at a
time with fgets(), then use sscanf() to process the line after you've
read it. The value returned by fgets() will tell you whether you've
reached the end of the file.

The feof() function is only useful after you've reached the end of the
file, and only to tell you whether you've hit the end of the file or
an error.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.