multiple reading from folder
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++
add a comment |
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++
You callfstream::open
without closing. That won't work. Regarding formatting the number checkstd::stringstream
,setfill
andsetw
. If you can use C++17, then take a look atstd::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
add a comment |
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++
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++
c++ visual-c++
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 callfstream::open
without closing. That won't work. Regarding formatting the number checkstd::stringstream
,setfill
andsetw
. If you can use C++17, then take a look atstd::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
add a comment |
You callfstream::open
without closing. That won't work. Regarding formatting the number checkstd::stringstream
,setfill
andsetw
. If you can use C++17, then take a look atstd::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
add a comment |
2 Answers
2
active
oldest
votes
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();
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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();
add a comment |
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();
add a comment |
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();
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();
answered Nov 15 '18 at 15:15
Some programmer dudeSome programmer dude
303k25265426
303k25265426
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 15:23
sergeserge
70148
70148
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You call
fstream::open
without closing. That won't work. Regarding formatting the number checkstd::stringstream
,setfill
andsetw
. If you can use C++17, then take a look atstd::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