hi guys its my first time doing a c++ program to manipulate shell input
I need to create a c++ program which reads input from this unix shell command >> ls -ali
and then im supposed to count the number of dir,write files.
I am not allowed to use the STAT() function from the library.

I dont know whats wrong with my code below....but it always give me a 0 value output. I think the way i read the input from the command ls -ali is wrong

#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
using namespace std;

int main(int argc, char** argv)
{
	char input[150];
	int writeFile = 0;
	int countDir = 0;
	mode_t modeCheck;
	
	while (cin >> input)
	{
		if (modeCheck == S_IFDIR)
		countDir++;
		if (modeCheck == S_IWRITE)
		writeFile++;
	} 
	cout << countDir << endl;
	cout << writeFile << endl;
}

I'm not too sure, but may be this might help. This was done on windows.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string p;
	while (cin >> p != '\0')
		cout << p << endl;
}

> ls | ./myProg.exe

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.