print matrix in a file

I'm having problem in printing matrix to a txt file, can you guys help me? thx in advance
Here is the code that I've made
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int data[1000][1000],M,i,j;
FILE * (file);

cout<<"Input size of matrix (mxm) : ";
cin>>M;

for (i=0;i<M;i++)
{
	for (j=0;j<M;j++)
	{
		cout<<"data ["<<i+1<<"]["<<j+1<<"] : ";
		cin>>data[i][j];
	}
}
file=fopen("inputmatriks.txt","w");
for (i=0;i<M;i++)
{
	for (j=0;j<M;j++)
	{
		fprintf (file, "%d",data[i][j]);
	}
	fprintf (file,"\n");
}
fclose (file);
return 0;
}
Please describe what is the problem, otherwise I might be answering the wrong question.

I see a couple of problems now:
when you write a text file, you should open the file for writing text. Otherwise the linefeeds will not be expanded correctly (it will automatically change simple "\n" to "\r\n" under windows, which is the proper indication of line feed under windows).
ile=fopen("inputmatriks.txt","wt"); // notice "wt" instead of "w"

The second thing is - you might want to put spaces between matrix elements:
fprintf (file, "%d ",data[i][j]); // notice space after %d

Also it is a bit weird that you mix the usage of c++ streams with FILE*. Why don't you use std::ofstream instead of FILE*? By the way, standard streams are open for handling text input/output by default.
Last edited on
the problem is i want to print a matrix in a file :). while printing to a file i have an error :

matriks.cpp:24:34: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int*’ [-Wformat]

I use FILE * because I don't have another way to print the output to a file :(.
I use FILE * because I don't have another way to print the output to a file :(

Use fstream! Plese, please use fstream! I is much better in every way! USE FSTREAM!!!
can you give me an example how to use fstream?
This is not an error - this is a warning. And to be honest, a weird one (my compiler doesn't give any warnings).

Equivalent of your code using std::ofstream:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>

...

// open the file (for writing because it is an ofstream, meaning "output file stream")
std::ofstream output("inputmatriks.txt"); 
for (i=0;i<M;i++)
{
	for (j=0;j<M;j++)
	{
		output << data[i][j] << " "; // behaves like cout - cout is also a stream
	}
	output << "\n";
} 
// no need to close the file explicitly because it will be closed automatically by destructor of ofstream. 
// However you can do that if you like using output.close() 


Edit: Oh, and by the problem I meant not the task you are trying to solve, but explanation of what is going wrong. For example, "there is a compilation error (post the error)", or "I cannot find the output file", or "the ouput file is empty/it's contents are unexpected".
Last edited on
oh allright thank you very much.
Topic archived. No new replies allowed.