Yet another c++ question - reading multiple files

In summary: Is there a different way to do this? In summary, you can use the overloaded operator (+) to concatenate multiple strings.
  • #1
Moe
56
0
I have the following problem: My program is supposed to read data from files to an array. The files are numbered: file1.dat, file2.dat and so on. There are 24 of those files, and I really don't want to do the same step 24 times. Is there a way to write a loop along the lines of

for (int i=1; i<25, i++);
{
fstream myfile("C:\Data\file(i).dat")
...
}

In other words, is there a way to insert the "i" variable into the filename?

Thank you for your insights.
 
Technology news on Phys.org
  • #2
You can save the string inside myfile in a character array. Then you can change the number by doing file[where ever it is] = 3. You'll have to name the file 01, 02, 03, etc because you'll be dealing with double digits.

The other option is to use string concatentation. You can either do it the C or the C++ way. Using the C way you would include string.h and use the function called strcat(). If you do it the C++ way you would include string and use the member function of the string class called append().
 
  • #3
Thanks. I ended up solving the problem with strcat. For some weird reasons my compiler (MS VC++ 6) does not recognize string as a variable type, even though I #included string.h. All paths are correct, I am baffled as to why this is not working. Fortunately the array of char thing worked just fine.
 
  • #4
The string datatype doesn't work because you need to use #include<string> which is the STL library for C++. #include<string.h> is the C library. When using STL you also should put after the includes: "using namespace std;". That way you you don't need to do this when you want to declare a new string: "std::string mystring;"

[edit] Muzza, your right, you can use the overloaded operator (+) to contatenate multiple strings. For example, this would work:

Code:
#include<iostream>
#include<string>

using namespace std;

int main( int argc, char **argv) {

string str1 = "Hello";
string str2 = " ";
string str3 = "World!";

cout << str1 + str2 + str3 << endl;

}

The output being:

Hello World!
 
Last edited:
  • #5
So no .h? Well, now that the strings are working it is much easier. Thank you.
 
  • #6
[edit] Muzza, your right, you can use the overloaded operator (+) to contatenate multiple strings. For example, this would work:

Ack, why do people always manage to see the posts I delete ;) Anyway, I knew about string concatenation (in fact, I used it in my deleted post ;)), but using + to concatenate an /integer/ to an STL string doesn't seem to work (or so it appeared, my test program segfaulted).
 
Last edited:

1. How do I read multiple files in C++?

In order to read multiple files in C++, you can use the ifstream class from the <fstream> library. This class allows you to open and read files in your program. You can use a loop to open and read each file one by one.

2. Can I read different types of files in the same program?

Yes, you can read different types of files in the same program. As long as you use the appropriate file reading functions for each file type, you can read multiple files of different types in the same program.

3. How do I handle errors while reading multiple files?

In order to handle errors while reading multiple files, you can use the fail() function from the ifstream class. This function checks if there was an error while reading the file and returns a boolean value. You can also use the clear() function to clear any error flags and continue reading the remaining files.

4. Is it possible to read multiple files simultaneously?

Yes, it is possible to read multiple files simultaneously in C++. You can use threads to read each file in a separate thread, which allows for parallel execution and can potentially improve the performance of your program.

5. How can I store the data from multiple files in a single data structure?

In order to store the data from multiple files in a single data structure, you can use a container such as a vector or an array. You can read each file into a separate container and then combine them into a single container using functions like insert() or concat().

Similar threads

  • Programming and Computer Science
Replies
1
Views
415
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
6
Views
924
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
711
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
10
Views
904
  • Programming and Computer Science
Replies
2
Views
168
  • Programming and Computer Science
Replies
8
Views
5K
Back
Top