Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

325
Visualizações
How to use _popen() with string input?

I try to use _popen() to view files. When I write one word command like ipconfig or dir it works fine. However when I try something with space in it like cd .. or cd C:\, it won't work(it won't change the directory), the program just does nothing. I don't get a error. I'm on a Windows machine. I don't know if it has anything to do with the spaces. I tried something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

int main()
{
    while (1)
    {
        char buff[500];
        char input[100];
    
        printf("Command: ");
        fgets(input, 100, stdin);
    
        FILE* f = _popen(input, "r");
    
        while (fgets(buff, 200, f) != NULL)
        {
            printf("%s", buff);
        }
    
        _pclose(f);
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Thats because cd does not produce any output. In other words, does not write to stdout

Try "dir c:" and see if you can read from the pipe

Command: cd ..
Command: dir c:
 Volume in drive C has no label.
 Volume Serial Number is F4DC-394E

 Directory of popen

15-03-2022  15:28    <DIR>          .
15-03-2022  15:28    <DIR>          ..
15-03-2022  15:28    <DIR>          Debug
15-03-2022  15:28               520 popen.cpp
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda