Hi,

Can someone plz help me with copying the files contents into an array.

the file contents maybe like...

doit.dsp
doit2.dsw
doit3.cpp

I want them to be copied to an array.

Thank you

Recommended Answers

All 4 Replies

Why not a vector of strings?

You have to be careful since .dsp and .dsw files are binary if I remember right. That limits you to a container of unsigned char and unformatted binary input:

std::vector<unsigned char> v;
unsigned char c;

std::ifstream s1("doit.dsp", std::ios::binary);

while (s1.get(c))
  v.push_back(c);

Anything else risks undefined behavior or unwanted character conversions. Can you be more specific about what you're trying to do?

they may be text files also... i just want the list of those text files.. to an array.

thnxk you

A list of filenames is very different from an array of file contents. The former is very easy and becomes harder as you get lower level. A vector of strings is best:

#include <string>
#include <vector>

std::vector<std::string> v;

v.push_back("doit.dsp");
v.push_back("doit2.dsw");
v.push_back("doit3.cpp");

Followed by a boost array of strings:

#include <string>
#include <boost/array.hpp>

boost::array<std::string, 3> a;

a[0] = "doit.dsp";
a[1] = "doit.dsw";
a[2] = "doit.cpp";

Then an array of strings:

#include <string>

std::string a[3];

a[0] = "doit.dsp";
a[1] = "doit.dsw";
a[2] = "doit.cpp";

Then an array of arrays of char:

#include <cstring>

char a[3][9];

strcpy(a[0], "doit.dsp");
strcpy(a[1], "doit.dsw");
strcpy(a[2], "doit.cpp");

Then lastly, an array of pointers to char:

#include <cstring>

char *a[3];

a[0] = new char*[9];
strcpy(a[0], "doit.dsp");
a[1] = new char*[9];
strcpy(a[1], "doit.dsw");
a[2] = new char*[9];
strcpy(a[2], "doit.cpp");

// ...

delete [] a[2];
delete [] a[1];
delete [] a[0];

Is that what you're trying to do? Because I'm getting mixed messages. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.