wn compile a code(400 lines app) after running 3 times through the loop(loop in the sense assume the 200 lines) it says unable to open a file...(which it had opened d previous 3 times and executed wel)

so i tried perror() and found "not enough memory"

Please suggest me wt to do...

thanks in advance.

Check whether you are closing the file after opening it.

In most cases you will need to open the file only once. (depends on your requirements)

Can you copy your code here?

here's my code....

#include<stdio.h>
#include<stdlib.h>       //included to use random function
#include<conio.h>
#include"crc.c"
#define URL_LENGTH 200
#define MAX_NO_OF_DIGITS 5

/******
struct node :: the structure of nodes in d list tat maintains(web sites)
******/

struct node
{
int data;
char *url;
struct node *link;
struct out_links *list_of_outgoinglinks;
}*root,*curr_node,*next_node;


FILE *fp,*fpt,*dt,*chk;
char *temp,*str;

/******
struct out_links :: the structure of nodes in d list tat maintains all out-going links from a site
******/

struct out_links
{
	struct node *points_to;
	struct out_links *next_node;
}*outlink;

/**********
function prototype declaration
**********/

struct node *create_node(char *);
int graph_gen();
struct node *find_node_addr(char *);
struct out_links *create_outlink_node(char *);
int check(int,int,int *,int *);
void outlink_gen(struct node *);
char *find_filenm(char *);
/**************
fun 	: create a new node
param   : nil
return	: address of new_node
***************/

struct node *create_node(char *temp1)
{
	struct node *new_node;
	new_node=(struct node *)malloc(sizeof(struct node));    //alloc memory
	new_node->data=crc();
	new_node->url=(char *)malloc(URL_LENGTH);
	strcpy(new_node->url,temp1);
	new_node->link=NULL;
	new_node->list_of_outgoinglinks=NULL;
	return new_node;
}
/**************
fun 	: generates d graph
param   : total no of nodes
return	: NIL
***************/

int graph_gen()
{
	int graph_size=0;
	FILE *fp;
	char *temp;
	temp=(char *)malloc(URL_LENGTH);
	fp=fopen("url.txt","r");
	fscanf(fp,"%s",temp);		//written twice because we have 2 read the 2nd word
	fscanf(fp,"%s",temp);
	root=create_node(temp);
	curr_node=root;
	while(fscanf(fp,"%s",temp)!=EOF)		//list generation
	{
		fscanf(fp,"%s",temp);
		next_node=create_node(temp);
		curr_node->link=next_node;
		curr_node=next_node;
	}                              //while end
//	free(temp);
/********
traversing the list
********/

	curr_node=root;
	printf("%d--> ",curr_node->data);
	printf(" %s\n",curr_node->url);
	graph_size++;
	do
	{
		curr_node=curr_node->link;
		printf("%d--> ",curr_node->data);
		printf(" %s\n",curr_node->url);
		graph_size++;
	}while(curr_node->link!=NULL);		//do-while end
	fclose(fp);
	return(graph_size);
}
			//outlink generation//
/**************
fun 	: finds the node to be pointed
param   : var-data node to be pointed
return	: req nodes address
***************/

struct node *find_node_addr(char *var)
{
	struct node *present_node;
	present_node=root;
	do
	{
		if(strcmp(present_node->url,var)==0)
		{
		break;
		}
		present_node=present_node->link;
	}while(present_node->link!=NULL);
	return present_node;
}

/**************
fun 	: create a new node
param   : nil
return	: address of new_node
***************/

struct out_links *create_outlink_node(char *temp_node_content)
{
	struct out_links *temp;
	temp=(struct out_links *)malloc(sizeof(struct out_links));
	temp->points_to=find_node_addr(temp_node_content);
	printf("(%d,%s)\n",temp->points_to->data,temp_node_content);
	temp->next_node=NULL;
	return temp;

}

