944,168 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12735
  • C++ RSS
May 25th, 2006
0

read data from file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read data from file

I guess you expect everyone to be mind readers. You will have to post your code
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
May 25th, 2006
0

Re: read data from file

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read data from file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
May 25th, 2006
0

Re: read data from file

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


C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read data from file

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


00481D70
00481D74
00481D78
00481D7C
00481D80
00481D84
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read data from file

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 17th, 2010
-1
Re: read data from file
Good but provide CPP from basic
Reputation Points: 4
Solved Threads: 0
Newbie Poster
neela_482 is offline Offline
1 posts
since Jul 2010
Jul 18th, 2010
0
Re: read data from file
Click to Expand / Collapse  Quote originally posted by neela_482 ...
Good but provide CPP from basic
Pointless reopening of 4 years old thread and on top of that absolutely rubbish advice.

Thread closed
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: C++ Console document and server question.
Next Thread in C++ Forum Timeline: what is wrong with my code plss help?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC