thomas
thomas

Reputation: 161

'Cast' an integer to a string

I have a casting problem. I am writing a code to open all the files that are in a folder and have a common name, but with a number that make them different. Let's see this in an easier way with examples. The name of one file will be Table1.txt, another one Table2.txt, another one Table3.txt and so on.

So I'm writing something like this:

int TableId;
for(TableId=1;TableId<=7;TableId++) // We suppose that we have seven different files in my folder.
                                    // Could I make something simmilar to open all the files with these characteristics?
{
    string folder("C:\\example\\"); // Folder where my different files are stored in ".txt" files.

    string Id;            // Casting conversion
    Id = (char) TableId;
    folder += "Table";
    folder += Id;
    folder += ".txt";     // Extension of the file
    ifstream ifs(folder.c_str());
}

The problem is that my code compiles perfectly, but when it runs, it's not in the best way. When I add to the string folder the string Id, my code doesn't understand it in the good way. How do I solve it?

Could I make something so that I could open all the files with the same characteristics, as the files I'm trying to open?

Upvotes: 0

Views: 591

Answers (4)

MSalters
MSalters

Reputation: 179799

The easiest solution might be Boost.

First, your basic example

for(int TableId=1;TableId<=7;TableId++) // We suppose that we have 7 different files in my folder
{
    string folder = "C:/example/"; // Folder where my different files are stored in ".txt" files.
    string file = folder + boost::format("Table%1%.txt") % TableId;
    ifstream ifs(file.c_str());  
}

Now, for the "Could I make something simmilar to open all the files with these characteristics?" part, you'd also use Boost. There's already another SO question which has good answers.

Upvotes: 1

sinek
sinek

Reputation: 2488

It is not a casting problem since you can't just 'cast' an integer to string.

First you need to convert int to string and than concatenate converted string and the folder name. You should check check this post.

Upvotes: 1

RvdK
RvdK

Reputation: 19790

From what I understand from your question is that the Id is not correctly added to the string folder. You could use the itoa function to convert an integer to a string. See here.

Upvotes: 0

templatetypedef
templatetypedef

Reputation: 372784

I think that your problem is that the numeric values 0, 1, 2, ..., 9 are not equivalent to the character values for '0', '1', '2', ..., '9'. The digits actually have numeric values 48, 49, , 50, ..., 57. (See the ASCII table for more details). If you want to iterate across the numbers as characters, you can do so using an explicit for loop like this one:

for (char digit = '1'; digit <= '7'; ++digit) {
     /* ... use digit here ... */
}

This avoids the casting altogether and correctly produces the character sequence that you want.

As for your second question, the C++ standard library does not have much in the way of file system management, and you can't list all files in directory without resorting to third-party libraries. However, the Boost.Filesystem library has some great functionality for exploring directories.

This may be overkill for what you want, but if you are looking for all strings of certain patterns, you might also want to check out Boost.Regex for a set of good C++ regular expression matchers.

Hope this helps!

Upvotes: 2

Related Questions