multiple reading from folder










-1















I need to read all files from the folder and write to buffer. All files have the same name except the last portion (file_0000.mdf, file_0001.mdf,....file_9999.mdf). How i can read all the files? testFolder contains all the files. If i go with for_loop,it will start with 0 but my test_files start from 0000 and so on. Also, i need file size of each individual test file. My logic is wrong, but i do not know how to fix it. Some updated code is also given below the first approach.



#include <iostream>
#include <fstream>
#include <string>
int main()

std::string path="C:\testFolder\";
std::string constName="file_";
std::string lastName = ".mdf";
std::fstream InputStream;
std::string fileWithPath;

for (int i=0; i <9999;i++)

fileWithPath=path+constName+std::to_string(static_cast<long long>
(i)+lastName;
InputStream.open(fileWithPath,std::ios::binary);
long InputFileSize= InputStream.tellg();


return 0;



Short update by using boost/filesystem. Need comments on this approach.



#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>

std::string SourceFolder="C:\testFolder\";
path mDirectory(SourceFolder);

std::cout<<"Directory includes the following files"
if(is_directory(mDirectory)){
for(auto testFile=mDirectory.begin();testFile!=mDirectory.end();testFile++)
std::cout<< testFile->string()<<std::endline;










share|improve this question
























  • You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

    – Werner Henze
    Nov 15 '18 at 15:16











  • Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

    – aschepler
    Nov 16 '18 at 15:07















-1















I need to read all files from the folder and write to buffer. All files have the same name except the last portion (file_0000.mdf, file_0001.mdf,....file_9999.mdf). How i can read all the files? testFolder contains all the files. If i go with for_loop,it will start with 0 but my test_files start from 0000 and so on. Also, i need file size of each individual test file. My logic is wrong, but i do not know how to fix it. Some updated code is also given below the first approach.



#include <iostream>
#include <fstream>
#include <string>
int main()

std::string path="C:\testFolder\";
std::string constName="file_";
std::string lastName = ".mdf";
std::fstream InputStream;
std::string fileWithPath;

for (int i=0; i <9999;i++)

fileWithPath=path+constName+std::to_string(static_cast<long long>
(i)+lastName;
InputStream.open(fileWithPath,std::ios::binary);
long InputFileSize= InputStream.tellg();


return 0;



Short update by using boost/filesystem. Need comments on this approach.



#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>

std::string SourceFolder="C:\testFolder\";
path mDirectory(SourceFolder);

std::cout<<"Directory includes the following files"
if(is_directory(mDirectory)){
for(auto testFile=mDirectory.begin();testFile!=mDirectory.end();testFile++)
std::cout<< testFile->string()<<std::endline;










share|improve this question
























  • You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

    – Werner Henze
    Nov 15 '18 at 15:16











  • Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

    – aschepler
    Nov 16 '18 at 15:07













-1












-1








-1








I need to read all files from the folder and write to buffer. All files have the same name except the last portion (file_0000.mdf, file_0001.mdf,....file_9999.mdf). How i can read all the files? testFolder contains all the files. If i go with for_loop,it will start with 0 but my test_files start from 0000 and so on. Also, i need file size of each individual test file. My logic is wrong, but i do not know how to fix it. Some updated code is also given below the first approach.



#include <iostream>
#include <fstream>
#include <string>
int main()

std::string path="C:\testFolder\";
std::string constName="file_";
std::string lastName = ".mdf";
std::fstream InputStream;
std::string fileWithPath;

for (int i=0; i <9999;i++)

fileWithPath=path+constName+std::to_string(static_cast<long long>
(i)+lastName;
InputStream.open(fileWithPath,std::ios::binary);
long InputFileSize= InputStream.tellg();


return 0;



Short update by using boost/filesystem. Need comments on this approach.



#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>

std::string SourceFolder="C:\testFolder\";
path mDirectory(SourceFolder);

std::cout<<"Directory includes the following files"
if(is_directory(mDirectory)){
for(auto testFile=mDirectory.begin();testFile!=mDirectory.end();testFile++)
std::cout<< testFile->string()<<std::endline;










share|improve this question
















I need to read all files from the folder and write to buffer. All files have the same name except the last portion (file_0000.mdf, file_0001.mdf,....file_9999.mdf). How i can read all the files? testFolder contains all the files. If i go with for_loop,it will start with 0 but my test_files start from 0000 and so on. Also, i need file size of each individual test file. My logic is wrong, but i do not know how to fix it. Some updated code is also given below the first approach.



#include <iostream>
#include <fstream>
#include <string>
int main()

std::string path="C:\testFolder\";
std::string constName="file_";
std::string lastName = ".mdf";
std::fstream InputStream;
std::string fileWithPath;

for (int i=0; i <9999;i++)

fileWithPath=path+constName+std::to_string(static_cast<long long>
(i)+lastName;
InputStream.open(fileWithPath,std::ios::binary);
long InputFileSize= InputStream.tellg();


return 0;



Short update by using boost/filesystem. Need comments on this approach.



#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>

std::string SourceFolder="C:\testFolder\";
path mDirectory(SourceFolder);

std::cout<<"Directory includes the following files"
if(is_directory(mDirectory)){
for(auto testFile=mDirectory.begin();testFile!=mDirectory.end();testFile++)
std::cout<< testFile->string()<<std::endline;







c++ visual-c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:09









genpfault

42.5k954100




42.5k954100










asked Nov 15 '18 at 15:10









Qaribullah Khan YousafzaiQaribullah Khan Yousafzai

143




143












  • You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

    – Werner Henze
    Nov 15 '18 at 15:16











  • Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

    – aschepler
    Nov 16 '18 at 15:07

















  • You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

    – Werner Henze
    Nov 15 '18 at 15:16











  • Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

    – aschepler
    Nov 16 '18 at 15:07
















You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

– Werner Henze
Nov 15 '18 at 15:16





You call fstream::open without closing. That won't work. Regarding formatting the number check std::stringstream, setfill and setw. If you can use C++17, then take a look at std::filesystem::directory_iterator.

– Werner Henze
Nov 15 '18 at 15:16













Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

– aschepler
Nov 16 '18 at 15:07





Please don't edit a question in a way so that existing answers don't make sense. You can open another question instead.

– aschepler
Nov 16 '18 at 15:07












2 Answers
2






active

oldest

votes


















0














Plain integers doesn't have leading zeros. To get leading zeros you need to use some other way of formatting your file-names. For example by using std::ostringstream and standard I/O manipulators like std::setw and std::setfill:



std::ostringstream oss;
oss << path << constName << std::setw(4) << std::setfill('0') << i << lastName;
fileWithPath = oss.str();





share|improve this answer






























    0














    You may use FindFirstFile() and FindNextFile() functions scanning the files in the directory using wildcard, i.e. "C:Datafile_???.mdf".
    Returned WIN32_FIND_DATA will contain also a file size.



    Take a look on the complete example "Listing the Files in a Directory".



    Once a file is listed, you may read its content with ifstream as usually.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322431%2fmultiple-reading-from-folder%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Plain integers doesn't have leading zeros. To get leading zeros you need to use some other way of formatting your file-names. For example by using std::ostringstream and standard I/O manipulators like std::setw and std::setfill:



      std::ostringstream oss;
      oss << path << constName << std::setw(4) << std::setfill('0') << i << lastName;
      fileWithPath = oss.str();





      share|improve this answer



























        0














        Plain integers doesn't have leading zeros. To get leading zeros you need to use some other way of formatting your file-names. For example by using std::ostringstream and standard I/O manipulators like std::setw and std::setfill:



        std::ostringstream oss;
        oss << path << constName << std::setw(4) << std::setfill('0') << i << lastName;
        fileWithPath = oss.str();





        share|improve this answer

























          0












          0








          0







          Plain integers doesn't have leading zeros. To get leading zeros you need to use some other way of formatting your file-names. For example by using std::ostringstream and standard I/O manipulators like std::setw and std::setfill:



          std::ostringstream oss;
          oss << path << constName << std::setw(4) << std::setfill('0') << i << lastName;
          fileWithPath = oss.str();





          share|improve this answer













          Plain integers doesn't have leading zeros. To get leading zeros you need to use some other way of formatting your file-names. For example by using std::ostringstream and standard I/O manipulators like std::setw and std::setfill:



          std::ostringstream oss;
          oss << path << constName << std::setw(4) << std::setfill('0') << i << lastName;
          fileWithPath = oss.str();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 15:15









          Some programmer dudeSome programmer dude

          303k25265426




          303k25265426























              0














              You may use FindFirstFile() and FindNextFile() functions scanning the files in the directory using wildcard, i.e. "C:Datafile_???.mdf".
              Returned WIN32_FIND_DATA will contain also a file size.



              Take a look on the complete example "Listing the Files in a Directory".



              Once a file is listed, you may read its content with ifstream as usually.






              share|improve this answer



























                0














                You may use FindFirstFile() and FindNextFile() functions scanning the files in the directory using wildcard, i.e. "C:Datafile_???.mdf".
                Returned WIN32_FIND_DATA will contain also a file size.



                Take a look on the complete example "Listing the Files in a Directory".



                Once a file is listed, you may read its content with ifstream as usually.






                share|improve this answer

























                  0












                  0








                  0







                  You may use FindFirstFile() and FindNextFile() functions scanning the files in the directory using wildcard, i.e. "C:Datafile_???.mdf".
                  Returned WIN32_FIND_DATA will contain also a file size.



                  Take a look on the complete example "Listing the Files in a Directory".



                  Once a file is listed, you may read its content with ifstream as usually.






                  share|improve this answer













                  You may use FindFirstFile() and FindNextFile() functions scanning the files in the directory using wildcard, i.e. "C:Datafile_???.mdf".
                  Returned WIN32_FIND_DATA will contain also a file size.



                  Take a look on the complete example "Listing the Files in a Directory".



                  Once a file is listed, you may read its content with ifstream as usually.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 15:23









                  sergeserge

                  70148




                  70148



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322431%2fmultiple-reading-from-folder%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto