i write a program which write a value of a pointer into file using "out" now i need to read this data from the file but when i read it and print it on the screen a minus value appear.

can you help me by tell9ing me how to read data using "in" and why yhis problem occur

Recommended Answers

All 8 Replies

I guess you expect everyone to be mind readers. You will have to post your code

#include<iostream>
#include <fstream>
#include <string>
using namespace std;
 
int main()
{
ifstream in("myfile.txt");
 
in>>address;
static_cast<long>(address);
 
cout<<address;
 
}

what is variable "address"? where did you declare it?
also post the code that wrote the file. and the first line or two of the file contents.

>>static_cast<long>(address);
that line does nothing at all. you might as well delete it.

thank you for your interest
i try to defined address as a string and read data but didn't work
below is the code that write into a file

#include <fstream>
#include <string>
using namespace std;

void main()

{  int arraysize=50;
   float **a;
	a=new float * [arraysize];
	for(int i=0;i<arraysize;i++)
	*(a+i)=new float[arraysize];


	float **b;
	b=new float * [arraysize];
	for( i=0;i<arraysize;i++)
	*(b+i)=new float[arraysize];
	
	
 	float **c;
	c=new float * [arraysize];
	for( i=0;i<arraysize;i++)
	*(c+i)=new float[arraysize];

	
	ofstream out("myfile.txt");
   
	
   
  for (i=0;i<arraysize;i++)
		for(int j=0;j<arraysize;j++)
		{
		a[i][j]=rand()*20 + 1 ;
		
		out<<(a + i*arraysize + j)<<endl;
		
		}  


for (i=0;i<arraysize;i++)
		for(int j=0;j<arraysize;j++)
		{
		b[i][j]=rand()*20 + 1 ;
		
		out<<(b + i*arraysize + j)<<endl;
		
		}  

		for (i=0;i<arraysize;i++)
		for(int j=0;j<arraysize;j++)
		{
		c[i][j]=rand()*20 + 1 ;
		
		out<<(c+ i*arraysize + j)<<endl;
		
		}  

/////////////////////////////////////////////////////////////////
	for(i=0;i<arraysize;i++)
		{ 
			for (int q=0;q<arraysize;q++)
			{	
				for(int j=0;j<arraysize;j++)
				{	c[i][q]=a[i][j]*b[j][q]+c[i][q];
				out<<(a + i*arraysize +j)<<endl;
				out<<(b +j*arraysize +q)<<endl;
				out<<(c + i*arraysize +q)<<endl;
				out<<(c + i*arraysize +q)<<endl;
				}//end of for i
			
			}//end of for q
		}//end of for i



		/////////////////////////////////////////////////////////////////

   out.close();
}

and this is a first few lines from the file it represent a hexadecimal value


00481D70
00481D74
00481D78
00481D7C
00481D80
00481D84

Why are you using this form: (a + i*arraysize +j) rather than subscript notation a[i][j] ?

It's simpler, for one, and it is also easier to see that (a + i*arraysize +j) means the same as &a[i][j] .
Or that *(a + i*arraysize +j) means the same as a[i][j] .

So what you are putting into your file are not the values you have created, but their location -- which is fairly meaningless.

It might be helpful to output some text into the file to denote matrices, and perhaps make them look like 2D matrices rather than one long column.

Also, MSVC6 uses incorrect scoping rules with variables declared in a for loop. I'd say just move your i, j, q outside of the loops.

And do you instead want % here?

a[i][j]=rand() * 20 + 1 ;

And main returns an int.

You may want to seed your random number generator. Often srand(time(0)); is used.

Good but provide CPP from basic

commented: useless bump of a 4-year old thread -6

Good but provide CPP from basic

Pointless reopening of 4 years old thread and on top of that absolutely rubbish advice.

Thread closed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.