#include <iostream>
    using namespace std;
    using std::cerr;
    using std::cout;
    using std::endl;
    #include <sstream>
 #include <string>

    #include <fstream>
    using std::ifstream;

    #include <cstdlib>
     // for exit function

    // This program reads values from the file '1.txt'
    // and echoes them to the display until a negative value
    // is read.

    int main()
    {
       ifstream indata; 
       char num;
       int sara=0,ri=0,sat=0,ha=0;
       int some_int=1;
       ostringstream buffer;
       buffer << some_int;
       string s1 =buffer.str();
       char s[]=".txt";
       
       strcat(s1,s); //giving error while copmiling

      indata.open(s1); // opens the file
       if(!indata) { // file couldn't be opened
          cerr << "Error: file could not be opened" << endl;
          exit(1);
       }

      indata >> num;
       while ( !indata.eof() ) { // keep reading until end-of-file
          indata >> num;
          switch(num){
                      case 'a':sara+=1;break;
                      case 'b':ri+=1;break;
                      case 'c':sat+=1;break;
                      case 'd':ha+=1;break;}
           // sets EOF flag if no value found
       }
       indata.close();
       cout<<sara<<endl<<ri<<endl<<sat<<endl<<ha;
       cout << "End-of-file reached.." << endl;
       cin.get();
       return 0;
    }

this program giving error while concatenation of two string in which s1 has been changed from in type to string.... main purpose of doing this to handle multiple files in my src folder by changing s1.....plz help why giving error

>> strcat(s1,s); //giving error while copmiling

You can't use strcat on s1 because s1 is std::string. All you need is s1 += s;

now indata.open(s1); this is giving error

open() needs a c-style string, use indata.open(s1.c_str());

now the problem is that i have two txt files (1.txt and 2.txt) in my source folder.
but this program running only 1.txt file.content of the 1.txt file is abb so the outcome should be sara 1 and ri 2 but the,output is sara 0 and ri 3 plz help whats gone wrong...and for multiple file accessing also.....

#include <iostream.h>
#include <fstream.h>/*
* Input using cin*/
int main ()
{char myline[256];
int lc = 0;
ofstream outfile("demo.txt",ios::app);
ifstream infile("stdcodes.xyz");
if (! infile)
{cerr << "Failed to open input file\n";
exit(1);
}


---------------------------------------------------------------------------------------
Want to get-on Google's first page and loads of traffic to your website? Hire a SEO Specialist from Ocean Groups seo pecialist

Try putting infile >> num after the switch statement in the while() loop:

indata >> num;
       while ( !indata.eof() ) { // keep reading until end-of-file
          
          switch(num){
                      case 'a':sara+=1;break;
                      case 'b':ri+=1;break;
                      case 'c':sat+=1;break;
                      case 'd':ha+=1;break;}
          indata >> num;
           // sets EOF flag if no value found
       }

A better loop does not use eof()

while ( indata >> num ) { // keep reading until end-of-file
          
          switch(num){
                      case 'a':sara+=1;break;
                      case 'b':ri+=1;break;
                      case 'c':sat+=1;break;
                      case 'd':ha+=1;break;
                      default: cout << "oops!\n"; break;
          }
           // sets EOF flag if no value found
       }
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.