Jump to content

Photo

Making small tools in C


  • Please log in to reply
16 replies to this topic

#1 Majestic

Majestic

    Megabyte

  • Members
  • 289 posts
  • LocationOut of phase.

Posted 29 September 2012 - 02:53 PM

I'm learning C, so, I'm making small tools to get used to C, then I will move to the harder stuff.

So anyway, here are my two tools:

Attached Files


*insert cliché inspirational quote here*


#2 Majestic

Majestic

    Megabyte

  • Members
  • 289 posts
  • LocationOut of phase.

Posted 30 September 2012 - 07:12 AM

Well, I've been using C++ today, And I'm actually finding it easier than C. It's also better too.

Anyway, I've made a small application, like a console application, And I plan to make a full on console call "The Terminal"

Here is the code:

main.cpp
#include <iostream>
#include <sys/stat.h>
#include <string>
#include "functions.h"
using namespace std;
int terminal()
{
string command = "";
const char *command_cpy;
cout << endl << "$ ";
getline(cin, command);

if(command == "exit")
{
  exit(0);
}
if(command == "delete")
{
  string file;
  const char *file_cpy;
  cout << " Delete file: ";
  getline(cin, file);
  file_cpy = file.c_str();
 
  if(file == "cancel")
  {
   cout << " Delete file: canceled!" << endl;
   terminal();
  }
  else if(file == "")
  {
   cout << " Path cannot be empty!" << endl;
   terminal();
  }
  else if(!file_exists(file_cpy))
  {
   cout << " Delete file: " << file_cpy << " does not exist!" << endl;
   terminal();
  }
  else if(is_file(file_cpy) == 0)
  {
   cout << " Delete file: " << file_cpy << " is not a file!" << endl;
   terminal();
  }
  else
  {
 
   if(remove(file_cpy) == 0)
   {
    cout << " Delete file: " << file_cpy << " deleted successfully!" << endl;
    terminal();
   }
   else
   {
    cout << " Delete file: " << file_cpy << " could not be deleted!" << endl;
    terminal();
   }
  }
}
if(command == "")
{
  cout << endl;
  terminal();
}
else
{
  cout << " Command: " << command << " does not exist!" << endl;
  terminal();
}

return 0;
}
int main()
{
cout << "Welcome to The Terminal. Read the manual for more info." << endl;

terminal();

return 0;
}

funtions.h

#include <iostream>
#include <sys/stat.h>
#include <string>
using namespace std;
int file_exists(const char *file_name)
{
struct stat buf;
int i = stat(file_name, &buf);
if(i == 0)
{
  return 1;
}
else
{
  return 0;
}
}
int is_file(const char *path)
{
struct stat s;
if(stat(path, &s) == 0)
{
	 if(s.st_mode & S_IFREG)
	 {
		 return 1;
	 }
	 else
	 {
	  return 0;
	 }
}
else
{
	 return 0;
}
}

*insert cliché inspirational quote here*


#3 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 05 October 2012 - 02:12 AM

May I ask why you're writing a terminal that can only run within a terminal?

Rumors of my demise have been greatly exaggerated.


#4 Wolf

Wolf

    Zettabyte

  • Members
  • 6,487 posts

Posted 05 October 2012 - 08:21 AM

May I ask why you're writing a terminal that can only run within a terminal?


I was gonna ask this, but I didn't wanna sound stupid >.>

lest there be some strikingly obvious reason

#5 Guest_ElatedOwl_*

Guest_ElatedOwl_*
  • Guests

Posted 05 October 2012 - 12:57 PM

May I ask why you're writing a terminal that can only run within a terminal?

We don't always need an excuse to program. :P

#6 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 07 October 2012 - 02:03 AM

We don't always need an excuse to program. :P

This is true.

