C/C++ Yet another c++ question - reading multiple files

AI Thread Summary
The discussion revolves around efficiently reading data from multiple files in a program using loops. The original poster seeks a method to dynamically construct file names like "file1.dat" through a loop, avoiding repetitive code for each file. Solutions proposed include using character arrays to manipulate file names or employing string concatenation techniques in C or C++. It is noted that for C++, the correct header file is #include<string>, not #include<string.h>, and the use of "using namespace std;" is recommended for easier string declarations. The conversation also touches on the use of the overloaded operator (+) for string concatenation, with examples provided. A user encountered issues with string types in MS VC++ 6, highlighting the importance of using the correct libraries. The discussion concludes with a clarification about concatenating integers to strings, noting that this can lead to segmentation faults if not handled correctly.
Moe
Messages
56
Reaction score
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
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().
 
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.
 
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:
So no .h? Well, now that the strings are working it is much easier. Thank you.
 
[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:
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top