gets and fgets have problem.

These codes have no problem and do well, but, I want to use "gets(buf);" instead of "cin.getline(buf, sizeof(buf));". However, when substituting "cin.getline(buf, sizeof(buf));" for "gets(buf);" or "fgets(buf, sizeof(buf), stdin);", a problem occurs in "popentest.cpp".

What is the problem in popentest.cpp? and, How can I solve?

p.s. In test.cpp, I must use gets().

// test.cpp
// make for Test
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char *trim(char *str){
char *blanks = (char *)" \n\t\v\r";
size_t lIdx = strspn(str, blanks);

for(int i=(int)strlen(str)-lIdx; i>=(int)lIdx; i--){
if(str[i] != ' ' && str[i] != '\r' && str[i] != '\t' && str[i] != '\n' && str[i] != '\v'){
str[i+1] = 0;
break;
}
}
return (str + lIdx);
}
int main(){
printf("======");
while(true){
char buf[1024];
cin.getline(buf, sizeof(buf));
if(strcmp("quit", buf) == 0) break;
printf("%s", buf);
}
string ar = "2222222";
cout << sizeof(ar) << endl;
return 0;
}

// Popentest.cpp
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define READ 0
#define WRITE 1
#define BUFSIZE 10000

int p_stdin[2], p_stdout[2];
pid_t pid;
pid_t popen2(const char *command, int *infp, int *outfp)
{
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;

pid = fork();

if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);

execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}
//sleep(2);
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];

if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];

return pid;
}

int main(int argc, char **argv)
{
int infp, outfp;
char buf0[BUFSIZE], buf1[BUFSIZE], buf2[BUFSIZE];

if (popen2("./Test", &infp, &outfp) <= 0)
{
exit(1);
}
printf("$\n");
*buf0 = '\0';
read(outfp, buf0, BUFSIZE);
printf("buf = '%s'\n", buf0);

write(infp, " was \n", 6);
printf("$$\n");
*buf1 = '\0';
read(outfp, buf1, BUFSIZE);
printf("buf = '%s'\n", buf1);

write(infp, "\the is\n", 7);
printf("$$$\n");
*buf2 = '\0';
read(outfp, buf2, BUFSIZE);
printf("buf = '%s'\n", buf2);

write(infp, "quit\n", 5);
close(infp);
close(outfp);
return 0;
}
Last edited on
[code] "Please use code tags" [/code]
Never use gets. RTFM to know why.
Don't cast away compiler errors/warnings. const char *blanks = " \n\t\v\r";

p.s. In test.cpp, I must use gets().
¿why?
a problem occurs in "popentest.cpp".
http://www.cplusplus.com/forum/articles/40071/#msg218019
Never use gets

Quote, do not use it. Why?
Try something like this:
1
2
char LittleBuffer[2];
gets(LittleBuffer);

and input a long string (longer than 2 chars). It will fail (Buffer too short assertion, probably).
Because when you get the string, your program does not know how many chars LittleBuffer can store. On MS Visual Studio you can use gets_s and input its size too, but:
1. It requires the .NET Framework installed
2. It's MSVS-only, as of now, as far as i know.
Topic archived. No new replies allowed.