/**************
fun 	: generates d outlinks for node passed
param   : total no of nodes,node for which outlinks needed to be generated
return	: NIL
***************/

void outlink_gen(struct node *temp_curr_node)
{
	struct out_links *outlink_header,*curr_outlink_node,*next_outlink_node;
	int i,no_of_outlinks,node_content,temp_var1,*outlinks_array,*temp_array;	//node_content : has data of the node to be pointed ; outlinks_array : has values of nodes to be pointed & temp_array is used for manipulation of outlinks_array
	char *out_url,*out_file;
	FILE *out_fp;
	out_url=(char *)malloc(URL_LENGTH);
	out_file=find_filenm(temp_curr_node->url);

	//////////////////////////////
	if((out_fp=fopen(out_file,"r"))==NULL)
	{
		printf("Cannot open file %s in full graph\n",out_file);
		exit(1);
	}
	if(!feof(out_fp))
	{
		fscanf(out_fp,"%s",out_url);
		outlink_header=create_outlink_node(out_url);
		curr_outlink_node=outlink_header;
	}
	else
	  outlink_header=NULL;  //the node has no outlink, outlink is assinged to NULL
	while(!feof(out_fp))	//gen link list of outlinks
	{
		fscanf(out_fp,"%s",out_url);
		next_outlink_node=create_outlink_node(out_url);
		curr_outlink_node->next_node=next_outlink_node;
		curr_outlink_node=next_outlink_node;
	}
	temp_curr_node->list_of_outgoinglinks=outlink_header;
}

char* outlinks(char *file)
{
	char *str1,*fil_num,ext[5]=".txt\0",*file_nm;
	int i=0,flag;
	static int file_no=0;
	str1=(char *)malloc(URL_LENGTH);
	temp=(char *)malloc(URL_LENGTH);
	file_nm=(char *)malloc(7+MAX_NO_OF_DIGITS);
	fil_num=(char *)malloc(MAX_NO_OF_DIGITS);
	file_no++;
	strcpy(file_nm,"out");
	itoa(file_no,fil_num,10);
	strcat(file_nm,fil_num);
	strcat(file_nm,ext);
	printf("%s",file_nm);
	if ((fpt = fopen(file_nm,"w"))==NULL)		//if there exists a file of tis name it clears its data
	{
		printf("Cannot open file %s in outlinks-1\n",file_nm);
		exit(1);
	}
	fclose(fpt);

	if ((fp = fopen(file,"r"))==NULL)
	{
		printf("Cannot open file %s in outlinks-2\n",file);
		exit(1);
	}
	while(fscanf(fp, "%s", str1)!=EOF)
	{
		if((temp=strstr(str1,"href="))!=NULL)
		{
			while(str1[i+6]!='"')
			{
				temp[i]=str1[i+6];
				i++;
			}
			temp[i]=NULL;
			flag=is_in_dataset(temp);
			if(flag==0)	//0-not in dataset & 1-is in dataset
			{
				if(is_repeating(file_nm,temp))	//1-not a repeatin url & 0-repeating url
				{
					if((fpt=fopen(file_nm,"a"))==NULL)
					{
						printf("Cannot open file %s in outlinks-3\n",file_nm);
						exit(1);
					}
					fprintf(fpt,"%s\n",temp);
					fclose(fpt);
					printf("%s\n",temp);
				}
			}
			i=0;
		}

	}
	fclose(fp);

//	free(str1);
//	free(temp);
//	free(fil_num);
//	free(&i);
//	free(&flag);
//	fclose(fpt);
	return(file_nm);
}
int is_in_dataset(char *temp_url)
{
	int flag2=1;
	str=(char *)malloc(URL_LENGTH);
	if((dt=fopen("urll.txt","r"))==NULL)
	{
		perror("");
		printf("Cannot open file url.txt in outlinks-4\n");
		exit(1);
	}
	while(flag2)
	{
		if(fscanf(dt,"%s",str)!=EOF)
		{
			if(strcmp(str,temp_url)==0)
				flag2=0;
		}
		else
			break;
	}
	fclose(dt);
	return(flag2);
}

