473,473 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

code for reading a file then storing the value and matrix into the system..

34 New Member
hi, well this is the file i have to read into the system...
Expand|Select|Wrap|Line Numbers
  1. <matrix>
  2.     rows = 2
  3.     cols = 2
  4.  
  5. 1 2
  6. 2 4
  7. </matrix>
  8.  
any ideas on how to do it?

below is my code..
and i checked that this doesnt work...
and apparantly someone told me that this is in c programmin style..
could ya help me design a method to read the file in a c++ programming way?
i have done my work.. and this is just exercise from the textbook i am learning c++ from.. unfortunately it doesnt have the solutions.. so i am lost on how to do it.. check my other postings to see that i have actually done my work and this is not homework.. so could someone help me please????

Expand|Select|Wrap|Line Numbers
  1. void Matrix::readMatrixFile(string FILENAME)
  2. {
  3.     const string filename(FILENAME);
  4.     ifstream file(filename.c_str());
  5.     string line;
  6.     if (getline(file, line) && line == "<matrix>")
  7.     {
  8.         if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
  9.         {
  10.             if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
  11.             {
  12.                 for (int r = 0; r < row; r++)
  13.                 {
  14.                     for (int c = 0; c < col; c++)
  15.                     {
  16.                         int value;
  17.                         if (file >> value)
  18.                         {
  19.                             m[r][c] = value;
  20.                         }
  21.                     }
  22.                     cout << endl;
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }
  28.  
thanks
Aug 1 '08 #1
5 2075
boxfish
469 Recognized Expert Contributor
Hi,
Well, the code works fine for me, what goes wrong when you run it?
Aug 1 '08 #2
gpraghuram
1,275 Recognized Expert Top Contributor
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.

Raghu
Aug 1 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You may be going to a lot of extra touble here. All arrays in C++ are one-dimensional. You might read this: http://bytes.com/forum/thread772412.html.
Aug 1 '08 #4
slizorn
34 New Member
You are reading a ; from the line rows = 2 but the file you have posted dosent have it...
Also i think the logic is complex....
Try reading the vine and tokenize it to get the data.

Raghu


how to tolkenize it to get the data?
could u show me the code that does it?
Aug 1 '08 #5
slizorn
34 New Member
Hi,
Well, the code works fine for me, what goes wrong when you run it?

well the program doesnt work for me when i run it...
could u take a look and tell me wats wrong?

header file...
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include "Matrix.h"
  6. using namespace std;
  7.  
  8. class Matrix
  9. {
  10. public:
  11.     Matrix(void);
  12.     ~Matrix(void);
  13.     void readMatrixFile(string FILENAME);
  14.     void addMatrix(Matrix m1, Matrix m3);
  15.     void displayMatrix();
  16.     void transposeMatrix();
  17.     void multiplyMatrices(Matrix m2, Matrix m3);
  18.  
  19. private:
  20.     int row;
  21.     int col;
  22.     /*m = new (double*)[10];
  23.     for(int i = 0; i < 10; i++)
  24.     {
  25.         m[i] = new double[10];
  26.     }
  27.     for(int j = 0; j < 10; j++)
  28.     {
  29.         delete[] m[i];
  30.     }
  31.     delete[] m;*/
  32.     double m[10][10];
  33. };
  34.  
matrix.cpp file
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. Matrix::Matrix(void)
  9. {
  10. }
  11.  
  12.  
  13. Matrix::~Matrix(void)
  14. {
  15. }
  16.  
  17.  
  18. void Matrix::readMatrixFile(string FILENAME)
  19. {
  20.     const string filename(FILENAME);
  21.     ifstream file(filename.c_str());
  22.     string line;
  23.     if (getline(file, line) && line == "<matrix>")
  24.     {
  25.         if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
  26.         {
  27.             if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
  28.             {
  29.                 for (int r = 0; r < row; r++)
  30.                 {
  31.                     for (int c = 0; c < col; c++)
  32.                     {
  33.                         int value;
  34.                         if (file >> value)
  35.                         {
  36.                             m[r][c] = value;
  37.                         }
  38.                     }
  39.                     cout << endl;
  40.                 }
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. void Matrix::addMatrix(Matrix m1, Matrix m3)
  48. {
  49.     for(int a = 0; a < row; a++)
  50.     {
  51.         for(int b = 0; b < col; b++)
  52.         {
  53.             m3.m[a][b]=(m1.m[a][b] + m[a][b]);
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. void Matrix::displayMatrix()
  60. {
  61.     for(int i = 0; i < row; i++)
  62.     {
  63.         cout << "| ";
  64.         for (int j = 0; j < col; j++)
  65.         {
  66.             cout << m[i][j] << " ";
  67.         }
  68.         cout << "|" << endl;
  69.     }
  70. }
  71.  
  72.  
  73. void Matrix::transposeMatrix()
  74. {
  75.     for(int k = 0; k < row; k++)
  76.     {
  77.         for(int j = 0; j < col; j++)
  78.         {
  79.             m[k][j] = m[j][k];
  80.             m[j][k] = m[k][j];
  81.         }
  82.     }
  83. }
  84.  
  85.  
  86. void Matrix::multiplyMatrices(Matrix m2, Matrix m3)
  87. {
  88.     if(col != m2.row)
  89.     {
  90.         cout << "These two matrices cannot be multiplied!!!" << endl;
  91.     }
  92.     else
  93.     {
  94.         for(int i = 0; i < row; i++)
  95.         {
  96.             for(int j = 0; j < m2.col; j++)
  97.             {
  98.                 m3.m[i][j] = 0;
  99.                 for(int k = 0; k < m2.row; k++)
  100.                 {
  101.                     m3.m[i][j] = m3.m[i][j] + m[i][k] * m2.m[k][j];
  102.                 }
  103.             }
  104.         }
  105.     }
  106. }
  107.  
cpp file containing main
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     Matrix m1, m2 ,m3;
  11.     char theOption = '0';
  12.     char whichMatrixToDisplay;
  13.     string filename1, filename2;
  14.  
  15.     while(theOption != '5')
  16.     {
  17.         cout << "Please choose one of the following options from the menu below:" << endl;
  18.         cout << "---------------------------------------------------------------" << endl;
  19.         cout << "(1) Add two matrices" << endl;
  20.         cout << "(2) Multiply two matrices" << endl;
  21.         cout << "(3) Take transpose of a matrix" << endl;
  22.         cout << "(4) Display a matrix" << endl;
  23.         cout << "(5) Exit" << endl;
  24.         cout << "---------------------------------------------------------------" << endl;
  25.         cin >> theOption;
  26.  
  27.         switch(theOption)
  28.         {
  29.             case '1':
  30.                 cout << "You have chosen option 1!: Add two matices!" << endl;
  31.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  32.                 cout << "Enter name of 1st file" << endl;
  33.                 cin >> filename1;
  34.                 cout << "Enter name of 2nd file" << endl;
  35.                 cin >> filename2;
  36.                 m1.readMatrixFile(filename1);
  37.                 m2.readMatrixFile(filename2);
  38.                 m2.addMatrix(m1, m3);
  39.                 cout << "The two matrices have been added together." << endl;
  40.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  41.                 break;
  42.  
  43.             case '2':
  44.                 cout << "You have chosen option 2!: Multiply two matices!" << endl;
  45.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  46.                 cout << "Enter name of 1st file" << endl;
  47.                 cin >> filename1;
  48.                 cout << "Enter name of 2nd file" << endl;
  49.                 cin >> filename2;
  50.                 m1.readMatrixFile(filename1);
  51.                 m2.readMatrixFile(filename2);
  52.                 m1.multiplyMatrices(m2, m3);
  53.                 cout << "The two matrices have been multiplied with each other." << endl;
  54.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  55.                 break;
  56.  
  57.             case '3':
  58.                 cout << "You have chosen option 3!: Transpose a matrix!" << endl;
  59.                 cout << "Please enter the filename that contains the matrix to be transposed!" << endl;
  60.                 cout << "Enter name of file" << endl;
  61.                 cin >> filename1;
  62.                 m1.readMatrixFile(filename1);
  63.                 m1.transposeMatrix();
  64.                 cout << "The matrix has been transposed." << endl;
  65.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  66.                 break;
  67.  
  68.             case '4':
  69.                 cout << "You have chosen option 4!: Display a matix!" << endl;
  70.                 cout << "Please enter the option 1, 2 or 3 to display the respective matrices!" << endl;
  71.                 cin >> whichMatrixToDisplay;
  72.                 if(whichMatrixToDisplay == '1')
  73.                     m1.displayMatrix();
  74.                 else if(whichMatrixToDisplay == '2')
  75.                     m2.displayMatrix();
  76.                 else if(whichMatrixToDisplay == '3')
  77.                     m3.displayMatrix();
  78.                 else
  79.                 cout << endl;
  80.                 break;
  81.  
  82.             case '5':
  83.                 cout << "You have chosen the option to Quit. Goodbye!" << endl;
  84.                 break;
  85.  
  86.             default:
  87.                 cout << " Sorry the option chosen is invalid. Please enter a valid option!" << endl << endl;
  88.                 break;
  89.         }
  90.     }
  91.  
  92.     return 0;
  93. }
  94.  
Aug 1 '08 #6

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

Similar topics

3
by: Steven Burn | last post by:
The application; Service on my webserver that allows a user to upload their HOSTS file for functions to verify the contents are still valid. Uses; 1. XMLHTTP (MSXML2) 2. FileSystemObject...
3
by: Josh | last post by:
Howdy i was recently given a program to do. I have to create a 2d matrix with pointers i have the whole idea down with pointers but there is a problem with one of them i have the code written down...
2
by: Pushpendra | last post by:
I am storing the encrypted password in xml file which I have saved in unicode format. While I try to read the xml file and create a dataset from it, it shows me the following error ...
3
by: Babak | last post by:
Hello everyone, I'm working on some Finite Elements(FE) codes in C and now I encountered some problems in assembly stage. The main idea is that a large number of 3 by 3 elemental stiffness...
4
by: pariub | last post by:
So here is my problem once... i have a file called network.txt ...its a 13X13 matrix...but the network.txt can go up to 100X 100. I should read the text file and store it in array . ...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.