read data from file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

read data from file

 
0
  #1
May 25th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,347
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read data from file

 
0
  #2
May 25th, 2006
I guess you expect everyone to be mind readers. You will have to post your code
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

Re: read data from file

 
0
  #3
May 25th, 2006
  1. #include<iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ifstream in("myfile.txt");
  9.  
  10. in>>address;
  11. static_cast<long>(address);
  12.  
  13. cout<<address;
  14.  
  15. }
Last edited by cscgal; May 25th, 2006 at 10:29 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,347
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: read data from file

 
0
  #4
May 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

Re: read data from file

 
0
  #5
May 25th, 2006
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


  1.  
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void main()
  7.  
  8. { int arraysize=50;
  9. float **a;
  10. a=new float * [arraysize];
  11. for(int i=0;i<arraysize;i++)
  12. *(a+i)=new float[arraysize];
  13.  
  14.  
  15. float **b;
  16. b=new float * [arraysize];
  17. for( i=0;i<arraysize;i++)
  18. *(b+i)=new float[arraysize];
  19.  
  20.  
  21. float **c;
  22. c=new float * [arraysize];
  23. for( i=0;i<arraysize;i++)
  24. *(c+i)=new float[arraysize];
  25.  
  26.  
  27. ofstream out("myfile.txt");
  28.  
  29.  
  30.  
  31. for (i=0;i<arraysize;i++)
  32. for(int j=0;j<arraysize;j++)
  33. {
  34. a[i][j]=rand()*20 + 1 ;
  35.  
  36. out<<(a + i*arraysize + j)<<endl;
  37.  
  38. }
  39.  
  40.  
  41. for (i=0;i<arraysize;i++)
  42. for(int j=0;j<arraysize;j++)
  43. {
  44. b[i][j]=rand()*20 + 1 ;
  45.  
  46. out<<(b + i*arraysize + j)<<endl;
  47.  
  48. }
  49.  
  50. for (i=0;i<arraysize;i++)
  51. for(int j=0;j<arraysize;j++)
  52. {
  53. c[i][j]=rand()*20 + 1 ;
  54.  
  55. out<<(c+ i*arraysize + j)<<endl;
  56.  
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////
  60. for(i=0;i<arraysize;i++)
  61. {
  62. for (int q=0;q<arraysize;q++)
  63. {
  64. for(int j=0;j<arraysize;j++)
  65. { c[i][q]=a[i][j]*b[j][q]+c[i][q];
  66. out<<(a + i*arraysize +j)<<endl;
  67. out<<(b +j*arraysize +q)<<endl;
  68. out<<(c + i*arraysize +q)<<endl;
  69. out<<(c + i*arraysize +q)<<endl;
  70. }//end of for i
  71.  
  72. }//end of for q
  73. }//end of for i
  74.  
  75.  
  76.  
  77. /////////////////////////////////////////////////////////////////
  78.  
  79. out.close();
  80. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

Re: read data from file

 
0
  #6
May 25th, 2006
and this is a first few lines from the file it represent a hexadecimal value


00481D70
00481D74
00481D78
00481D7C
00481D80
00481D84
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: read data from file

 
0
  #7
May 25th, 2006
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC