C++ - my program stops running `freopen` function from <cstdio> - c++

In my main.cpp:
#include <cstdio>
#include "hashtable.h"
int main() {
printf("1hello");
freopen("2.txt", "w", stdout);
printf("2hello");
freopen("1.txt", "r", stdin);
printf("3hello");
int type;
char buffer[1000];int data;
hashtable table(10000, new naive_hashing(), new linear_probe());
while (true) {
scanf("%d", &type);
if (type == 0) {
scanf("%s", buffer);scanf("%d", &data);
table.insert(hash_entry(buffer, data));
}
else if (type == 1) {
scanf("%s", buffer);
printf("%d\n", table.query(buffer));
}
else break;
}
return 0;
}
1.txt:
0 dhu_try 3039
0 shirin 3024
0 SiamakDeCode 2647
0 huanghansheng 233
1 dhu
1 dhu_try
1 shirin
1 siamakdecode0
1 huanghansheng
2
output:
1hello
As you can see the program paused after it entered the first freopen function. I have checked the document already and still yet cannot find the reason why it stopped running. Can anyone help me please? :pleading_face:

You redirected all output to stdout to the file 2.txt when you did
freopen("2.txt", "w", stdout);
That's why no printf outputs are shown on the console after that freopen. Look in the file 2.txt and you will most probably see the output there - if the freopen succeeded. Always check if functions that can fail have succeeded.

Related

How does append becomes prepend when forget to fclose the file?

I found this interesting behaviour in the following c++ program.
#include <fstream>
void append(std::string path, int i){
FILE *out = fopen(&path[0], "a");
fprintf(out, "%d\n", i);
//fclose(out);
}
int main(){
for(int i=0; i<3; i++) append("./text.txt",i);
return 0;
}
where I forget to close the file in append.
Then the content of text.txt after one execution will be
2
1
0
other than
0
1
2
Once I fclose the file correctly, this effect will disappear.
I wonder how does this happen? BTW, I'm running it on a Ubuntu machine.

Why does using process substitution here cause a hang?

I have a program which needs to launch other programs, possibly substituting their stdio with files and pipes. While it appears to "work" in that the sub-process does get it's I/O from the source pipe, unfortunately, it also causes a hang. The sub-process is seemingly never getting an EOF.
Here is a minimal reproduction of the code, why does this hang after printing "Hello World\n"?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
switch (pid_t pid = fork()) {
case 0: {
// in child
// replace the child's stdin with whatever filename is given as argv[1]
freopen(argv[1], "r+b", stdin);
// construct an argv array for to exec, no need for anything except
// argv[0] since we want it to use stdin
char path[] = "/bin/cat";
char *args[] = {path, NULL};
// run it!
execv(args[0], args);
abort(); // we should never get here!
}
case -1:
// error
return -1;
default: {
// in parent, just wait for the sub-process to terminate
int status;
const auto r = waitpid(pid, &status, __WALL);
if (r == -1) {
perror("waitpid");
return -1;
}
break;
}
}
}
# runs printf creating a pipe, which is then passed as the argv of my test program
./test >(printf "Hello\n")
./test <(printf "Hello\n")
Switch to >(...) to <(...) to read from printf rather than write to it.
freopen(argv[1], "rb", stdin);
Don't use r+. You're only reading from the file, so make it r.

Equivalent of C++ cin.tie() for C stdio? [duplicate]

I'm using Eclipse to code in C/C++ and I'm struggling with what might be something pretty easy. In my code below I use printf() and after scanf(). Althougth printf is written before scanf() the output differs. I was able to find out something about similar issue here. But I wasn't able to solve it. Any ideas?
Code:
#include <stdio.h>
int main()
{
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Expected output:
Enter a number:1
1
Instead I get:
1
Enter a number:1
Your output is being buffered.
You have 4 options:
explicit flush
fflush after each write to profit from the buffer and still enforce the desiredbehavior/display explicitly.
fflush( stdout );
have the buffer only buffer lines-wise
useful for when you know that it is enough to print only complete lines
setlinebuf(stdout);
disable the buffer
setbuf(stdout, NULL);
disable buffering in your console through what ever options menu it provides
Examples:
Here is your code with option 1:
#include <stdio.h>
int main() {
int myvariable;
printf("Enter a number:");
fflush( stdout );
scanf("%d", &myvariable);
printf("%d", myvariable);
fflush( stdout );
return 0;
}
Here is 2:
#include <stdio.h>
int main() {
int myvariable;
setlinebuf(stdout);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
and 3:
#include <stdio.h>
int main() {
int myvariable;
setbuf(stdout, NULL);
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
Ok, so finally I used something similar to what #zsawyer wrote as an option labelled 3.
In my code I inserted this line:
setvbuf(stdout, NULL, _IONBF, 0);
As a first line in main():
#include <stdio.h>
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
I got it from here.

C++: how to input text in console and keep it at the bottom

The description of the problems:
I code a program that prints out some information frequently.
I want to input some commands during the program running.
std::out will flush out my input.
For examples:
>>> ./my_program
[sensorA] initial...ok
[sensorB] initial...ok
ge //!< I want to input 'get' here but the next output break it
[motorA] self-check...ok
t //!< break it into two spice
Expected:
>>> ./my_program
[sensorA] initial...ok
[sensorB] initial...ok
[motorA] self-check...ok
get //!< always fixed here whenever I input
Thanks a lot !
Appreciation
Firstly, I show my Great appreciation to Sam Varshavchik
Primary Result I found
Sam gave me the hints to use Curses Library. I read the doc and now finish the basic function.
My method is to create to sub-windows(output_win and input_win). User input show in input_win whereas program information print on output_win.
Let me share my code:
#include <iostream>
#include <string>
#include <curses.h>
#include <thread>
#include <atomic>
#include <chrono>
#include <unistd.h>
using namespace std;
WINDOW* win;
WINDOW* output_win;
WINDOW* input_win;
int row = 0, col = 0;
std::atomic<bool> flag(false);
string buf;
void ninit()
{
win = initscr();
getmaxyx(win, row, col);
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
refresh();
}
void nprintf(string str)
{
touchwin(win);
str += '\n';
wprintw(output_win, str.c_str());
wrefresh(output_win);
}
void nprintf(const char* fmt, ...)
{
touchwin(win);
va_list ap;
va_start(ap, fmt);
vw_printw(output_win, fmt, ap);
va_end(ap);
wrefresh(output_win);
}
void nmonitor()
{
while(1)
{
char x = getch();
if(x != '\r')
{
touchwin(win);
buf += x;
waddch(input_win, x);
}
else
{
nprintf(buf);
touchwin(input_win);
flag = true;
wclear(input_win);
}
wrefresh(input_win);
}
}
string nget()
{
while(!flag)
usleep(100);
string cmd = buf;
flag = false;
buf = "";
return cmd;
}
////////////////////////////////
void print_thread()
{
while(1)
{
static int i = 0;
nprintf("no.%d\n", i++);
usleep(100000);
}
}
int main()
{
ninit();
fflush(stdin);
output_win = subwin(win, row - 1, col, 0, 0);
scrollok(output_win, true);
input_win = subwin(win, 1, col, row - 1, 0);
std::thread pthr(print_thread);
std::thread nthr(nmonitor);
string cmd;
while(1)
{
cmd = nget();
if(cmd == "quit")
break;
else
nprintf("[info] You input: %s\n", cmd.c_str());
}
getch();
endwin();
}
Environment Configure and Build
For Mac OSX:
brew install ncurses
For Ubuntu:
sudo apt-get install libcurses5-dev
To build:
g++ f04.cpp - f04 -lcurses # I try for 4 times so name it f04
Some bugs
Actually it has some bugs, here I found:
when you input backspace, it will not delete a char but show a special char;
after inputting enter, output_win sometimes show some strange words.
I am a beginner and may need help.
(Maybe I will solve them soon.)
May it can help others indeed.
You can try the following:
Before you print something, read from stdin everything the user
entered so far.
If there was something in stdin, print '\r' (so that the next output will overwrite the text entered by the user).
Print your output.
Print the text the user entered so far (but without '\n').
Please also see:
Rewinding std::cout to go back to the beginning of a line

How to clear input in C++ Console commands [duplicate]

Is there any way to hide user input when asked for in C?
For example:
char *str = malloc(sizeof(char *));
printf("Enter something: ");
scanf("%s", str);getchar();
printf("\nYou entered: %s", str);
// This program would show you what you were writing something as you wrote it.
// Is there any way to stop that?
Another thing, is how can you only allow certain characters?
For example:
char c;
printf("Yes or No? (y/n): ");
scanf("%c", &c);getchar();
printf("\nYou entered: %c", c);
// No matter what the user inputs, it will show up, can you restrict that only
// showing up if y or n are entered?
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
int set_disp_mode(int fd,int option)
{
int err;
struct termios term;
if(tcgetattr(fd,&term)==-1){
perror("Cannot get the attribution of the terminal");
return 1;
}
if(option)
term.c_lflag|=ECHOFLAGS;
else
term.c_lflag &=~ECHOFLAGS;
err=tcsetattr(fd,TCSAFLUSH,&term);
if(err==-1 && err==EINTR){
perror("Cannot set the attribution of the terminal");
return 1;
}
return 0;
}
int getpasswd(char* passwd, int size)
{
int c;
int n = 0;
printf("Please Input password:");
do{
c=getchar();
if (c != '\n'||c!='\r'){
passwd[n++] = c;
}
}while(c != '\n' && c !='\r' && n < (size - 1));
passwd[n] = '\0';
return n;
}
int main()
{
char *p,passwd[20],name[20];
printf("Please Input name:");
scanf("%s",name);
getchar();
set_disp_mode(STDIN_FILENO,0);
getpasswd(passwd, sizeof(passwd));
p=passwd;
while(*p!='\n')
p++;
*p='\0';
printf("\nYour name is: %s",name);
printf("\nYour passwd is: %s\n", passwd);
printf("Press any key continue ...\n");
set_disp_mode(STDIN_FILENO,1);
getchar();
return 0;
}
for linux
For the sake of completeness: There is no way to do this in C. (That is, standard, plain C without any platform-specific libraries or extensions.)
You did not state why you wanted to do this (or on what platform), so it's hard to make relevant suggestions. You could try a console UI library or a GUI library. You could also try your platform's console libraries. (Windows, Linux)