int is_repeating(char *fil,char *temp_url)
{
	int flag1=1;
	str=(char *)malloc(URL_LENGTH);
	if((chk=fopen(fil,"r"))==NULL)
	{
		perror("perror : ");
		printf("Cannot open file %s in outlinks-5\n",fil);
		exit(1);
	}
	while(flag1)
	{
		if(fscanf(chk,"%s",str)!=EOF)
		{
			if(strcmp(str,temp_url)==0)
			{
				flag1=0;
				break;
			}
		}
		else
			break;
	}
	fclose(chk);
	return(flag1);
}

char *find_filenm(char *uid)
{
	char str[URL_LENGTH],temp[20];
	FILE *url;
	clrscr();

	if ((url=fopen("url.txt","r"))==NULL)
	{
		printf("Cannot open file url.txt find_fil-1\n");
		exit(1);
	}
	while(fscanf(url,"%s",str)!=EOF)
	{
		if(strcmp(str,uid)==0)
			break;
		strcpy(temp,str);
	}
	fclose(url);
//	free(str);
	return(outlinks(temp));

}
int main()
{
	FILE *ptr;
	struct node *traversal_node;
	int temp_size;
	clrscr();
	setbuf(stdout,(char*)0);                //flushing output buffer
	temp_size=graph_gen();
	traversal_node=root;
	while(traversal_node->link!=NULL)
	{
		outlink_gen(traversal_node);
		traversal_node=traversal_node->link;
	}
	outlink_gen(traversal_node);
	traversal_node=traversal_node->link;

	traversal_node=root;
	curr_node=root;

	if((ptr=fopen("input.txt","w"))!=NULL)	  //open file for store the node
	{
		printf("Cannot open file input.txt in full graph-2\n");
		exit(1);
	}

	while(temp_size!=0)
	{
	  fprintf(ptr,"%d",curr_node->data);
	  curr_node=curr_node->link;
	  outlink=traversal_node->list_of_outgoinglinks;
	    while(outlink->next_node!=NULL)
	    {
	      traversal_node=outlink->points_to;
	      fprintf(ptr," %d",traversal_node->data);
	      outlink=outlink->next_node;
	    }                                 //while end
	 traversal_node=outlink->points_to;
	    if(traversal_node->data!=0)
	       fprintf(ptr," %d",traversal_node->data);
	 traversal_node=curr_node;
	    if(temp_size!=1)
	       fprintf(ptr,"\n");
	 temp_size--;
	}                                  //while end
	fclose(ptr);                       //close file
	getch();
	return 0;

}

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

hey i'm sorry tat i've to leave now.....
so please look over my code and reply me where have i made a mistake...
will reply u later..

no i'm not callin main anywhere...

Guess my psychic powers were on the fritz....

Please consider reading the Rules and Sticky posts that describe how to ask for help. For one, CODE tags are described
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in!
so you can't miss them -- only ignore them.

And posting almost 400 lines of code and expecting us to try to figure out
1) What you're trying to do
2) What is wrong
3) Where it's wrong
4) How to fix it
just is not appropriate. Help us. Try to pinpoint where it's wrong, it's your job to give us enough details to understand the problem.


[edit]

hey i'm sorry tat i've to leave now.....
so please look over my code and reply me where have i made a mistake...
will reply u later..

Ahhh, another "here, you fix it for me while I go do something else" request. Figures.
[/edit]

Edited 6 Years Ago by WaltP: n/a

Comments
Well said

Like I said before, you are not closing your file pointers after opening them.

I saw one instance in "outlink_gen" function. The "out_fp " file pointer needs to be closed.


Please go through the code to see if there are any more such instances.

You can debug your code using some debugger like gdb.

This article has been dead for over six months. Start a new discussion instead.