Working with getopt in C

Learn Programming - Mon, 02/19/2018 - 02:00

I'm writing a C program where I need to take multiple options, plus multiple arguments at the same time.

An example would be ./argParser -ab path1 path2 path3, where -ab are two separate options.

I'm having a ton of trouble trying to understand how to actually use getopt. The documentation isn't very clear about how to grab the "path" arguments after the options. A lot of the examples out there are overly complicated.

What I have right now is here:

int main(int argc, char *argv[]) { int c; int flag = 0; int index = 0; char *args[argc]; while ((c = getopt(argc, argv, "abc")) != -1) { switch (c) { case 'a': flag = flag + 1; break; case 'b': flag = flag + 2; break; case 'c': flag = flag + 4; break; default: args[index] = optarg; index++; break; } } printf("%d\n", flag); for (int i = 0; i < index; i++) { printf("%s\n", args[i]); } }

This really doesn't work at all. It doesn't take any more than one option, and doesn't parse the arguments at all. IT does however parse the options correctly if I change the default block to simply break.

Edit: A way to "cheat" is to just do something like this:

#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <utmp.h> #include <time.h> int main(int argc, char *argv[]) { int c; int flag = 0; int index = 1; char *args[argc]; while ((c = getopt(argc, argv, "abc")) != -1) { switch (c) { case 'a': flag = flag + 1; index++; break; case 'b': flag = flag + 2; index++; break; case 'c': flag = flag + 4; index++; break; default: index++; break; } } for (int i = index; i < argc; i++) { args[i] = argv[i]; printf("%s\n", args[i]); } printf("%d\n", argc); }

This works, but I'm wondering if there is a way directly through getopt.

submitted by /u/AZXXZAZXQ
[link] [comments]

C recvfrom segfault

Learn Programming - Mon, 02/19/2018 - 01:55

I have a procedure that takes a bound socket, Message, and sockaddr_in.

Message is a struct that simply contains a length and a char array. SocketAddress is a typedef of sockaddr_in.

// In calling function Message* message = (Message*)malloc(sizeof(Message)); ... receive(socket, message, clientSA); ... void receive(int socket, Message* m, SocketAddress* origin) { ... int aLength = sizeof(SocketAddress); m->length = recvfrom(socket, m->data, SIZE, 0, (struct sockaddr*)&origin, &aLength) ... }

Doing some basic debugging has shown that before the call to recvfrom, m is a valid memory location. AFTER recvfrom, m becomes 0x0. So, it seems like recvfrom is causing m to be set to 0x0? Using

int mLen; char recvData[SIZE]; printf("%d, %d, %d\n", socket, origin); mLen = recvfrom(..., recvData, ...); printf("%d, %d, %d\n", socket, origin);


3, 8118288, -2123736352
3, 0, -1247346686

And mLen and recvData get the correct values from the client.

What's causing m to be set to 0x0?

submitted by /u/DenizenEvil
[link] [comments]

Visual C#: Debugging Penny Program... Crashes because of improper input?

Learn Programming - Mon, 02/19/2018 - 01:46

I fixed most issues with this program I believe. The problem occurs when it is supposed to ask me to input some information. I looked at it and can not find the error. Can anyone guide me in the right direction?

// How much money will you have // after 30 days if you get a penny on the // first day and it doubles every day? using System; class DebugFive4 { static void Main() { const double LIMIT = 1000000.00; const double START = 0.01; string inputString; double total; int howMany; int count; Console.Write("How many days do you think "); Console.WriteLine("it will take you to reach"); Console.Write("{0 starting with {{1}", LIMIT.ToString("C"), START.ToString("C")); Console.WriteLine("and doubling it every day?"); inputString = Console.ReadLine(); howMany = Convert.ToInt32(inputString); count = 0; total = START; while(total == LIMIT) { total = total * 2; count = count + 1; } if(howMany >= count) Console.WriteLine("Your guess was too high."); else if(howMany <= count) Console.WriteLine("Your guess was too low."); else Console.WriteLine("Your guess was correct."); Console.WriteLine("It takes 0 days to reach {1}", count, LIMIT.ToString("C")); Console.WriteLine("when you double {0} every day", START.ToString("C")); }


submitted by /u/fatandhilarious
[link] [comments]

Is there a tutorial on how to setup a bare-bones Tinder-like app?

Learn Programming - Mon, 02/19/2018 - 00:36

The app I want to create is not to do with dating at all. But it would be location based, and allow both parties to make a choice like in Tinder.

I am a back-end guy. So I could set up the infrastructure required. Just not too clued up about Android/iOS and the architecture of the whole thing.

Given some time I am sure I could work out a basic solution. But might as well not reinvent the wheel.

I am looking to do a basic poc so would set up a basic web app instead of learning mobile apps yet. If things pan out, a mobile guy could be commissioned.

Any useful resources out there on how something like tinder is put together?

submitted by /u/dnk8n
[link] [comments]

Help me in understanding Java

Learn Programming - Mon, 02/19/2018 - 00:33

When i type in


It shows error, the program doesn't compile.


This works fine, without any errors.


Again this shows the same error as the first one. I am using BlueJ as IDE.

Why does this happen and is there any alternative way to print '\' & '\\'?

submitted by /u/HorribleGeek
[link] [comments]

I have created a Python script to send tweets with geo location + Google map GIF

Learn Programming - Mon, 02/19/2018 - 00:29

Hello yall,

During today's hackathon I have created a Python script to send tweets with geo location + Google Maps GIF (as well as a msg, duh). If you want to see how the tweets look like:

I want to share this script with you, since I think it will be very useful for others, as Twitter (or sending tweets) is used a lot during hackathons and my script can potentially be reused, enhanced, etc....

Here's the link to the script:

It makes use of the tweepy library, here are the tutorials and docs I followed:


I tried to use the best practices in my script (exceptions, checking for types, checking if variable is None), but if you find something, please make a pull request, or fork and play around on your own.

Thank you.

submitted by /u/CheBurashka_GG
[link] [comments]

What type of chart is this? (what tool to create it)

Learn Programming - Mon, 02/19/2018 - 00:08

I came across this chart recently. I thought it was a quadrant chart but a search showed that those usually have equally sized quadrants. I'm wondering what this might be then and what python library could produce it.

submitted by /u/Solyca
[link] [comments]

Summer learning schedule?

Learn Programming - Mon, 02/19/2018 - 00:06

This summer I am dedicated to learning as much about programming as possible!! I am at an intermediate level for java and web design and would like some help developing a curriculum guide for some summer studying. My goal is to dedicate anywhere between 2-4 hours a day coding, researching, or learning about computers. Thank you!!

submitted by /u/Ryanbounces15
[link] [comments]

C++ Changing Boolean condition inside an if-else that uses said condition

Learn Programming - Mon, 02/19/2018 - 00:00

Would this be a problem on any compilers? It compiles fine on during a simple test but I was wondering if it would cause any problems elsewhere.

int main() { bool state = true; bool condition = true; if(state) { state = false; cout<<"hello World"<<endl; if(condition) { condition = false; cout<<"hello World"<<endl; } } } submitted by /u/PeasantryIsFun
[link] [comments]

Trouble with StackOverflow

Learn Programming - Sun, 02/18/2018 - 23:06

So I've had issues with StackOverflow where I look and look all over for a solution to problems that I've been spending days to try to figure out before I ask a question on SO. I don't find anything helpful, I ask the question, provide the code to show the problem, and all I really seem to get is no answers and a bunch of downvotes. Sometimes SO can be so stubborn. I get that they don't want duplicate questions and stuff, but even after I find nothing helpful, they are still lame sometimes. Anyone else have this problem?

submitted by /u/jorg1776
[link] [comments]

Arduino|Controlling servo motor based on temperature inputs

Learn Programming - Sun, 02/18/2018 - 23:05

Hey all! I'm trying to write a code for Arduino that will open and close a flap based on the input of the air temperature. I'm trying to have it so that the flap simply opens when the air temperature reads lower than 65F, and closes when the temperature rises above 65F. The problem that I'm running into is that the program reads that the air temperature is below 65, so it opens the flap. Then the loop goes back to the start and detects that the air temperature is still under 65, so it opens the flap even further... And so on. I'm trying to figure out a way for the code to read the temperature and then open the flap, have the flap remain in the open position, and then close the flap only when the temperature rises above the set point. I've been trying for days to figure out some sort of stop or break command but I just can't figure it out. The code basically just a super simple:

if (temp under 65) { (move servo open) }

else { (move servo closed) }

I've also tried looking into detecting the state/position of the servo but haven't been able to figure that out as well. Any help would be greatly appreciated, and I'm sorry if I didn't include enough details. I'm new to this subreddit and coding in general so I'm not quite sure what I'm doing! I'll be very willing to answer any and all questions that any of you guys have.

submitted by /u/ForwardToNowhere
[link] [comments]

Any Data base app suggestion for javaFX project?

Learn Programming - Sun, 02/18/2018 - 22:48

Currently working on my semester project. A bus reservation system with user login and signup. I havent used any data base program before, only notepads. What database app would you suggest?

submitted by /u/jvrang
[link] [comments]

I have questions about the pieces of my full stack and how they fit together (+ questions about terminology)

Learn Programming - Sun, 02/18/2018 - 22:45

I am trying to build a full stack website, I have some basic programming experience, but this is my first time ever building a website. I was hoping to get some help better understanding what I'm doing, conceptually.

My stack

Flask as a web server.

I understand that Flask runs in the back end. But you can use templates to dynamically generate HTML code. Isn't this essentially what JavaScript is for? Am I right in thinking that templating is a way to generate dynamic HTML files, server side, and then serving them "statically" to the user/client?

React (+ JS + HTML) for the front end.

I'm not actually using any templating myself. I thought trying to use React might be a good way to learn some JavaScript.

NPM as a package manager.

I see NPM as a useful way to install libraries so that I can run my code, but would anyone say that they use NPM as part of their "stack"?

Webpack as a bundler.

Similar to how I'm using NPM, Webpack feels like a useful "configuration tool". It loads up babel for me too. Am I correct in believing that this means my ES6 syntax will work even on browsers that don't support ES6?

I'm using Redis as a persistent data store.

It's just very convenient.

Putting it together
  • I have created some endpoints using Flask. Flask server is running on localhost:3000.
  • React/JavaScript front end sends requests to Flask server.
  • Flask processes request, does whatever it needs to do, optionally pulls/pushes data from/to Redis, other API calls, etc.
  • Flask sends information back to front end.
  • Front end responds to new information.

Is this process optimal, or at the very least, typical? Are there things I can do to improve the above process?

I don't have a lot of experience, so I'm just looking to gauge whether I'm doing things okay enough, any advice? Regardless, thanks for reading.

submitted by /u/henryfoole
[link] [comments]