stream to a 2d array(beginner help)

I

isaac2004

hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;

}

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like

char ** in_array;
in_array = new *int[height];

then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.
 
J

Jim Langston

isaac2004 said:
hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;

}

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like

char ** in_array;
in_array = new *int[height];

then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.

You never specified what type of data was in the file. Numbers, letters,
characters, what.

Also, this sounds a lot like homework. Homework we tend not to give code
for but pointers when someone has shown some effort.
 
G

Guest

hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;

}

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like

char ** in_array;
in_array = new *int[height];

Is the data in the file somehow arranged as an array or how are you
supposed to know how large the array should be? The most important thing
when reading data from a file is to know how it is stored, and you have
not said a word about the data.

BTW: When it comes to multi-dimensional arrays it might sometimes be
easier to simulate them using a normal array that is height*width large
and calculate the correct index.
 
J

Jonathan Lane

hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream file;
string name;

cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();

while (!file.fail()) {
cout << c;
c = file.get();
}

file.close();

return 0;

}

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like

char ** in_array;
in_array = new *int[height];

then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.

You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.
 
I

isaac2004

hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
string name;
cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();
while (!file.fail()) {
cout << c;
c = file.get();
}

return 0;

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like
char ** in_array;
in_array = new *int[height];
then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.

You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.

the problem is a cell automation game of life type problem. the input
file looks like

*****
**** ***
*** ***

i know how to put it into an array of char but its the double loop im
having a problem with
 
J

Jim Langston

isaac2004 said:
hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
string name;
cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();
while (!file.fail()) {
cout << c;
c = file.get();
}

return 0;

This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like
char ** in_array;
in_array = new *int[height];
then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.

You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.

the problem is a cell automation game of life type problem. the input
file looks like

*****
**** ***
*** ***

i know how to put it into an array of char but its the double loop im
having a problem with

Instead of an array of char, I would probably go with a vector of
std::string. Either way, it's about the same.

clear the string.
while not end of file
read a character.
if it's end of line
push the string onto our vector
clear the string
else
add the character to our string

Now, this is using a vector of strings, but you can also juse a vector of
vectors
or a two dimentional char array

Again, I'm not showing code because I think this is homework.
 
I

isaac2004

hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file into
a 2d array. I have this to get the file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
string name;
cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();
while (!file.fail()) {
cout << c;
c = file.get();
}
file.close();
return 0;
}
This just grabs and couts the file. What I want to do is grab the file
and put it into a 2d array. I know it has to be like
char ** in_array;
in_array = new *int[height];
then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.
You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.
the problem is a cell automation game of life type problem. the input
file looks like
*****
**** ***
*** ***
i know how to put it into an array of char but its the double loop im
having a problem with

Instead of an array of char, I would probably go with a vector of
std::string. Either way, it's about the same.

clear the string.
while not end of file
read a character.
if it's end of line
push the string onto our vector
clear the string
else
add the character to our string

Now, this is using a vector of strings, but you can also juse a vector of
vectors
or a two dimentional char array

Again, I'm not showing code because I think this is homework.- Hide quoted text -

- Show quoted text -

i understand the algorithm, i just dont know how to setup a two dim
array. is it just

char ** in_array;

for(i= 0; i < height; i++)
{
for(j= 0; i < width; j++)
{
in array = file.get(i,j);
}
}

or is it something more than that. yes it is homework and i understand
that providing code isnt cool but its the implementation that i am
having issues with. thanks for any help
 
Joined
Oct 16, 2007
Messages
4
Reaction score
0
new in side the loop

once you have the char ** ptr you have to basically treat it like an array of pointers. to arrays. so you declare you inital array of char*, the size of this array is your first demension. then loop through each element in that array and create arrays of chars. you will use new for the array of char * and new for each array of char's.

Best i can explain without just doing it for you.
 
J

Jim Langston

isaac2004 said:
On Oct 16, 4:24 am, Jonathan Lane <[email protected]>
wrote:
On Oct 16, 10:07 am, isaac2004 <[email protected]> wrote:
hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file
into
a 2d array. I have this to get the file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
string name;
cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();
while (!file.fail()) {
cout << c;
c = file.get();
}

