LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-03-2004, 03:44 PM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
strcmp and some problems I have [strange ones]


hello I'm trying some low-level stuff:
Code:
while( (dirent_ptr = readdir(dir_ptr)) != NULL )
	{
		//printf("i-node: %ld - ", dirent_ptr->d_ino);
		//printf("filename: %s\n", dirent_ptr->d_name);
		
		if ( stat(dirent_ptr->d_name, &boo_buf) < 0 )
		{
			perror(dirent_ptr->d_name);
			exit(1);
		}

// OK till here


		// result SHOULD  be 0, if file matches with . [but it IS NOT!!] :(
		int result;
                printf("%s\n",dirent_ptr->d_name);
		result = strcmp(dirent_ptr->d_name, ".") ;
                printf("%i\n",result);
eventhough dirent_ptr->d_name is in the beginning . result DOES NOT become 0
if I have ".." then it becomes 0 [in the second time of the loop]

so I guess the strcmp doesn't like the "."??
I tried '.' GCC doesn't like it now!

anyhelp is HIGHLY appreciated.

thx in advance

Last edited by zeppelin; 04-03-2004 at 04:25 PM.
 
Old 04-03-2004, 04:12 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
So your output is

.
1
..
0

Correct?
 
Old 04-03-2004, 04:13 PM   #3
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
yes. irritating hah?

I'm forced to write in C. Oh boy! in C# one could just write != , ==
well I know the Power of C in the low-level, that's why I want your help in this [and not only] but this is the basic to continue :P

Last edited by zeppelin; 04-03-2004 at 04:15 PM.
 
Old 04-03-2004, 04:24 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
c#? don't swear at me C++ does have a string class as well you know.

printf("dirent_ptr->d_name");

shouldn't that be

printf( "%s", dirent_ptr->d_name );
 
Old 04-03-2004, 04:25 PM   #5
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
oh yes. the code has some addition to make it easy to understand and to help!
plz propose me a solution. I know it's %s :P
I also know that C++ does the trick.
a C only solution would rock
 
Old 04-03-2004, 04:31 PM   #6
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Try '.' instead of "."

or a simple array check

Code:
if ( dirent_ptr->d_name[0] == '.' && dirent_ptr->d_name[1] == '\0' )
   result = 0;
else
   result = 1;
This is probably better and quicker than calling a function.
 
Old 04-03-2004, 04:53 PM   #7
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
oh well you're right. strcmp doesn't do the thing with "." nor with '.'
well I should check just the array then as you say. thanks

this is the best :P [damn to that strcmp]
Code:
if ( dirent_ptr->d_name[0] == '.' || dirent_ptr->d_name == ".." )
			result = 0;
		else
			result = 1;
thanks for your quick help
 
Old 04-03-2004, 05:00 PM   #8
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
It is strange that strcmp is returning 1 on "." but 0 on ".." I have tried it hear and it is the other way around, something weird going on in your setup

To be honest I'm not sure that code will work. dirent_ptr->d_name == ".." your comparing the pointer to a constant array. Theres also stuff that could start .thishere as well.

Last edited by leonscape; 04-03-2004 at 05:02 PM.
 
Old 04-03-2004, 06:02 PM   #9
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
d_name is a character array IIRC

anyways, the point is that now it's working as expected, and the problem is afterwards [should write an algorithm that will check for the files in a dir, and check some of those files are Dirs, and if they are it should open them etc.. go deep in a recurvise implementation..]

or I could try reading the code in GNU cp

the cp -a
i believe should be fine :P
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 08:29 AM
strcmp() giving a large value instead of -1, 0, or 1 sovietpower Programming 4 03-09-2005 08:50 AM
c++ strings-strcmp function...... sachitha Programming 4 09-12-2004 07:28 AM
strcmp.. ways to make it work.. miguetoo Programming 1 03-08-2004 07:13 PM
strcmp function does not seemed to work Linh Programming 4 06-12-2003 06:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration