Home > C++, Experiments > C++ Program to tweet

C++ Program to tweet

It’s very interesting to play with C++ and if it is in GNU/Linux, lots of tools like ‘wget’, ‘grep’,etc. are there to help us in time. Here is a simple C++ snippet which tweets directly to your time line. Feel free to add more features and would be great if you could share it with me too πŸ™‚

/*
 *      shell-tweet.cpp
 *
 *      Copyright 2009 Ershad K  ershad92@gmail.com
 *      Licensed under GPL Version 3
 *
 *      Please change USERNAME, PASSWORD and compile with g++
 *      To compile - g++ shell-tweet.cpp
 *      To execute - ./a.out
 */

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
char name[] = “username”, password[] = “password”, status[500], cmd[500];

char sym[] = {char(34),”};

again: cout << “\n Status: “;
fgets (status,140,stdin);

cout << “\n Length = ” << strlen(status);

if (strlen(status) > 139)
{
cout << ” Exceeds 140 character limit, Type again \n”;
goto again;
}
else
{

strcpy(cmd,”wget -nv –keep-session-cookies –http-user=”);

strcat(cmd,name);
strcat(cmd,” –http-password=”);
strcat(cmd,password);
strcat(cmd,” –post-data=”);
strcat(cmd,sym);
strcat(cmd,”status=”);
strcat(cmd,status);
strcat(cmd,sym);
strcat(cmd,” http://twitter.com:80/statuses/update.xml&#8221;);

system(cmd);

cout << “\n Done”;
}
return 0;
}

Advertisements
  1. September 20, 2009 at 10:22 pm

    Good Work! πŸ™‚
    One Suggestion is that it is not good to use goto statements in programs and another is that try methods like hashing/mappings from std. template library!

    • September 21, 2009 at 10:21 am

      Thank you very much for the compliment. I’m new to C++ and will definitely follow your advice here after πŸ™‚

  2. September 24, 2009 at 2:22 pm

    Nice work..
    Just some nitpicking.. πŸ˜‰
    That fgets will read a maximum of only 139 characters (the 140 size limit is including the trailing null character). So, strlen(status) will always be less than 140 even if the user input was longer. So there’s no point in checking if the length is greater than 139, since it’ll never be! (check the behavior with a smaller limit like 4 or 5).

    Good luck.

    • September 24, 2009 at 3:42 pm

      Thanks a lot for your kind comment.

      You are right, There’s no point checking the user input. It was my mistake, I had been using ‘gets()’ instead of ‘fgets()’ without any size parameter. Since it’s dangerous, I replaced it with ‘fgets()’ just before posting and forgot to remove the ‘if’ condition.

      Dear all, you could one of these two :
      1. Remove the ‘if’ condition where you manually have to care 140 limit.
      2. Use ‘scanf()’ instead of ‘fgets()’ so that the program would check the limit.

      Thanks again Syam etta πŸ™‚

      • September 24, 2009 at 6:07 pm

        One more thing.. It’s not a good idea to use scanf instead of fgets. Are you suggesting to use “%s” as the format argument of scanf? In that case, a user could give a very long input so that the ‘status’ array overflows. In that case, you’ll end up with the same problem as gets().
        To make scanf read only fixed-length strings, use something like “%140s” (check the documentation to see if it includes the null termination, I don’t remember well). But then again, this solution is same as using fgets!

        • September 24, 2009 at 6:37 pm

          Yeah, You are right. Here is one solution, Let the user input as much as he likes and if it’s > 140, program will ask for input again.

          Dear all, please change fgets like this

          fgets(status,500,stdin);

          Thank you etta πŸ™‚

          • September 24, 2009 at 7:26 pm

            You’re welcome..

            But Ershad, you are not quite there yet 😦
            Consider this (unlikely, but possible) situation: In the first attempt, the user enters 550 characters. fgets will read 499, and will *leave the remaining characters* in the input buffer. So, in the next iteration of the loop, the fgets call will pick up those characters and will not work as expected.

            Test it out with some smaller numbers: like 10 characters for the array size and 3 characters for the message length limit.

            So, you’ll need to somehow ignore the remaining characters in the input buffer. The C++ way is to use cin.ignore() function, but since you are using fgets, use some C method. i guess a web search will get you a *portable* solution (and no, fflush(stdin) is a terrible idea!).

            I guess you never thought it’d get this complicated when you first wrote this program πŸ™‚
            But this is how we learn and improve ourselves. Kudos to your efforts.

            • September 25, 2009 at 9:10 am

              That was a wonderful comment!! Thank you very much, etta πŸ™‚

              Web search gave me these functions :

              std::cin.clear();
              std::cin.ignore(); // the one you advised

              If fgets is a C way, shall we use the C++ way ? Will

              cin.clear()

              clear the input buffer ?

              ..But this is how we learn and improve ourselves.

              I have no words to express my joy, you made me aware of the problems that we face while handling strings – which are very important in programming. Thanks a lot, etta πŸ™‚

              • September 25, 2009 at 3:40 pm

                I appreciate your spirit. Good.. he he..

                Anyways, if you are using C++, forget about fgets. Just create an object of std::string and use cin >> str to read in to that string. No worries about string length now!

              • September 25, 2009 at 3:45 pm

                And cin.clear() is not the function you want. cin.ignore() is the one that’ll work for you.

                cout << "Enter text: ";
                cin.get(status, 500);
                if(strlen is too high)
                {
                print some message;
                cin.ignore(std::numeric_limits::max(), ‘\n’);
                }

                you should #include for that numeric_limits() function to be available.

                http://www.daniweb.com/forums/thread90228.html

                }

                • September 25, 2009 at 3:57 pm

                  Thanks a lot, etta. That solves the issue πŸ™‚

                  You are a wonderful programmer and a tutor !! All the very best.

                  PS: Playing with strings are not as easy as i thought, be careful while approaching πŸ˜‰

                  • September 25, 2009 at 4:01 pm

                    No more flattering please. I can’t take it any more πŸ˜‰
                    Keep programming and blogging. I’ll be tracking it, thanks to planet smc.

                    • September 25, 2009 at 4:08 pm

                      yeah, thanks to SMC too πŸ™‚

  3. May 21, 2010 at 9:22 am
  4. Shajahan ES
    May 15, 2011 at 5:07 pm

    i dnt completely understand the code, since i have just started with c++.

    but the two codes u hav put, to copile nd then execute that was useful.. thanks..

    • May 15, 2011 at 6:26 pm

      Thank you. Twitter moved OAuth based authorization a few months back, this C++ program no longer works. What this program had been doing was passing the user and password information, along with text to tweet to a command app called wget. It comes bundled with almost every GNU/Linux distro. It is primarily intended to download files from internet, also it has many other features, like the one did here – sending POST request to a server with username and password. The C++ part was to get input string to tweet and call wget with appropriate parameters using system() function, defined under stdlib.h. Hope you understood it now.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: