How to read from file 1 to X(unknown) files
Greetings to all experts here. I have been pondering this issue for 2 days and wonder any 1 can offer me a simple solution to it.
For example,
There are X numbers of files. First file starts at 000001.txt and the last file may end at 001001.txt . But the problem is I do not know the number of files i am reading so I can't use For loop.
But since the first file is 000001.txt and the next file always increment by one till the last file is reached.
How can i know if I have reached the last file using ifStream and how do i actually put the zeros in front of the file name?"
I sincerely thank any kind folk who can provide me guidance in this T_T.
[701 byte] By [
lone2082] at [2007-11-20 11:02:38]

# 1 Re: How to read from file 1 to X(unknown) files
I believe this is operating system dependant, so you would need to write code to find all the files in a directory using an OS library. There are also the Boost libraries which have OS independant code for file and folder handling.
# 2 Re: How to read from file 1 to X(unknown) files
Start with 0001.txt, increment the names and stop when you try to open a file that doesn't exist. A while loop would be a better choice.
GCDEF at 2007-11-9 1:26:00 >

# 3 Re: How to read from file 1 to X(unknown) files
This is sloppy and crude at best, but it's 1:44 am here and I already had the code to convert a numeric variable to a string so...
#include <iostream>
#include <fstream>
#include <sstream>
std::string ToString(unsigned int x)
{
std::ostringstream o;
if (!(o << x))
return "Error in ToString.";
return o.str();
}
int main()
{
const std::string DIRECTORY = "C:/read_files/";
const std::string EXTENSION = ".txt";
std::ifstream read;
std::string buffer = "?";
std::string file_name = "?";
unsigned int file_no = 0;
bool valid = false;
do
{
if(file_no >= 1000)
file_name = DIRECTORY + "f_" + ToString(file_no) + EXTENSION;
else if(file_no >= 100)
file_name = DIRECTORY + "f_0" + ToString(file_no) + EXTENSION;
else if(file_no >= 10)
file_name = DIRECTORY + "f_00" + ToString(file_no) + EXTENSION;
else
file_name = DIRECTORY + "f_000" + ToString(file_no) + EXTENSION;
read.open(file_name.c_str());
if(!read.fail())
{
valid = true;
std::cout << file_name << ":" << std::endl;
while(!read.eof())
{
read >> buffer;
// Print or store values...
std::cout << buffer;
}
read.close();
}
else
valid = false;
++file_no;
} while(valid);
read.close();
return 0;
}
Like I said, I am aware that it's really dodgy, but it definitely solves one of your problems. :)
How can i know if I have reached the last file using ifStreamread.fail().
Note: You'll obviously have to change DIRECTORY and change the file prefix to suit your needs.
# 4 Re: How to read from file 1 to X(unknown) files
You could use sprintf with a width specifier to left fill with zeros.
GCDEF at 2007-11-9 1:28:07 >

# 5 Re: How to read from file 1 to X(unknown) files
I've seen sprintf and printf etc. around and I've always wondered what it is... I learned to use std::cout from the beginning... so is it a C function? I'm guessing that whatever it is, it's portable, because my friend was doing an engineering course and he apparently learned that and didn't get taught cout...?
# 6 Re: How to read from file 1 to X(unknown) files
A similar solution is to use a stringstream and use setw() to set the
width and setfill() to put the zeroes at the start.
Here is one solution ... nut using a class instead of a function would
be better.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
bool FileExists(std::ifstream & in)
{
using namespace std;
static int n = 1;
in.clear();
if (in.is_open()) in.close();
stringstream ss;
ss << setw(6) << setfill('0') << n++ << ".txt";
string fname = "c:\\" + ss.str();
in.open( fname.c_str() );
return in.is_open();
}
int main()
{
std::ifstream in;
while (FileExists(in))
{
// read from in
std::cout << "processing next file\n";
}
return 0;
}
# 7 Re: How to read from file 1 to X(unknown) files
This should suit your needs:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int FileExists(string file)
{
ifstream ExistTester(file.c_str());
if(!ExistTester)
{
//File doesn't exist
return 1;
}
return 0;
ExistTester.close();
}
string IntToString(int NumberToConvert)
{
stringstream ss;
string str;
ss << NumberToConvert;
ss >> str;
return str;
}
int main()
{
int FileCounter = 1;
string file = "00000.txt";
while(FileExists(file) == 0)
{
if(FileCounter <= 10000)
{
file = "0000" + IntToString(FileCounter) + ".txt";
}
else if(FileCounter <= 1000)
{
file = "000" + IntToString(FileCounter) + ".txt";
}
else if(FileCounter <= 100)
{
file = "00" + IntToString(FileCounter) + ".txt";
}
else if(FileCounter <= 10)
{
file = "0" + IntToString(FileCounter) + ".txt";
}
else
{
file = IntToString(FileCounter) + ".txt";
}
cout << "File Exists" << endl;
FileCounter++;
//You would obviously read from the file here
}
cout << "Finished reading all the files" << endl;
cin.get();
}
BageDevimo,
# 8 Re: How to read from file 1 to X(unknown) files
This code certainly look amazing by padding it with zeros and making it short. Thanks a lot!
A similar solution is to use a stringstream and use setw() to set the
width and setfill() to put the zeroes at the start.
Here is one solution ... nut using a class instead of a function would
be better...