954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

read data from file

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

mhm_ra
Newbie Poster
16 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
 
int main()
{
ifstream in("myfile.txt");
 
in>>address;
static_cast<long>(address);
 
cout<<address;
 
}
mhm_ra
Newbie Poster
16 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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(address);
that line does nothing at all. you might as well delete it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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();
}
mhm_ra
Newbie Poster
16 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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


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

mhm_ra
Newbie Poster
16 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Good but provide CPP from basic

neela_482
Newbie Poster
1 post since Jul 2010
Reputation Points: 4
Solved Threads: 0
 
Good but provide CPP from basic


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

Thread closed

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You