return 0;

This just grabs and couts the file. What I want to do is grab the
file
and put it into a 2d array. I know it has to be like
char ** in_array;
in_array = new *int[height];
then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.
You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.
the problem is a cell automation game of life type problem. the input
file looks like
*****
**** ***
*** ***
i know how to put it into an array of char but its the double loop im
having a problem with

Instead of an array of char, I would probably go with a vector of
std::string. Either way, it's about the same.

clear the string.
while not end of file
read a character.
if it's end of line
push the string onto our vector
clear the string
else
add the character to our string

Now, this is using a vector of strings, but you can also juse a vector of
vectors
or a two dimentional char array

Again, I'm not showing code because I think this is homework.- Hide
quoted text -

- Show quoted text -

i understand the algorithm, i just dont know how to setup a two dim
array. is it just

char ** in_array;

for(i= 0; i < height; i++)
{
for(j= 0; i < width; j++)
{
in array = file.get(i,j);
}
}

or is it something more than that. yes it is homework and i understand
that providing code isnt cool but its the implementation that i am
having issues with. thanks for any help

char ** in_array; is a pointer to a pointer of char. Now,pointers can be
used as arrays.
If we wanted an array of characters, we would set it up as a pointer to a
char. if we wanted an array of ints, a pointer to an int, and so on. a
pointer to whatever you want an array of. In this case you want an array of
an array of chars, so a pointer to a pointer of char. char ** in_array,
however, is not a two dimentational array.

A two dimentional array is like
char foo[10][10];
A pointer to get to this data is simply
char* bar;
and you have to manually calculate the rows and columns.

what char ** in_array sets up is an array of char pointers.
each char pointer can point ot memory for a char array. This is differnt
from a 2 dmentational array in that a 2 dimentional array has it's memory
continuous. So, basically, you're going ot need to figoure out how many
lines you are going to need, and set enough memory aside for that many
pointers.
Then point each pointer to some memory

Which is a pain in the behind, which is why it's better to use a vector of
vector or vector of string. Or even a vector ofy our own class for each
line.
 
J

Jim Langston

isaac2004 said:
On Oct 16, 4:24 am, Jonathan Lane <[email protected]>
wrote:
On Oct 16, 10:07 am, isaac2004 <[email protected]> wrote:
hello, i posted with a topic like this but got no real feedback(prob
cuz of lapse in my explanation) so i am trying it again. i am trying
to set up a function that brings in a txt file and adds the file
into
a 2d array. I have this to get the file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file;
string name;
cout << "File name? ";
cin >> name;
file.open(name.c_str());
if (file.fail()) {
cout << "Could not open " << name << ".\n";
return 1;
}
char c;
c = file.get();
while (!file.fail()) {
cout << c;
c = file.get();
}

return 0;

This just grabs and couts the file. What I want to do is grab the
file
and put it into a 2d array. I know it has to be like
char ** in_array;
in_array = new *int[height];
then some looping construct and that is what I am having a problem
with, Thank you for anyhelp.
You already have the looping construct in your sample code. You might
want to keep a counter of which iteration of the loop you're on. You
also need to decide on how you determine which element of the arrays
you want to put each element into.
the problem is a cell automation game of life type problem. the input
file looks like
*****
**** ***
*** ***
i know how to put it into an array of char but its the double loop im
having a problem with

Instead of an array of char, I would probably go with a vector of
std::string. Either way, it's about the same.

clear the string.
while not end of file
read a character.
if it's end of line
push the string onto our vector
clear the string
else
add the character to our string

Now, this is using a vector of strings, but you can also juse a vector of
vectors
or a two dimentional char array

Again, I'm not showing code because I think this is homework.- Hide
quoted text -

- Show quoted text -

i understand the algorithm, i just dont know how to setup a two dim
array. is it just

char ** in_array;

for(i= 0; i < height; i++)
{
for(j= 0; i < width; j++)
{
in array = file.get(i,j);
}
}

or is it something more than that. yes it is homework and i understand
that providing code isnt cool but its the implementation that i am
having issues with. thanks for any help

Incidently,ifyou knew before hand that you had 10 lines each with 10
characters you could just declare it

char in_array[10][10];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top