GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Aug-2008, 19:57
elo9p elo9p is offline
New Member
 
Join Date: Aug 2008
Posts: 1
elo9p is on a distinguished road

Reading A matrix From a .dat file


I am looking to create a C++ code that will first open and read a .dat file called shms.dat. This file is a first order 6 X 6 matrix, which is set up as follows:


1st row -1.38013 0.00000 0.00000 0.00000 0.00000 1.65616
2nd row -0.57498 -0.72457 0.00000 0.00000 0.00000 3.23192
3rd row 0.00000 0.00000 -1.63518 0.00000 0.00000 -0.15092
4th row 0.00000 0.00000 -2.71347 -0.61155 0.00000 0.07025
5th row 0.35082 -0.12000 0.05244 0.00923 1.00000 -0.05134
6th row 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000


Now, the code ideally is to do 2 things: it is supposed to first read in the shms.dat file as a first order 6 X 6 matrix and writes it back out as a 6 X 6 matrix. Then, it is supposed to create an input vector that can be multiplied by this matrix after it is read in.

Now, I do have some idea of where to start; I am pretty sure the best way to do this in C++ is to use the fstream class. I have tried streaming the file and then reading it and writing it out; the code is as follows, so as to show what I've tried to do and perhaps more importantly that I've tried to work on this myself before asking someone else for insight:

CPP / C++ / C Code:
#include <stdio.h>
#include <float.h>

#include <stdlib.h>

#include <ifstream>
#include<fstream>
#include<math.h>





float R[6][6],in[6],outvec[6],Zo,z;
int using_x_direction,j,k,i,dum;
FILE *out;
FILE *file;
FILE *fstream;
FILE *ifstream;
int main()
{


ifstream in;

fstream file("shms.dat");

if (!file) {

cout << "Fail" << endl;

}



for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
fscanf(file,"shms",&dum,&dum,&(R[i][j]) );

fprintf(file,"shms.dat");

}

}
fclose( file );





}


The error messages I get when I run it are:

Code:
error: ifstream: No such file or directory junk2.c:7:18: error: fstream: No such file or directory junk2.c: In function ‘main’: junk2.c:25: error: expected ‘;’ before ‘in’ junk2.c:27: error: expected ‘;’ before ‘file’ junk2.c:31: error: ‘cout’ undeclared (first use in this function) junk2.c:31: error: (Each undeclared identifier is reported only once junk2.c:31: error: for each function it appears in.) junk2.c:31: error: ‘endl’ undeclared (first use in this function

Now, some of these error messages are rather peculiar, since I thought, for instance, the classes ifstream and fstream were inherent in C++. Now it seems that is not the case.

In any event, I was trying above to do the first part of the code-read in and write out a first order 6 X 6 matrix and write it out. Then, I was looking to multiply and write (unless most C++ users prefer to call it "print") out the multiplication results.

Thank you very much in advance for any type of help you can provide.
Last edited by admin : 03-Aug-2008 at 19:39. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 03-Aug-2008, 13:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,305
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Reading A matrix From a .dat file


Quote:
Originally Posted by elo9p
I am looking to create a C++ c...Now, I do have some idea of where to start; I am pretty sure the best way to do this in C++ is to use the fstream class....
I agree: that's the way to start...

However, I respectfully suggest that you look for examples that use this class.

Look in a book or find some explanation and examples on the web. For example: cplusplus.com files tutorial. There are lots of others. Poke around on the web until you find one that you like.

Look at their examples. Use the header files that they show. Then, maybe, if it's not exactly what you need, you will be starting from something that works.

Their examples use the getline function for reading, and that's the way that I might do it. For getting started, using the overloaded >> operator works, too, and is entirely appropriate for many programs such as yours.

For example, here's my take on the first part of your problem:

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    string dummy;
    char *inname = "shms.dat";
    double myArray[6][6]; 

    int i, j;

    ifstream infile(inname);
    if (!infile) {
        cout << "Couldn't open file " << inname << " for reading." << endl;
        return 0; //Or, maybe try something to recover
    }
    cout << "Opened file " << inname << " for reading." << endl;
    for (i = 0; i < 6; i++) {
        cout << "Line " << i;
        infile >> dummy;
        cout << "  First item is <" << dummy << ">";
        infile >> dummy;
        cout << ",  Second item is <" << dummy << ">" << endl;
        for (j = 0; j < 6; j++) {
            infile >> myArray[i][j];
            if (!infile) {
                cout << "***There was a problem trying to read element ["
                     << i << "][" << j << "]" << endl;
                return 0; // Or, maybe try something to recover
            }
        }
    }
    infile.close();

    cout << "Here's the array from the file" << endl;
    cout << fixed << setprecision(5);
    for (i = 0; i < 6; i++) {
        for (j = 0; j < 6; j++) {
            cout << setw(10) << myArray[i][j];
        }
        cout << endl;
    }
    cout << endl;

    return 0;
}

Key features: Always test to make sure the file opened properly. Test the condition of the stream after numeric inputs to make sure it's OK.

Be verbose in user output messages. For example, if there are input errors, I never just say "program terminating," I tell the user exactly what happened that caused the problem.

There are a lot of ways to get the program to ignore the first two "things' on a line so that you can get to the numeric stuff. Your way of reading something into a "dummy" variable that you then throw away is very practical. The thing is, that since the first two "things" are not numeric quantities, you can't read them into integer data types. I used a std::string dummy variable; you can do other things as well

I also show some elementary output formatting so the output is lined up neatly in columns.

For your program to write the matrix values to a file, you would open an ofstream and instead of "cout << ", you would use the "<<" operator with the outpug stream.

Here's the output from my test program:
Code:
Opened file shms.dat for reading. Line 0 First item is <1st>, Second item is <row> Line 1 First item is <2nd>, Second item is <row> Line 2 First item is <3rd>, Second item is <row> Line 3 First item is <4th>, Second item is <row> Line 4 First item is <5th>, Second item is <row> Line 5 First item is <6th>, Second item is <row> Here's the array from the file -1.38013 0.00000 0.00000 0.00000 0.00000 1.65616 -0.57498 -0.72457 0.00000 0.00000 0.00000 3.23192 0.00000 0.00000 -1.63518 0.00000 0.00000 -0.15092 0.00000 0.00000 -2.71347 -0.61155 0.00000 0.07025 0.35082 -0.12000 0.05244 0.00923 1.00000 -0.05134 0.00000 0.00000 0.00000 0.00000 0.00000 1.00000

Note that I would probably comment out the "line x First item..." stuff after I had made sure that the program reads exactly the number of lines and data items that the file contains. The point is that, especially with new programs, I make the program tell me exactly what it is doing at each step. (The silliness of "line 0" is due to the fact that I just printed out the value of i and it starts from zero. Since that's only there during initial program testing, I didn't try to "dress it up" by making the line numbers go from 1 to 6. Chacun à son goût...)

Regards,

Dave

Footnote: It's C++. Although it is "legal" to use C library functions (including the FILE stuff from <stdiio.h>), there are many advantages to using C++ streams. Generally speaking, I think that most C++ programmers don't like to use both types in the same program. My advice: If you are learning to use C++, then use the functions and features of C++. (That's only an opinion, however, and it is worth exactly what you want it to be worth.)
Last edited by davekw7x : 03-Aug-2008 at 15:00.
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
contents of .txt file into 2D array anirudhroxrulz C Programming Language 4 10-Apr-2008 22:45
Need help - complex matrix, complex vector mosquito C++ Forum 30 10-Apr-2008 03:16
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Reading Matrix from Text File TheHive C Programming Language 3 30-Aug-2006 17:55
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:58.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.