Why just earlier today I was testing out an unfamiliar IDE, so I wrote a window with a (_|_( button that says "BUTTS" if you click it.

Ahh, technology.

Rumors of my demise have been greatly exaggerated.


#7 Wolf

Wolf

    Zettabyte

  • Members
  • 6,487 posts

Posted 07 October 2012 - 08:40 AM

This is true.

Why just earlier today I was testing out an unfamiliar IDE, so I wrote a window with a (_|_( button that says "BUTTS" if you click it.

Ahh, technology.


priceless

#8 Dasherman

Dasherman

    Megabyte

  • Members
  • 496 posts
  • LocationAzeroth and Sanctuary, simultaneously by using quantum powers

Posted 13 October 2012 - 12:46 PM

This is true.

Why just earlier today I was testing out an unfamiliar IDE, so I wrote a window with a (_|_( button that says "BUTTS" if you click it.

Ahh, technology.


Is it opensource?
Quis custodiet ipsos custodes?
(//MihiPotestasSit\\)

#9 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 15 October 2012 - 04:20 PM

Is it opensource?

LGPL. I was using it to test out Qt creator and Qt has a strict LGPL license. You want the source? It's nothing special.

Rumors of my demise have been greatly exaggerated.


#10 Dasherman

Dasherman

    Megabyte

  • Members
  • 496 posts
  • LocationAzeroth and Sanctuary, simultaneously by using quantum powers

Posted 16 October 2012 - 01:41 AM

LGPL. I was using it to test out Qt creator and Qt has a strict LGPL license. You want the source? It's nothing special.


Just post it then :D
Never actually saw any Qt source which wasn't overly complicated.
Quis custodiet ipsos custodes?
(//MihiPotestasSit\\)

#11 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 16 October 2012 - 05:51 AM

communicate.h
#ifndef COMMUNICATE_H
#define COMMUNICATE_H
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
class Communicate : public QWidget {
    Q_OBJECT
public:
    Communicate(QWidget *parent = 0);
private slots:
    void OnStupid();
private:
    QLabel *label;
};
#endif // COMMUNICATE_H

communicate.cpp
#include "communicate.h"
Communicate::Communicate(QWidget *parent) : QWidget(parent) {
    QPushButton *butts = new QPushButton("(_|_(", this);
    butts->setGeometry(50,80,75,30);
    label = new QLabel("", this);
    label->setGeometry(190,80,80,30);
    connect(butts, SIGNAL(clicked()), this, SLOT(OnStupid()));
}
void Communicate::OnStupid() {
    QString butts = "BUTTS";
    label->setText(butts);
}

main.cpp
#include "communicate.h"
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    Communicate window;
    window.setWindowTitle("Butts");
    window.show();
    return app.exec();
}

Rumors of my demise have been greatly exaggerated.


#12 Wolf

Wolf

    Zettabyte

  • Members
  • 6,487 posts

Posted 16 October 2012 - 07:07 AM

Qhave a nice Qday

thank you for using Qt Qsoftware

#13 Dasherman

Dasherman

    Megabyte

  • Members
  • 496 posts
  • LocationAzeroth and Sanctuary, simultaneously by using quantum powers

Posted 16 October 2012 - 10:52 AM

I thought I would understand it using C#-knowledge, but I'm not getting that far. Might just be because I'm a noob though.
Quis custodiet ipsos custodes?
(//MihiPotestasSit\\)

#14 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 16 October 2012 - 10:51 PM

It's pretty basic stuff. I really haven't spent much time learning about C#, but if you can't grasp the code I posted with basic knowledge of it, I'd really recommend you spend some time learning a more traditional OOP language.

Rumors of my demise have been greatly exaggerated.


#15 Dasherman

Dasherman

    Megabyte

  • Members
  • 496 posts
  • LocationAzeroth and Sanctuary, simultaneously by using quantum powers

Posted 20 October 2012 - 04:24 PM

It's actually starting to make sense now, at 0:23 in the morning.
I don't get what "label->" does though.
And some other stuff that I guess is Qt knowledge, which I lack.
Quis custodiet ipsos custodes?
(//MihiPotestasSit\\)

#16 K_N

K_N

    Megabyte

  • Members
  • 576 posts
  • LocationPhoenix

Posted 20 October 2012 - 10:15 PM

label is an object, saying label->setGeometry() means to use the setGeometry function inside the class called Qlabel that label belongs to.

-> is an operator that always precedes an object, and can refer to a function or variable inside that object.

Say I made a class like this:

class tree {
public:
	string fruit;
}

Then made an object of that class like

tree : appleTree {
   fruit = "apples";
}

I'd access the value of fruit in apple tree like this:

appleTree->fruit;

Rumors of my demise have been greatly exaggerated.


#17 Majestic

Majestic

    Megabyte

  • Members
  • 289 posts
  • LocationOut of phase.

Posted 21 October 2012 - 01:19 AM

Bloomin' threadjackers... :P

*insert cliché inspirational quote here*