By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

How to read data from files

P: 11
I want to read the string in 4 files as the cose show, the problem is that the code read the data from the first file only ?!!this is the code ....I dont know what is the problem

Expand|Select|Wrap|Line Numbers
  1.     ifstream  inFile1;
  2.           char *p; 
  3.       p = new char[500];
  4.  
  5.  
  6.  
  7. ofstream Result;
  8.     Result.open("Result.txt");
  9.  
  10.     for(i=1;i<5;++i)
  11.     { 
  12.        //Inter the Patterns file
  13.  
  14.         cout<<"Enter patterns file name.\n";
  15.             cin>>pattern;
  16.  
  17.         inFile1.open(pattern.c_str());
  18.  
  19.         if ( !inFile1 )
  20.         {
  21.           cout<<"Unable to open file. Enter a different name: ";
  22.           inFile1.clear();
  23.               cin >> pattern;
  24.           inFile1.open(pattern.c_str());
  25.          }
  26.  
  27.     //Define files for patterns after classification
  28.  
  29.  
  30.  
  31.     while(!inFile1.eof())
  32.            {         
  33.              getline(inFile1, line);
  34.             strcpy_s(p, 500, line.c_str());
  35.                cout<<strlen(p)<<"\n";
  36.  
  37. }
  38.  
  39.                }
  40.     cout<<"endfile\n";
  41.     inFile1.close();
  42.     }
  43.  
  44.  
  45.  
  46.  
  47.  
Oct 17 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.