How is the CodeRunner Program Input field used? - objective-c

How is the Custom Run sheet Program Input field used in CodeRunner say for a C or Objective-C program?

The text entered in the Program Input text box will be sent to your program/script through the standard input. You can access the input in the same way that you would access the standard input using your language of choice.
In C, you'd use the standard fread, fgets, fgetc etc functions. The following example will echo the supplied text in the Program Input to the console:
#include <stdio.h>
int main(int argc, char *argv[]) {
char str[80];
while(fgets(str, sizeof(str), stdin)) {
printf("%s", str);
}
}

Related

C++ Assigning a value to char *argv[] from inside a program

The customary way of passing values through argv in this declaration
int main(int argc, char* argv[]) {
is to pass it from your console through argv array into a program. I came across a situation where I wanted to achieve something different as follows. The program has the usual
int main(int argc, char* argv[]) {
declaration and inside my program I have a
function void foo(argv[1], 0).
Here is the layout and flow of my program's logic:
program ggg.cpp
:
void foo(char* x, int y) {
:
}//end foo
:
int main(int argc, char* argv[]) {
:
foo(argv[1], 0);
:
}//end main
(1) My normal method. Normally, when I compile the ggg.cpp program and my console input is:
ggg.exe . //the second input is a dot "."
the program runs fine (because argv[1] = '.' inside the program)
(2) However, I wanted to get innovative and execute the program as:
ggg.exe //here, the dot is not included
so that inside the program, I can assign the dot to argv[1] but I kept getting errors - no matter how I try other variations of the assignment statement.
By the way, I tried the assignment statement
argv[1]='.';
inside the main program (in method (2)) but it didn't work. I got an error message.
Background Information. I used to be very good in programming in my hay days but now I have forgotten a lot of things because I left coding for a very long time to do other things.

Passing File manually instead of stdin value_t read_sexpr(FILE *f) in c

I am using c file which takes input from console
value_t read_sexpr(FILE *f) // This is a Function
read_sexpr(stdin) // Thats how it is calling from main
I want to pass string instead of taking input from console . Can it be done ?
Use fmemopen() to open a FILE backed by a string:
#include <stdio.h>
const char mystring[] = "test";
FILE *testfile = fmemopen(mystring, sizeof mystring, "r");
Note that fmemopen is not a standard C function. It is standard POSIX though.
Don't forget to close the file afterwards. Read the manual page for more details.

Why the loop "scanf" method dead circle in objective-c lanuguage?

I write a small console program in objective-c. It need to use the scanf method to receive the number.When I enter a character, it will make a mistake.So I try to solve it,but it has entered a cycle of death! See the following code, to help me solve it, thank you very much!
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
int num1 = 0;
NSLog(#"Please input number:");
while (!scanf("%d", &num1)) {
fflush(stdin);
NSLog(#"Input error,just input number:");
}
}
return 0;
}
The documentation for fflush states:
The function fflush() forces a write of all buffered data for the given output or update stream via the
stream's underlying write function. The open status of the stream is unaffected.
and you are trying to flush input. Try fpurge:
The function fpurge() erases any input or output buffered in the given stream.
Also do not write !scanf(...). Check the documentation, this function returns an integer, not a boolean, and the value could be positive or negative (look up the definition of EOF). A negative value indicates an error, but the ! operator will yield false and your code would not ask for new input.
If successful scanf returns the number of items successfully parsed, check for that.
The documentation for all these functions is available via Xcode or the man command.
HTH

Why readline() input-echoing in console?

int main(int argc, const char * argv[])
{
const char* input = readline(NULL);
NSLog(#"%d", atoi(input));
return 0;
}
I was trying to enter a sequence of characters in console, but whenever I typed the one key, the same char will following immediately after the char which I just typed. (For example, I typed '1' in console, the console will display "11") Moreover, when I try to delete the entered char using delete-key. The up-side-down question mark will appear, but inside the char* the last char has been deleted?
Why all these happened?
This is expected behavior as of Xcode 5. You can run it outside Xcode in the terminal and it should behave normally.

How to display colored text in the terminal?

How can I display colored text on the terminal from a ObjectiveC command line application?
Is there a portable way that will work both on iTerm and Terminal?
Just create a command line project and use this one as a starter. When compiling and running it you should see colors in the terminal.
#include <stdio.h>
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
int main(int argc, const char * argv[])
{
printf("%sred\n", KRED);
printf("%sgreen\n", KGRN);
printf("%syellow\n", KYEL);
printf("%sblue\n", KBLU);
printf("%smagenta\n", KMAG);
printf("%scyan\n", KCYN);
printf("%swhite\n", KWHT);
printf("%snormal\n", KNRM);
return 0;
}
If you type set in Terminal you’ll probably see:
TERM=xterm-color
Among others. The “xterm-color” is an old way of saying what control and escape sequences the terminal will respond to, if you look that up you’ll find lots of fun things.