#1) You can't use strcat on strings. Only with char*. Look up string methods.
#2) Break the following line into pieces to create the file name and output it so you know it's correct before using the open function. It's also less confusing.
infile.open(strcat(strcat(st1,(strcat((string)i,st2))),ios::in)
#3) Read this about the following line (.eof() is the same as feof())
while (!infile.eof())
#4) Also read this to find out how to format code so it can be understood.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Here is an example of one way to construct those filenames
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string st1 = "file";
string ext = ".txt";
string filename;
for(int i = 1; i < 10; i++)
{
stringstream ss;
ss << i;
filename = st1 + ss.str() + ext;
cout << filename << "\n";
}
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The open() function only takes char * as an argument --it won't take std::string. Say this instead:
infile.open( filename.c_str(), ios::in );
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
since infile is of type ifstream it is not necessary to specify ios::in because that is the default
after closing the stream you have to also clear the error bits because if you don't the file will not open the second and succeeding loop iterations.
infile.close();
infile.clear();
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I thought you are supposed to be writing a c++ program? If so, then why are you resorting to C language syntax and C functions?
>> freq[filecounter]lineineachfile[filecounter]
I think you are missing square brackets in that statement.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you had it all right except the open statement, and Duoas gave you the correct way to do that.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343