Yet another c++ question - reading multiple files

Click For Summary

Discussion Overview

The discussion revolves around a programming challenge in C++ related to reading multiple files into an array. Participants explore methods for dynamically constructing file names based on a loop index, addressing issues with string handling and file input/output operations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a need to read from multiple files named sequentially and seeks a way to incorporate a loop variable into the file name.
  • Another suggests using a character array to store the file name and modifying it to include the loop index, recommending double-digit naming for files.
  • A participant mentions successfully using the `strcat` function to solve the problem, despite issues with the `string` type in their compiler.
  • Another participant clarifies that the `string` type requires including the correct header `` instead of ``, and suggests using `using namespace std;` for convenience.
  • One participant shares a code example demonstrating string concatenation using the overloaded `+` operator.
  • A later reply notes a concern about concatenating an integer to an STL string, mentioning a segmentation fault encountered during testing.

Areas of Agreement / Disagreement

Participants express differing views on the correct usage of string types and concatenation methods in C++. There is no consensus on the best approach to handle file naming and string manipulation.

Contextual Notes

Participants highlight limitations related to the specific compiler being used, which may affect the recognition of certain data types and functions. There are also unresolved issues regarding the handling of integers in string concatenation.

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:

Similar threads

  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K