• Advertisement

Archived

This topic is now archived and is closed to further replies.

How can i read array by a file?

This topic is 5495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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[ i ]);
}

/* close the file */
fclose(fp);
return 1;
}

int Save(char *filename) {

/* open the file for writing */
FILE *fp = fopen(filename,"w");

/* check if file opened */
if( fp == NULL )
{
fprintf(stderr,"Error: could not open file for writing\n");
return 0;
}

/* print the number of values */
fprintf(fp,"%d\n",ArraySize);

/* read in each variable */
for( i=0; i<ArraySize; ++i )
{
fprintf(fp,"%f\n",&SomeArray[ i ]);
}

/* close the file */

fclose(fp);
return 1;
}


In C++


  
#include <iostream>
#include <fstream>

// a pointer which will hold the location of the array

float *SomeArray = NULL;

// the number of elements in the array

unsigned int ArraySize = 0;

bool Load(char *filename) {

std::ifstream ifs(filename);

// check if file opened

if( !ifs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// de-allocate old data

delete [] SomeArray;
SomeArray = NULL;

// read the number of values

ifs >> ArraySize;

// allocate memory for array

SomeArray = new float[ ArraySize ];

// check allocation ok

if( !SomeArray )
{
std::cerr << "Error, memory allocation failed"
<< std::endl;
return false;
}

// read in each variable

for( int i=0; i<ArraySize; ++i )
{
ifs >> SomeArray[ i ];
}

// close the file

ifs.close();


return true;
}

bool Save(char *filename) {

std::ofstream ofs(filename);

// check if file opened

if( !ofs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// write the number of values

ofs << ArraySize << std::endl;

// read in each variable

for( int i=0; i<ArraySize; ++i )
{
ofs << SomeArray[ i ] << std::endl;
}

// close the file

ofs.close();

return true;
}



In C++ using STL


  
#include <iostream>
#include <fstream>
#include <vector>


std::vector<float> SomeArray;

bool Load(char *filename) {

std::ifstream ifs(filename);

// check if file opened

if( !ifs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

int ArraySize=0;

// read the number of values

ifs >> ArraySize;

// resize the array

SomeArray.resize(ArraySize);

// read in each variable

std::vector<float>::iterator it = SomeArray.begin();
for( ; it != SomeArray.end(); ++it )
{
ifs >> *it
}

// close the file

ifs.close();

return true;
}

bool Save(char *filename) {

std::ofstream ofs(filename);

// check if file opened

if( !ofs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// write the number of values */

ofs << SomeArray.size() << std::endl;

// write out each variable

std::vector<float>::iterator it = SomeArray.begin();
for( ; it != SomeArray.end(); ++it )
{
ofs << *it << std::endl;
}

// close the file

ofs.close();

return true;
}



[edited by - Rob The Bloke on February 2, 2003 5:49:42 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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[ i ]);
}

/* close the file */
fclose(fp);
return 1;
}

int Save(char *filename) {

/* open the file for writing */
FILE *fp = fopen(filename,"w");

/* check if file opened */
if( fp == NULL )
{
fprintf(stderr,"Error: could not open file for writing\n");
return 0;
}

/* print the number of values */
fprintf(fp,"%d\n",ArraySize);

/* read in each variable */
for( i=0; i<ArraySize; ++i )
{
fprintf(fp,"%f\n",&SomeArray[ i ]);
}

/* close the file */
fclose(fp);
return 1;
}


In C++


#include <iostream>
#include <fstream>

// a pointer which will hold the location of the array
float *SomeArray = NULL;

// the number of elements in the array
unsigned int ArraySize = 0;

bool Load(char *filename) {

std::ifstream ifs(filename);

// check if file opened
if( !ifs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// de-allocate old data
delete [] SomeArray;
SomeArray = NULL;

// read the number of values
ifs >> ArraySize;

// allocate memory for array
SomeArray = new float[ ArraySize ];

// check allocation ok
if( !SomeArray )
{
std::cerr << "Error, memory allocation failed"
<< std::endl;
return false;
}

// read in each variable
for( int i=0; i<ArraySize; ++i )
{
ifs >> SomeArray[ i ];
}

// close the file
ifs.close();


return true;
}

bool Save(char *filename) {

std::ofstream ofs(filename);

// check if file opened
if( !ofs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// write the number of values
ofs << ArraySize << std::endl;

// read in each variable
for( int i=0; i<ArraySize; ++i )
{
ofs << SomeArray[ i ] << std::endl;
}

// close the file
ofs.close();

return true;
}



In C++ using STL


#include <iostream>
#include <fstream>
#include <vector>


std::vector<float> SomeArray;

bool Load(char *filename) {

std::ifstream ifs(filename);

// check if file opened
if( !ifs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

int ArraySize=0;

// read the number of values
ifs >> ArraySize;

// resize the array
SomeArray.resize(ArraySize);

// read in each variable
std::vector<float>::iterator it = SomeArray.begin();
for( ; it != SomeArray.end(); ++it )
{
ifs >> *it
}

// close the file
ifs.close();

return true;
}

bool Save(char *filename) {

std::ofstream ofs(filename);

// check if file opened
if( !ofs )
{
std::cerr << "Could not open file" << std::endl;
return false;
}

// write the number of values */


ofs << SomeArray.size() << std::endl;

// write out each variable

std::vector<float>::iterator it = SomeArray.begin();
for( ; it != SomeArray.end(); ++it )
{
ofs << *it << std::endl;
}

// close the file

ofs.close();

return true;
}



<SPAN CLASS=editedby>[edited by - Rob The Bloke on February 2, 2003 5:49:42 PM]</SPAN>

Nice, that what i like to see

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Although the other suggestions I''ve seen are good, they seem a bit umm over the top for such a simple thing.


  

const int MAX_SIZE = 500;
char array[MAX_SIZE];

// Write

FILE 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.


Share this post


Link to post
Share on other sites

  • Advertisement