public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Newbie's problem with fopen
@ 2003-09-27  2:55 Andre Kirchner
  2003-09-27 13:33 ` Eljay Love-Jensen
  2003-09-27 14:21 ` Ishwar Rattan
  0 siblings, 2 replies; 4+ messages in thread
From: Andre Kirchner @ 2003-09-27  2:55 UTC (permalink / raw)
  To: gcc-help

Hi,

after a lot of debug I realize that there was nothing
wrong with fopen at logLink, but with opendir in
makeSubDir.
makeSubDir creates the following new subdirectory. All
subdirectories are like /home/andre/001,
/home/andre/002, ...
The problem is that even though a directory exists,
sometimes opendir can't open it, and returns NULL.
Does anyone have any idea about what could be wrong?

Thanks,

Andre

int makeSubDir( const char * theDirectory, char *
newSubDirectory )
{
	int subDir = 0;
	if( opendir( theDirectory ) == 0 )
		return( -1 );

	do
	{
		subDir++;
		sprintf( newSubDirectory, "%s/%03d", theDirectory,
subDir );
	}
	while( opendir( newSubDirectory ) );

	mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR
);
	if( opendir( newSubDirectory ) == 0 )
		return( -1 );

	return( 0 );
}

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie's problem with fopen
  2003-09-27  2:55 Newbie's problem with fopen Andre Kirchner
@ 2003-09-27 13:33 ` Eljay Love-Jensen
  2003-09-27 14:21 ` Ishwar Rattan
  1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2003-09-27 13:33 UTC (permalink / raw)
  To: Andre Kirchner, gcc-help

Hi Andre,

When opendir fails, what is errno set to?

You can use errno to diagnose the problem.

The mnemonic identifier associated with the errno value is in errno.h header file.

That should tell you what went awry.

Note:  you seem to be failing to free the memory that opendir allocates.  Eventually, you will run out of heap.

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Newbie's problem with fopen
  2003-09-27  2:55 Newbie's problem with fopen Andre Kirchner
  2003-09-27 13:33 ` Eljay Love-Jensen
@ 2003-09-27 14:21 ` Ishwar Rattan
  1 sibling, 0 replies; 4+ messages in thread
From: Ishwar Rattan @ 2003-09-27 14:21 UTC (permalink / raw)
  To: Andre Kirchner; +Cc: gcc-help

Success in using library functions and system calls depends on
looking at the error returned (trap the error) and read the
man page. It has nothing to do with gcc.

-ishwar


On Fri, 26 Sep 2003, Andre Kirchner wrote:

> Hi,
>
> after a lot of debug I realize that there was nothing
> wrong with fopen at logLink, but with opendir in
> makeSubDir.
> makeSubDir creates the following new subdirectory. All
> subdirectories are like /home/andre/001,
> /home/andre/002, ...
> The problem is that even though a directory exists,
> sometimes opendir can't open it, and returns NULL.
> Does anyone have any idea about what could be wrong?
>
> Thanks,
>
> Andre
>
> int makeSubDir( const char * theDirectory, char *
> newSubDirectory )
> {
> 	int subDir = 0;
> 	if( opendir( theDirectory ) == 0 )
> 		return( -1 );
>
> 	do
> 	{
> 		subDir++;
> 		sprintf( newSubDirectory, "%s/%03d", theDirectory,
> subDir );
> 	}
> 	while( opendir( newSubDirectory ) );
>
> 	mkdir( newSubDirectory, S_IRUSR + S_IWUSR + S_IXUSR
> );
> 	if( opendir( newSubDirectory ) == 0 )
> 		return( -1 );
>
> 	return( 0 );
> }
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Newbie's problem with fopen
@ 2003-09-26  0:57 Andre Kirchner
  0 siblings, 0 replies; 4+ messages in thread
From: Andre Kirchner @ 2003-09-26  0:57 UTC (permalink / raw)
  To: gcc-help

Hi,

I made this function which should to log theLinks into
theLogFile, and return -1 if there was a problem, 0 if
theLinks has just been logged, or 1 if it was logged
before.
The problem is that somehow sometimes it cannot open
theFile even though theLogFile exits at the market
line.
Does anyone have any idea about the problem?
I'm using gcc on a Linux box if it matters.

Thanks a lot,

Andre


# include "logf.h"

int logLink( const char * theLink, const char *
theLogFile )
{
	char theLine[ 256 ];
	FILE * theFile;

	bzero( theLine, 256 );

	theFile = fopen( theLogFile, "r+" );
	if( theFile == NULL )
	{
		theFile = fopen( theLogFile, "w" );
		if( theFile == NULL )
It exits here-> return( -1 );
	}

	while( fgets( theLine, 256, theFile ) )
	{
		if( strcmp( theLine, theLink ) == 0 )
		{
			fclose( theFile );
			return( 1 );
		}
	}

	if( fprintf( theFile, "%s", theLink ) < 0 )
	{
		fclose( theFile );
		return( -1 );
	}
	fclose( theFile );

	return( 0 );
}

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-09-27 14:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-27  2:55 Newbie's problem with fopen Andre Kirchner
2003-09-27 13:33 ` Eljay Love-Jensen
2003-09-27 14:21 ` Ishwar Rattan
  -- strict thread matches above, loose matches on Subject: below --
2003-09-26  0:57 Andre Kirchner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).