Hello Peers,

I have a sequence of data files (Site_1.txt, Site_2.txt,.....,Site_N.txt) that i need to read into my main program. Although I managed to construct the file names (as character arrays using sprintf), I am not sure of how to pass them to the ifstream, as it needs the file names to be of string type.

Thanks

int Ns,Fj;
char buffer[50],S[]="Site_";
for (Ns=1;Ns<=N_Tables;Ns++)
		{
			Fj=sprintf(buffer,"%s",S);
			Fj+=sprintf(buffer+Fj,"%d.txt",Ns);
			//cout<<buffer;
			ifstream ifData(buffer);
			if(!ifData){cout<<"Cannot open the Data file"; return 1;}
		}

OK, here you go. (Don't use char arrays for strings.)

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string make_filename( int file_number ) {
  stringstream sfilename;
  sfilename << "Site_" << file_number << ".txt";
  return sfilename.str();
  }

int main() {
  int Ns;
  string filename;

  for (Ns = 1; Ns <= N_Tables; Ns++) {
    filename = make_filename( Ns );
    ifstream ifData( filename );
    ...
    }

  return EXIT_SUCCESS;
  }

If you are determined to use char arrays for your filename, then say: ifstream ifData( string( buffer ) ); Happy Thanksgiving.

Duoas, Thanks very much for your prompt reply.

Happy Thanksgiving!

Hi Duoas,
Is "string" a defined datatype in vc++? Because, whenever I type the word "string", the vc++ editor does not highlight it (whereas int, char gets highlighted). The code below throws up two error messages..

string make_filename (int file_number)
{
stringstream sfilename;
sfilename<<"SiteHazard_"<<file_number<<".txt";
return sfilename.str();
}

int main()
{
int Ns,Fj;
string filename;

for (Ns=1;Ns<=N_SiteHazard_Tables;Ns++)
{
filename=make_filename(Ns);
ifstream ifData(filename);
if(!ifData){cout<<"Cannot open the Data file"; return 1;}
}

return 0;
}

error C2871: 'system' : does not exist or is not a namespace

error C2664: '__thiscall std::basic_ifstream<char,struct std::char_traits<char> >::std::basic_ifstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

Thanks

Yes, "string" is a standard template class.

I always mix up the stream constructors. Your first post complained that the ifstream required a string, so I took that as right. But that's backwards. The ifstream constructor requires your filename to be a char*. So... use: ifstream ifData( filename.c_str() ); If that doesn't fix your 'system' problem, show me every place in your code where you use the word 'system'. If there isn't any, there is some other problem... alas.

Hope this helps.

Hi Duoas,

Sorry, it was my ignorance in using the right words. Please find below the entire code.

# include <iostream>
# include <fstream>
# include <cstdio>
# include <sstream>
# include <string>

# define N_SiteHazard_Tables 2
# define N_Events 21110

using namespace std;

string make_filename (int file_number)
{
	stringstream sfilename;
	sfilename<<"SiteHazard_"<<file_number<<".txt";
	return sfilename.str();
}

int main()
{
	int Ns,Fj;
	string filename;
	
	for (Ns=1;Ns<=N_SiteHazard_Tables;Ns++)
		{
			filename=make_filename(Ns);
			ifstream ifData(filename);
			if(!ifData){cout<<"Cannot open the Data file"; return 1;}
		}
	
	return 0;
}

Thanks

Don't be sorry. I'm the one who can't remember whether it takes string or char*.

Don't use <cstdio>. You don't need it.

I already gave the answer for the ifData stream in my last post.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.