Afternoon all,
I am programming in C++ and I wish to output the result of my numerical calculations to a txt file. I have a vector (one-dim array) that holds a list of numbers.
This is output to a txt file called eigenvalue.txt using the following code -
FILE *ptr;
-
-
ptr=fopen("c:\\eigenvalue.txt","w");
-
-
for(int i=0;i<20;i++){
-
fprintf(ptr,"%e\n",*(vector+i));
-
}
-
-
fclose(ptr);
What I want to do is to output a matrix (two-dim array) to another txt file (matrix.txt) using a similar code. I know that the above code will work, but I want to have the array printed in matrix form in the txt file. -
FILE *p;
-
p=fopen("c:\\matrix.txt","w");
-
-
for(int i=0;i<(int)(number_of_nodes);i++){
-
for(int j=0;j<(int)(number_of_nodes);j++){
-
fprintf(p,"%e\n",*(global_K+(int)(i*number_of_nodes)+j));
-
}
-
}
-
-
fclose(p);
-
Is it a case of changing the fprintf statement so that it will
4 2691 -
FILE *p;
-
p=fopen("c:\\matrix.txt","w");
-
-
for(int i=0;i<(int)(number_of_nodes);i++){
-
for(int j=0;j<(int)(number_of_nodes);j++){
-
fprintf(p,"%e\n",*(global_K+(int)(i*number_of_nodes)+j));
-
}
-
}
-
-
fclose(p);
-
I'm more familiar with streams (such as ifstream and ofstream), but it looks like your problem is in the actual fprintf call - you are printing %e and the newline character, '\n'. As I understand it, %e will be replaced with your value, which is fine - but you are currently printing a new line after every value. You should print at the end of every row, which will occur inside the first for...loop, but outside the next for...loop. It may look something like this: - for(int i=0;i<(int)(number_of_nodes);i++){
-
for(int j=0;j<(int)(number_of_nodes);j++){
-
fprintf(p,"%e",*(global_K+(int)(i*number_of_nodes)+j));
-
}
-
fprintf(p, "\n");
-
}
Again, I apologize for any syntax errors regarding the fprintf statement.
Also, your control for each loop is number_of_nodes, which sounds like the total number of elements in your array. You should be using the total number of rows and columns to control i and j, respectively.
I changed the code to read as follows -
for(int i=0;i<(int)(number_of_nodes);i++){
-
for(int j=0;j<(int)(number_of_nodes);j++)
-
fprintf(fptr,"%f ",*(global_K+(int)(i*number_of_nodes)+j));
-
fprintf(fptr,"\n");
-
}
-
Its essentially the same as what you suggested and it does the same job.
Thanks for the help.
Also, your control for each loop is number_of_nodes, which sounds like the total number of elements in your array. You should be using the total number of rows and columns to control i and j, respectively.
The size of the array is number_of_nodes*number_of_nodes so the loop control is ok.
The size of the array is number_of_nodes*number_of_nodes so the loop control is ok.
I thought that might have been the case, but I wasn't sure. I'm glad you got it working. Good luck with the rest of the project.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jonathan |
last post by:
I have a client solution that requires data and associated files to be
stored with data in a database. As such, I have a situation where JPEG
thumbnails/images that are stored as BLOBs (image...
|
by: Andrei Pociu |
last post by:
I have a major doubt about outputting text in ASP .NET when using code
behind.
I know most of the output you gain from a code behind file (.aspx.cs) is
outputted to the Webform (.aspx) using...
|
by: Jacek Dziedzic |
last post by:
Hello!
I have a piece of code that needs to display a formatted
table of pointers using streams, with the pointers represented
as hex values. It looks more or less like this:
#include...
|
by: Andy |
last post by:
Hi
I'm really stuck outputting a double number to the console with three
decimal places if the furthest right value is a zero.
I can coutput the number 4.546 as 4.546 but then if I output...
|
by: cephelo |
last post by:
I have no problems outputting the attribute value when the node is in
context, for example, @id when an <status> node is in context.
However, I am having trouble outputting it in a <xsl:value-of...
|
by: Heather Barber |
last post by:
Hi
So I'm fine with outputting to file from C#. But... as soon as I create an executable and move the file it no longer works - in fact it crashes due to security settings of some sort - whereever i...
|
by: Peter Nimmo |
last post by:
Hi,
I am writting a windows application that I want to be able to act as if
it where a Console application in certain circumstances, such as error
logging.
Whilst I have nearly got it, it...
|
by: phong.lee |
last post by:
Hello all,
I was wondering if someone can assist me in outputting 6 reports into
a pdf file? I created a macro that generates the 6 reports and right
now it's saved as a snapshot on my drive. ...
|
by: mohaakilla51 |
last post by:
Alright guys, I am working on a flashcard script... Previously I had it so that it onlty had predefined categories. People were complaining, so now I am trying to make it to where it reads...
|
by: billelev |
last post by:
This is probably a very easy question to answer:
I have been outputting some text to a message box, similar to the following:
strOutput = "---" & Chr(10) & Chr(10)
strOutput = strOutput &...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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,...
|
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,...
|
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...
| |