How can i read array by a file?
i have an array BYE[2000] and a text file with extension .map
Now i want read this file and put inside the array the contents of this file.
But how can i do?
How must be the .map file? if multirows, if must have space or parenthesis or in one row just one number....
THX
go one better make it a variable array.
In C
In C
#include <stdio.h>#include <stdlib.h>/* a pointer which will hold the location of the array */float *SomeArray = NULL;/* the number of elements in the array */unsigned int ArraySize = 0;int Load(char *filename) { int i; /* open the file */ FILE *fp = fopen(filename,"r"); /* check if file opened */ if( fp == NULL ) { fprintf(stderr,"Error: could not open file for writing\n"); return 0; } /* check to see if data loaded */ if( SomeArray != NULL ) { /* de-allocate old data */ free(SomeArray); SomeArray = NULL; } /* read the number of values */ fscanf(fp,"%d",&ArraySize); /* allocate memory for array */ SomeArray = (float *)malloc( sizeof(float) * ArraySize ); /* check allocation ok */ if( SomeArray == NULL ) { fprintf(stderr,"Error, memory allocation failed\n"); return 0; } /* read in each variable */ for( i=0; i<ArraySize; ++i ) { fscanf(fp,"%f",&SomeArray);<br> }<br><br> /* close the file */<br> fclose(fp);<br> return 1;<br>}<br><br>int Save(char *filename) {<br><br> /* open the file for writing */<br> FILE *fp = fopen(filename,"w");<br><br> /* check if file opened */<br> if( fp == NULL )<br> {<br> fprintf(stderr,"Error: could not open file for writing\n");<br> return 0;<br> }<br><br> /* print the number of values */<br> fprintf(fp,"%d\n",ArraySize);<br><br> /* read in each variable */<br> for( i=0; i<ArraySize; ++i )<br> {<br> fprintf(fp,"%f\n",&SomeArray);<br> }<br><br> /* close the file */</font><br> fclose(fp);<br> <font color="blue">return</font> 1;<br>}<br> </pre></DIV><!–ENDSCRIPT–><br><br>In C++<br><br> <!–STARTSCRIPT–><BR><DIV CLASS=source><pre> <br><font color="green">#include <iostream><br></font><font color="green">#include <fstream><br></font><br><font color="gray">// a pointer which will hold the location of the array <br></font><br><font color="blue">float</font> *SomeArray = NULL;<br><br><font color="gray">// the number of elements in the array <br></font><br><font color="blue">unsigned</font> <font color="blue">int</font> ArraySize = 0;<br><br><font color="blue">bool</font> Load(<font color="blue">char</font> *filename) {<br><br> std::ifstream ifs(filename);<br><br> <font color="gray">// check if file opened <br></font><br> <font color="blue">if</font>( !ifs )<br> {<br> std::cerr << <font color="darkred">"Could not open file"</font> << std::endl;<br> <font color="blue">return</font> false;<br> }<br><br> <font color="gray">// de-allocate old data <br></font><br> <font color="blue">delete</font> [] SomeArray;<br> SomeArray = NULL;<br><br> <font color="gray">// read the number of values <br></font><br> ifs >> ArraySize;<br><br> <font color="gray">// allocate memory for array <br></font><br> SomeArray = <font color="blue">new</font> float[<font color="purple"> ArraySize </font>];<br><br> <font color="gray">// check allocation ok <br></font><br> <font color="blue">if</font>( !SomeArray )<br> {<br> std::cerr << <font color="darkred">"Error, memory allocation failed"</font> <br> << std::endl;<br> <font color="blue">return</font> false;<br> }<br><br> <font color="gray"><font color="gray">// read in each variable <br></font></font><br> <font color="blue">for</font>( <font color="blue">int</font> i=0; i<ArraySize; ++i )<br> {<br> ifs >> SomeArray[<font color="purple"> i </font>];<br> }<br><br> <font color="gray">// close the file <br></font><br> ifs.close();<br><br><br> <font color="blue">return</font> true;<br>}<br><br><font color="blue">bool</font> Save(<font color="blue">char</font> *filename) {<br><br> std::ofstream ofs(filename);<br><br> <font color="gray">// check if file opened <br></font><br> <font color="blue">if</font>( !ofs )<br> {<br> std::cerr << <font color="darkred">"Could not open file"</font> << std::endl;<br> <font color="blue">return</font> false;<br> }<br><br> <font color="gray">// write the number of values <br></font><br> ofs << ArraySize << std::endl;<br><br> <font color="gray"><font color="gray">// read in each variable <br></font></font><br> <font color="blue">for</font>( <font color="blue">int</font> i=0; i<ArraySize; ++i )<br> {<br> ofs << SomeArray[<font color="purple"> i </font>] << std::endl;<br> }<br><br> <font color="gray">// close the file<br></font><br> ofs.close();<br><br> <font color="blue">return</font> true;<br>}<br> </pre></DIV><!–ENDSCRIPT–><br><br><br>In C++ using STL<br><br> <!–STARTSCRIPT–><BR><DIV CLASS=source><pre> <br><font color="green">#include <iostream><br></font><font color="green">#include <fstream><br></font><font color="green">#include <vector><br></font><br><br>std::vector<float> SomeArray;<br><br><font color="blue">bool</font> Load(<font color="blue">char</font> *filename) {<br><br> std::ifstream ifs(filename);<br><br> <font color="gray">// check if file opened <br></font><br> <font color="blue">if</font>( !ifs )<br> {<br> std::cerr << <font color="darkred">"Could not open file"</font> << std::endl;<br> <font color="blue">return</font> false;<br> }<br><br> <font color="blue">int</font> ArraySize=0;<br><br> <font color="gray">// read the number of values <br></font><br> ifs >> ArraySize;<br><br> <font color="gray">// resize the array <br></font><br> SomeArray.resize(ArraySize);<br><br> <font color="gray">// read in each variable <br></font><br> std::vector<float>::iterator it = SomeArray.begin();<br> <font color="blue">for</font>( ; it != SomeArray.end(); ++it )<br> {<br> ifs >> *it<br> }<br><br> <font color="gray"><font color="gray">// close the file <br></font></font><br> ifs.close();<br><br> <font color="blue">return</font> true;<br>}<br><br><font color="blue">bool</font> Save(<font color="blue">char</font> *filename) {<br><br> std::ofstream ofs(filename);<br><br> <font color="gray">// check if file opened <br></font><br> <font color="blue">if</font>( !ofs )<br> {<br> std::cerr << <font color="darkred">"Could not open file"</font> << std::endl;<br> <font color="blue">return</font> false;<br> }<br><br> <font color="gray">// write the number of values */<br></font><br> ofs << SomeArray.size() << std::endl;<br><br> <font color="gray">// write out each variable <br></font><br> std::vector<float>::iterator it = SomeArray.begin();<br> <font color="blue">for</font>( ; it != SomeArray.end(); ++it )<br> {<br> ofs << *it << std::endl;<br> }<br><br> <font color="gray"><font color="gray">// close the file <br></font></font><br> ofs.close();<br><br> <font color="blue">return</font> true;<br>}<br> </pre></DIV><!–ENDSCRIPT–><br><br><br><SPAN CLASS=editedby>[edited by - Rob The Bloke on February 2, 2003 5:49:42 PM]</SPAN>
quote:Original post by Rob The Bloke
go one better make it a variable array.
In C
#include <stdio.h>#include <stdlib.h>/* a pointer which will hold the location of the array */float *SomeArray = NULL;/* the number of elements in the array */unsigned int ArraySize = 0;int Load(char *filename) { int i; /* open the file */ FILE *fp = fopen(filename,"r"); /* check if file opened */ if( fp == NULL ) { fprintf(stderr,"Error: could not open file for writing\n"); return 0; } /* check to see if data loaded */ if( SomeArray != NULL ) { /* de-allocate old data */ free(SomeArray); SomeArray = NULL; } /* read the number of values */ fscanf(fp,"%d",&ArraySize); /* allocate memory for array */ SomeArray = (float *)malloc( sizeof(float) * ArraySize ); /* check allocation ok */ if( SomeArray == NULL ) { fprintf(stderr,"Error, memory allocation failed\n"); return 0; } /* read in each variable */ for( i=0; i<ArraySize; ++i ) { fscanf(fp,"%f",&SomeArray);<br> }<br><br> /* close the file */<br> fclose(fp);<br> return 1;<br>}<br><br>int Save(char *filename) {<br><br> /* open the file for writing */<br> FILE *fp = fopen(filename,"w");<br><br> /* check if file opened */<br> if( fp == NULL )<br> {<br> fprintf(stderr,"Error: could not open file for writing\n");<br> return 0;<br> }<br><br> /* print the number of values */<br> fprintf(fp,"%d\n",ArraySize);<br><br> /* read in each variable */<br> for( i=0; i<ArraySize; ++i )<br> {<br> fprintf(fp,"%f\n",&SomeArray);<br> }<br><br> /* close the file */<br> fclose(fp);<br> return 1;<br>}<br> <br><br>In C++<br><br> <br>#include <iostream><br>#include <fstream><br><br>// a pointer which will hold the location of the array <br>float *SomeArray = NULL;<br><br>// the number of elements in the array <br>unsigned int ArraySize = 0;<br><br>bool Load(char *filename) {<br><br> std::ifstream ifs(filename);<br><br> // check if file opened <br> if( !ifs )<br> {<br> std::cerr << "Could not open file" << std::endl;<br> return false;<br> }<br><br> // de-allocate old data <br> delete [] SomeArray;<br> SomeArray = NULL;<br><br> // read the number of values <br> ifs >> ArraySize;<br><br> // allocate memory for array <br> SomeArray = new float[ ArraySize ];<br><br> // check allocation ok <br> if( !SomeArray )<br> {<br> std::cerr << "Error, memory allocation failed" <br> << std::endl;<br> return false;<br> }<br><br> // read in each variable <br> for( int i=0; i<ArraySize; ++i )<br> {<br> ifs >> SomeArray;<br> }<br><br> // close the file <br> ifs.close();<br><br><br> return true;<br>}<br><br>bool Save(char *filename) {<br><br> std::ofstream ofs(filename);<br><br> // check if file opened <br> if( !ofs )<br> {<br> std::cerr << "Could not open file" << std::endl;<br> return false;<br> }<br><br> // write the number of values <br> ofs << ArraySize << std::endl;<br><br> // read in each variable <br> for( int i=0; i<ArraySize; ++i )<br> {<br> ofs << SomeArray << std::endl;<br> }<br><br> // close the file<br> ofs.close();<br><br> return true;<br>}<br> <br><br><br>In C++ using STL<br><br> <br>#include <iostream><br>#include <fstream><br>#include <vector><br><br><br>std::vector<float> SomeArray;<br><br>bool Load(char *filename) {<br><br> std::ifstream ifs(filename);<br><br> // check if file opened <br> if( !ifs )<br> {<br> std::cerr << "Could not open file" << std::endl;<br> return false;<br> }<br><br> int ArraySize=0;<br><br> // read the number of values <br> ifs >> ArraySize;<br><br> // resize the array <br> SomeArray.resize(ArraySize);<br><br> // read in each variable <br> std::vector<float>::iterator it = SomeArray.begin();<br> for( ; it != SomeArray.end(); ++it )<br> {<br> ifs >> *it<br> }<br><br> // close the file <br> ifs.close();<br><br> return true;<br>}<br><br>bool Save(char *filename) {<br><br> std::ofstream ofs(filename);<br><br> // check if file opened <br> if( !ofs )<br> {<br> std::cerr << "Could not open file" << std::endl;<br> return false;<br> }<br><br> // write the number of values */</font><br></font><br> ofs << SomeArray.size() << std::endl;<br><br> <font color="gray">// write out each variable <br></font><br> std::vector<float>::iterator it = SomeArray.begin();<br> <font color="blue">for</font>( ; it != SomeArray.end(); ++it )<br> {<br> ofs << *it << std::endl;<br> }<br><br> <font color="gray"><font color="gray"><font color="gray">// close the file <br></font></font></font><br> ofs.close();<br><br> <font color="blue">return</font> true;<br>}<br> </pre></DIV><!–ENDSCRIPT–><br><br><br><SPAN CLASS=editedby>[edited by - Rob The Bloke on February 2, 2003 5:49:42 PM]</SPAN> <br><br>Nice, that what i like to see <img src="smile.gif" width=15 height=15 align=middle>
THX it was C++
what is STL?
How must be the .map file? if multirows, if must have space or parenthesis or in one row just one number....
THX
what is STL?
How must be the .map file? if multirows, if must have space or parenthesis or in one row just one number....
THX
Although the other suggestions I''ve seen are good, they seem a bit umm over the top for such a simple thing.
Now if you want variable size maps then well you''ll need to write out an int value containing the size of the array before you write the data so that you know what size to read in.
Oh and please don''t bump this again, you''ve got more than one answer which will work.
const int MAX_SIZE = 500;char array[MAX_SIZE];// WriteFILE fp = fopen("something.map","w");fwrite((void*)&array,sizeof(char)*MAX_SIZE,1,fp);fclose(fp);// Read.FILE fp = fopen("something.map","r");fread((void*)&array,sizeof(char)*MAX_SIZE,1,fp);fclose(fp);
Now if you want variable size maps then well you''ll need to write out an int value containing the size of the array before you write the data so that you know what size to read in.
Oh and please don''t bump this again, you''ve got more than one answer which will work.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement