473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

stream to a 2d array(beginner help)

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.

Oct 16 '07 #1
8 1861
"isaac2004" <is********@yahoo.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
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.

Oct 16 '07 #2
On 2007-10-16 11:07, isaac2004 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();
}

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.

--
Erik Wikström
Oct 16 '07 #3
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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.

Oct 16 '07 #4
On Oct 16, 4:24 am, Jonathan Lane <jonathan.la...@googlemail.com>
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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

Oct 16 '07 #5
"isaac2004" <is********@yahoo.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
On Oct 16, 4:24 am, Jonathan Lane <jonathan.la...@googlemail.com>
wrote:
>On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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.
Oct 16 '07 #6
On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"isaac2004" <isaac_2...@yahoo.comwrote in message

news:11*********************@e9g2000prf.googlegrou ps.com...


On Oct 16, 4:24 am, Jonathan Lane <jonathan.la...@googlemail.com>
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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

Oct 16 '07 #7
"isaac2004" <is********@yahoo.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"isaac2004" <isaac_2...@yahoo.comwrote in message

news:11*********************@e9g2000prf.googlegro ups.com...


On Oct 16, 4:24 am, Jonathan Lane <jonathan.la...@googlemail.com>
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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
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.

Oct 17 '07 #8

"isaac2004" <is********@yahoo.comwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"isaac2004" <isaac_2...@yahoo.comwrote in message

news:11*********************@e9g2000prf.googlegro ups.com...
On Oct 16, 4:24 am, Jonathan Lane <jonathan.la...@googlemail.com>
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.comwrote:
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
Incidently,ifyou knew before hand that you had 10 lines each with 10
characters you could just declare it

char in_array[10][10];
Oct 17 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christopher Richards | last post by:
I have a variable that stores a temporary password. I'm just learning about arrays. I know this is wrong $pwList= ($newPW); I want to add the value of the $newPW which keeps changing to a unique...
5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
1
by: gurumare | last post by:
Howdy, I am just learning c++ and of course i am doing the classic "gradeBook" program, but i am trying to figure out a way to save my data so that the next time my prog is opened it will still be...
1
by: aemazing | last post by:
hello, im new to the forum and i wanted to help with my c++ program. the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: William Morgan | last post by:
is there a way to take a string that has binary data in it and put it in the memory stream? everything i try will not work. it wants it in bytes. and i don't see a way to convert it over.. ...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
1
by: comedydave | last post by:
Hi guys, I'm new to ASP and need some array help. I need to have a shopping cart. When you visit the site it creates a session and an array. When you click add to basket it adds the item ID to...
4
by: isaac2004 | last post by:
hello, what i am trying to do is set up a loop that grabs multiple variables from an input file. my question is, is there a built in function in the istream class that can grab multiple values on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.