I'm learning to create multi-file programs for one of my classes. Ultimately I need to implement a stack and do some stuff with the stack. Before I began implementing the stack I wanted to make sure my files were all linked together properly with a header file. For some reason when the user inputs "pop" or "print" the conditional statement is not triggered and the method in stack.c is not called. I've been looking at this for awhile and haven't gotten anywhere. Thank you for the help
MAIN.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void pop(char list[]);
void print(char list[]);
void push(char list[]);
int main(void)
{
char input[5];
char test[5];
while( strcmp("exit",input) != 0)
{
printf("Please enter a command. \n");
fgets(input,sizeof(input),stdin);
if(strcmp("pop",input)==0)
{
pop(test);
}
else if(strcmp("push",input)==0)
{
push(test);
}
else if (strcmp("print", input)==0)
{
print(test);
}
}
return 0;
}
STACK.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void pop(char list [])
{
printf("This is in the stack file in pop\n");
}
void push(char list [])
{
printf("This is in the stack file in push\n");
}
void print(char list[])
{
printf("This is in the stack file in print\n");
}
Console Output
Please enter a command.
push
This is in the stack file in push
Please enter a command.
Please enter a command.
pop
Please enter a command.
print
Please enter a command.
Please enter a command.
exit
I will suggest use of strstr() instead of strcmp(). If you use strstr() then there is no need to mention '\n' in the string to be searched.
The strstr() function finds the first occurrence of the substring needle in the string haystack.
For better understanding you can visit,
http://man7.org/linux/man-pages/man3/strstr.3.html
Code will look like,
while( strstr(input,"exit") == NULL)
{
printf("Please enter a command. \n");
memset(input,0,sizeof(input));
fgets(input,sizeof(input),stdin);
if(strstr(input,"pop"))
{
printf("pop\n");
}
else if(strstr(input,"push"))
{
printf("push\n");
}
else if (strstr(input,"print"))
{
printf("print\n");
}
}
I agree with #Govind Parmar that 5 bytes are not sufficient for input buffer. You need to declare input buffer with 7 bytes.
Three things:
The line read by fgets() will include \n at the end. Test for strcmp("word\n", input)==0.
5 is not sufficient size for input since you need to be testing for newlines ("push\n\0" is 6 bytes; "print\n\0" is 7 bytes)
You test for strcmp("exit", input) without input being initialized. This is undefined behavior. Set input to be all-zeroes before beginning your loop.
Related
I have the following piece of code which reads in a character using the read system write call:
char character;
read(STDIN_FILENO, &character, 1);
How do I detect whether character is a backspace or not? Based on that, I need to delete the last character from the console output.
You can check if it's a backspace by looking for character number 8 (ASCII). It's written in C as '\b'.
However did you forget to put your terminal in RAW mode?
To change terminal to raw mode, see this answer to a different question: https://stackoverflow.com/a/13129698/14768 ; the code you want is in function changemode.
#include<iostream>
#include<string>
#include<cstdlib>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main(){
string str;
getline(cin, str);
if(GetAsyncKeyState(8)) //checks to see if the input contained any backspaces
{
cout<<"Backspace was detected";
}
else{
cout<<"Backspace Not detected";
}
return 0;
}
or similarly using a character:
#include<iostream>
#include<string>
#include<cstdlib>
#include <windows.h>
#include <winuser.h>
using namespace std;
int main(){
char c;
cin.get(c);
if(GetAsyncKeyState(8)) //checks to see if the input contained any backspaces
{
cout<<"Backspace was detected";
}
else{
cout<<"Backspace Not detected";
}
return 0;
}
I now realised you are using C instead of C++ so all you do is change the libraries you are using.
My program is supposed to exit when the user types in exit similar to how its done in a shell. First I checked online to see if syscall could be called in a loop, but then I noticed the indices of the characters in the array are wrong. Why are these changing; when I ran the program and typed in exit I had my program shoot out the 3rd index for testing purposes and it returned 'e'. So I thought it might've been flipped and flipped all values and my exit still did not work. Any thoughts on what the underlying issue may be?
#include <stdio.h>
//Abstract: This program runs a script to emulate shell behavior
#define MAX_BIN_SIZE 100
int main() { //Memory allocation
char * entry[MAX_BIN_SIZE];
while(1)
{
printf("msh>");
fgets(entry,MAX_BIN_SIZE,stdin); //Getting user input
if(entry[0]=='t' && entry[1]=='i' && entry[2]=='x' && entry[3]=='e')
{
//printf("Exiting");
exit(0); //exit(system call)
break;
printf("Inside of exit");
}
printf("msh> you typed %s %c %c %c %c",entry,entry[3],entry[2],entry[1],entry[0]); //returning user input
}
return 0;
}
I am sorry I don't have enough reputation points to add a comment, but #lundman is correct. I don't think you need to create a pointer to entry. Also, you are checking for "exit" in the reverse order. I tried and edited the code; this seems to work:
#include <stdio.h>
//Abstract: This program runs a script to emulate shell behavior
#define MAX_BIN_SIZE 100
int main()
{ //Memory allocation
char entry[MAX_BIN_SIZE];
while(1)
{
printf("msh>");
fgets(entry,MAX_BIN_SIZE,stdin); //Getting user input
if(entry[0]=='e' && entry[1]=='x' && entry[2]=='i' && entry[3]=='t')
{
printf("Inside of exit");//printf("Exiting");
exit(0); //exit(system call)
}
printf("msh> you typed %s %c %c %c %c\n",entry,entry[3],entry[2],entry[1],entry[0]); //returning user input
}
return 0;
}
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char terminal[100];
printf("Enter cmds: ");
scanf(" %s", terminal);
if(strcmp(terminal, "help") == 0){
printf("test");
scanf(" %s", terminal); // trying to detect if a user types
// "help" the menu will pop up again
}
return 0;
}
When a user types "help", the menu pops up, (good so far). But when they type "help" again, the menu does not pop up. Does anybody know what is going on?
The initial comments hit the nail on the head here. You need to loop over new input multiple times. This can be done fairly easily.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char terminal[100];
printf("Enter cmds: ");
// this expression will return zero on invalid input, exiting the loop
while (scanf("%99s", terminal) == 1) {
// your if statement and other code you want to repeat go here.
}
}
To better encapsulate this kind of behaviour, defining some sort of function that compares strings and returns an element of an enum is a very common practice, but not required in this question.
I am trying to get name of the input, output, and data files from the array for further processing. However, I am getting a weird error or problem. So, my program is not reaching the for loop. It does not even print the statement before the for loop. However, I tried using the debugger and the program is correctly printing step by step. So, when I run it does not print and when I debug step by step it prints. That is Weird!
char *method;
method=malloc(25);
method=NULL;
char *dataFileName;
char *inputMethod;
inputMethod=malloc(25);
inputMethod=NULL;
char *inputFileName;
char *outputMethod;
outputMethod=malloc(25);
outputMethod=NULL;
char *outputFileName;
char *commandArray[]={"if=q.txt","of=output.txt"};
char**args=(char**) malloc(sizeof(char*)*256);
args=commandArray;
int i;
printf("Before second for");
for(i=0;i<2;i++)
{
printf("I am here");
if(*args[i]=='d')
{
method=strtok_r(args[i],"=",&dataFileName);
printf("The method given is %s",method);
printf("Data File Name is %s",dataFileName);
}
else if(*args[i]=='o')
{
outputMethod=strtok_r(args[i],"=",&outputFileName);
printf("The output method given is %s",outputMethod);
printf("output File Name is %s",outputFileName);
}
else
{
inputMethod=strtok_r(args[i],"=",&inputFileName);
printf("The input method given is %s",inputMethod);
printf("Input File Name is %s",inputFileName);
}
}
if(method==NULL)
{
dataFileName=malloc(256);
printf("Please Enter A File Name");
scanf("%255s",dataFileName);
printf("%s",dataFileName);
}
if((inputMethod==NULL)||(outputMethod==NULL) )
{
char* array[]={"stdin","stdout"};
if(inputMethod==NULL)
inputMethod=array[0];
if(outputMethod==NULL)
outputMethod=array[1];
}
I am developing using Netbeans in C. The above code is written inside main. Thanks!
i intentionally left the previous answer because understanding memory allocation is trivial in programming in c specially. and as i see you have a big issue with that.
but still you have issue in nearly every thing. in my actual answer, i will try to simplify you how to use strtok, to split string and parse it. i guess this is the second main problem with your code.
the code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char commandArray[][256]={
"if=q.txt",
"of=output.txt"
};
char infile[256], outfile[256];
for(int i=0; i<2;i++){
char *ptr,*cmd;
cmd=commandArray[i];
ptr=NULL;
printf("parsing command '%s'\n",cmd);
cmd=strtok(cmd,"=");
ptr=strtok(NULL,"=");
if(!cmd){
printf("Error parsing the string '%s'\n",commandArray[i]);
exit(1);
}
if (strcmp(cmd,"if")==0){
strcpy(infile,ptr);
}
else if (strcmp(cmd,"of")==0){
strcpy(outfile,ptr);
}
else{
printf("unknow token '%s'\n",cmd);
exit(1);
}
}
printf(
"\n\n"
"input file: '%s'\n"
"output file: '%s'\n"
"\n\n",
infile,outfile);
return 0;
}
the main problem is this:
char *method;
method=malloc(25);//allocating space for 25 char
method=NULL; // throwing up the allocation without freeing it;
// now the allocation is lost
// now method is useless (it is null)
I'm doing school assignments. Writing a simple command line interpreter. One of the functions is to clear screen. Its is called cmd_clr. For that, I'm trying to use clear() function from curses.h library. The problem is clear() returns -1 value, for some reason. Here is the code:
#include <stdio.h> /* Standard IO functions */
#include <string.h> /* String library, might be useful */
#include <dirent.h> /* Directory entry API */
#include <curses.h> /* Useful for screen control */
#include <unistd.h> /* Process management API */
/* Global constants */
#define true 1
#define false 0
/* Global variables, structures */
char pwd[512];
char shell[512];
void cmd_dir(char []);
void cmd_cd(char []);
void cmd_clr(void);
int main (void)
{
char prompt[512] = {":#"};
char command[512];
char temp[512];
char* token;
while (true)
{
/* Print command prompot, including PWD */
printf("%s%s ", pwd, prompt);
/* Get command input */
gets(command);
/* Isolate the command token */
strcpy(temp, command);
token = strtok (temp, " ");
void cmd_dir(char directory[])
{
printf("shell command: dir\n");
token = strtok(NULL, " "); //to get the directory
execlp("/bin/ls","ls", "-l", token, NULL);
}
void cmd_cd(char directory[])
{
printf("shell command: cd\n");
token = strtok(NULL, " "); //to get the directory
chdir(token);
system("pwd");//to print the current directory
}
void cmd_clr(void)
{
printf("shell command: clr\n");
int tv = clear();
printf("%d", tv);
}
if (strcmp(token, "dir") == 0)
{
cmd_dir(command);
}
else if (strcmp(token, "cd") == 0)
{
cmd_cd(command);
}
else if (strcmp(token, "clr") == 0)
{
cmd_clr();
}
}
}
The output is:
mekhron#ubuntu:~/folder4$ gcc -o test test.c -lncurses
mekhron#ubuntu:~/folder4$ ./test
:# clr
shell command: clr
-1:# ^C
mekhron#ubuntu:~/folder4$
The curses clear() function, like most curses functions, cannot be used without first calling initscr().
Judging by the rest of your code, you probably don't want to be using curses or ncurses anyway. curses is designed to manage your entire screen. It's incompatible with the other I/O you're doing. The curses clear() function doesn't just clear the screen; it clears the curses internal representation of the state of your screen. Your actual screen won't be cleared until you call refresh().
If you just want to clear the screen immediately, you should find another way to do it. The clear command should do it; just call system("clear");.
One more thing I need to point out: You're using the gets() function. Don't. gets() cannot be used safely; since it doesn't let you specify the size of the array into which you're reading, it cannot prevent a long input line from overflowing your array and clobbering other memory. The fgets() function is a little more difficult to use (in particular, it stores the trailing '\n' in your array), but it can be used safely.
And assuming your compiler supports it, you can drop the definitions of false and true and just add #include <stdbool.h> to the top of your program.