creation of unwanted directory

hi all

i have a file with server id's and I need to create a directory corresponding to each server id listed in the text file.
eg:
id directory name
1 node_1
2 node_2
... ..... and so on.

There is no id with value 0 in the said text file.

the program is creating the directories but also creating an extra directory node_0 when executing in one machine and does not create directory node_0 (as required) when executing in another machine.
Can anybody tell me the reason for this.

plz. help

thanx

There is a bug in the program. Different behavior on different systems is a common result of a bug in a C program.

Post the code that creates a directory and we can probably help you.

///////// here is the code that i am using to create the directory//////////////

#include<stdio.h>
#include<stdlib.h>
#include<sys/dir.h> 
#include<malloc.h>
#include<string.h>

struct sconfig *head;
struct sconfig *tail;
struct sconfig
{
	int type,id;
	char username[20], IP[15];
	struct sconfig *next;
};

int dircreate(int id,int type)  //code to create the directory
{
	char *dname;
	int stat;
	dname=(char *)malloc(sizeof(int));
	sprintf(dname,"node_%d",id);
	stat=mkdir(dname,"rwx");
	if(stat!=0)
	{
		printf("\nnode dir cud not b created");
	}
	return 0;
}

int loadlist(int chcount)//code to read s_config.txt	
{
	FILE *fsdt;
	struct sconfig *temp;
	if((fsdt=fopen("/root/sample_read/s_config.txt","r")) == NULL)
	{
		fprintf(stderr, "cannot open s_config.txt\n");
		return 1;
	}
	while(feof(fsdt) == 0)
	{
		temp=(struct sconfig *)malloc(sizeof(struct sconfig));
		fscanf(fsdt,"%d %s %s %d",&temp->id, temp->username, temp->IP, &temp->type);
		dircreate(temp->id,temp->type);
		temp->next='\0';
		if (head==NULL)
		{	   head=temp;   		}
		else
                {         tail->next=temp;		}
		tail=temp;
	}
	fclose(fsdt);
	return 0;
}

int main()
{
	FILE *fsdt;
	int chcount=0;
	char ch;
	fsdt=fopen("/root/sample_read/s_config.txt","r");
	if (fsdt!=NULL)
	{
		while(1)
		{
			ch=fgetc(fsdt);
			if(ch==EOF)
				break;
			else
				chcount=chcount+1;
		}
		fclose(fsdt);
		if(chcount>0)
		{ 		loadlist(chcount);		}
	}
	else
	{
		fprintf(stderr, "cannot open s_config.txt\n");
		return 1;
	}
	return 0;
}

/////////////////////contents of s_config.txt file ////////////////////////

1 bhakti 123.123.123.123 1
2 mridu 255.254.255.254 2
3 ash 11.11.11.11 1
4 ashish 45.23.12.56 2

hope anyone can find the bug

thankx

dname=(char *)malloc(sizeof(int));
sprintf(dname,"node_%d",id);

this'll allocate 4 bytes on my machine for instance, and string "node_%d" requires at least 6 bytes.

hi

thanx for your suggestion, but allocation of large size to dname is not of any help as its not making any difference.

I tried putting a printf() statement with in the while loop that reads the file.
Even when the ptr finishes reading the last line of the file its not showing eof. and is executing the loop for 1 extra time.

any suggestion will b of great help.

thanx

A lot of bugs there! :eek:

Well, the one you are asking about is your while loop. feof() will not return EOF until fscanf goes too far. Check the return code from fscanf. Do not do the rest of the loop if fscanf fails. Or rewrite the loop to loop while fscanf succeeds.