Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

reading matrix from text file in c++ and adding them and then storing them in oother c++ file

#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function
// This program reads values from the file 'example.dat'
// and echoes them to the display until a negative value
// is read.
int main()
{
   ifstream indata; // indata is like cin
   int num; // variable for input value
indata.open("example.dat"); // 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
      cout << "The next number is " << num << endl;
      indata >> num; // sets EOF flag if no value found
   }
   indata.close();
   cout << "End-of-file reached.." << endl;
   return 0;
}
Source by www.bgsu.edu #
 
PREVIOUS NEXT
Tagged: #reading #matrix #text #file #adding #storing #oother #file
ADD COMMENT
Topic
Name
2+4 =