| | |
read data from file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 16
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream in("myfile.txt"); in>>address; static_cast<long>(address); cout<<address; }
Last edited by cscgal; May 25th, 2006 at 10:29 am.
•
•
Join Date: Mar 2006
Posts: 16
Reputation:
Solved Threads: 0
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
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)
#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(); }
Why are you using this form:
It's simpler, for one, and it is also easier to see that
Or that
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
And main returns an int.
You may want to seed your random number generator. Often
(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
![]() |
Similar Threads
- how to read data from text file into data grid??? (VB.NET)
- Get data out of excel file stored as an image (MS SQL)
- help me to read my data file:(( (C)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- How to read data from csv file in an array and parse (C++)
- How can I read from a txt file? (C++)
Other Threads in the C++ Forum
- Previous Thread: trie
- Next Thread: read & write Binary data
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






