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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top