Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7619013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:33:03+00:00 2026-05-31T03:33:03+00:00

Basically I need to open and read a list of files I get from

  • 0

Basically I need to open and read a list of files I get from another command.
For each line of output of popen
open a file usen ifstream.open

it compiles and if I put the file name directly it works fine, but it doesn’t do anything when using popen output. I’ve seen questions like this but none of this particular way of giving filenames.

here’s the code:

#include <iostream>
#include <sqlite3.h>
#include <stdio.h>
#include <fstream>

using namespace std;


int main () {

    ifstream singlefile;
    FILE *filelist;
    char filename[512];
    string progline;

    if(!(filelist = popen("find `pwd` -name \"*.js\"", "r"))){
        return 1;
    }

    while( fgets(filename, sizeof(filename), filelist)!=NULL)
    {
        cout << filename;
        singlefile.open(filename, ifstream::in);
        while ( singlefile.good() )
        {
            getline (singlefile,progline);
            cout << progline << endl;
        }

        singlefile.close();
    }

    pclose(filelist);

  return 0;

}

next step would be not open each file inside the loop but to store the file list and then open each file.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:33:05+00:00Added an answer on May 31, 2026 at 3:33 am

    fgets keeps the trailing newline, resulting in a filename of a non-existing file. Also the stream state is only updated after reading. If I replace the while body with the following code, it works for me:

      cout << filename;
      size_t len = strlen(filename);
      // chop off trailing newline
      if (len > 1 && filename[len - 1] == '\n') filename[len - 1] = 0;
      singlefile.open(filename, ifstream::in);
      while ( getline(singlefile, progline) )
        {
          cout << progline << endl;
        }
    
      singlefile.close();
    

    If you actually want to iterate through a list of files, I’d use Boost.Filesystem, which has a nice C++ interface, works for all filenames (even for those with newlines), and is platform-independent.

    If this actually is only an example and your actual command is not find, there is still some room for simplification. Here is a suggestion that uses Boost.Iostreams to get rid of most of the C function calls (it would be great to have a device source reading from a process’s standard output, but Boost.Iostreams lacks that):

    #include <cstdio>
    #include <iostream>
    #include <fstream>
    #include <stdexcept>
    #include <string>
    
    #include <stdio.h>
    
    #include <boost/noncopyable.hpp>
    #include <boost/iostreams/stream.hpp>
    #include <boost/iostreams/device/file_descriptor.hpp>
    
    using namespace std;
    namespace io = boost::iostreams;
    
    class Popen: private boost::noncopyable {
    public:
      explicit Popen(const char* command):
        m_stream(popen(command, "r")) {
        if (!m_stream) throw runtime_error("popen failed");
      }
    
      ~Popen() {
        pclose(m_stream);
      }
    
      FILE* stream() const {
        return m_stream;
      }
    
    private:
      FILE* m_stream;
    };
    
    
    int main() {
      Popen pipe_wrapper("find `pwd` -name \"*.cpp\"");
      io::file_descriptor_source pipe_device(fileno(pipe_wrapper.stream()), io::never_close_handle);
      io::stream<io::file_descriptor_source> pipe_stream(pipe_device, 0x1000, 0x1000);
      string filename;
      while (getline(pipe_stream, filename)) {
        cout << filename << endl;
        ifstream file_stream(filename.c_str(), ifstream::in);
        string progline;
        while (getline(file_stream, progline)) {
          cout << progline << endl;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I need to get older version of a file in the repository without
So im reading from a file like so. f = open(log.txt, 'w+r') f.read() So
So i basically need my program to open a file and do something. When
I want to read a Open Office Document spreadsheet (.ods file) from my android
I basically need to get user input: gets.chomp(input?) And then to convert the given
Basically I need to capture the video from videocamera do some processing on frames
EDIT: More detail here. Basically if you 1. open a tab1 to url1 (GET
Basically I have 14800x8 matrix that has been extracted from matlab as CSV file
Basically the question above, you don't need to read the lot below, just additional
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.