Hi guys I just wanna ask what is the difference between the mode_t and st_mode when using sys/stat.h and sys/types.h libraries. I looked them up google and find most people use st_mode but never mode_t.

Recommended Answers

All 7 Replies

Hi guys I just wanna ask what is the difference between the mode_t and st_mode when using sys/stat.h and sys/types.h libraries. I looked them up google and find most people use st_mode but never mode_t.

The mode_t is a type alias. The st_mode is a name of the struct stat member (of mode_t type).

typedef int Int;
Int inT;

What a difference between Int and inT in the snippet above? ;)

so to clarify, will mode_t work if I use it with pre defined macros such as S_IFDIR , S_IFLNK , S_IRUSR ?? or is it a must to use them with st_mode??

for example if I wanna do a file checking loop:

mode_t modeCheck;

while (cin >> input)
	{
		if (modeCheck == S_IFDIR)
		countDir++;
	}

someone help plssss:-/

so there is no other functions to find out the file type (dwrx) other than to check the string (dwrx etc...) if im not allowed to use stat functions?

because if this is the situation, and I implement a strtok or strchr to check the string for d,w,r,x what if the filename is also dwrxdwrx?

> because if this is the situation, and I implement a strtok or strchr to check the string for d,w,r,x what if the filename is also dwrxdwrx?
Er, because the permissions are always in field 3, and the filename is always in field 7 (or whatever your 'ls' generates)

That's part of your exercise to begin with, is take a line, then extract fields, then analyse.

ok so i came up with this code, seems that I have a compilation error and I cant seem to figure out why

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char input[255];
	int countWrite = 0;
	int countLink = 0;
	int countDir = 0;
	int countSize = 0;
	string owners[255];
	char* s;		
	string pid,perm,dir,owner,group,size,date,file;	
	char d,l,w;
	
while (cin.getline(input,255,'\n'))
{
	s = input ;	
	[pid,perm,dir,owner,group,size,date,file] = strtok(s," ");
	//tokenize into substrings	
	
	if (perm == "d*")//count directories
		countDir++;

	if (perm == "l*")//count link files
		countLink++;

	if (perm == "*w*")//count writeable files
		countWrite++;
	
	for (int i = 0 ; i<255 ; i++)
		if (owners[i] != owner)
			owners[i] = owner;			


	size+=size;//count total size of files in bytes
	
	
		
} 


return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.