473,569 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1872
"isaac2004" <is********@yah oo.comwrote in message
news:11******** **************@ t8g2000prg.goog legroups.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...@yah oo.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...@yah oo.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********@yah oo.comwrote in message
news:11******** *************@e 9g2000prf.googl egroups.com...
On Oct 16, 4:24 am, Jonathan Lane <jonathan.la... @googlemail.com >
wrote:
>On Oct 16, 10:07 am, isaac2004 <isaac_2...@yah oo.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...@rock etmail.comwrote :
"isaac2004" <isaac_2...@yah oo.comwrote in message

news:11******** *************@e 9g2000prf.googl egroups.com...


On Oct 16, 4:24 am, Jonathan Lane <jonathan.la... @googlemail.com >
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yah oo.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********@yah oo.comwrote in message
news:11******** **************@ v29g2000prd.goo glegroups.com.. .
On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
>"isaac2004" <isaac_2...@yah oo.comwrote in message

news:11******* **************@ e9g2000prf.goog legroups.com...


On Oct 16, 4:24 am, Jonathan Lane <jonathan.la... @googlemail.com >
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yah oo.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********@yah oo.comwrote in message
news:11******** **************@ v29g2000prd.goo glegroups.com.. .
On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
>"isaac2004" <isaac_2...@yah oo.comwrote in message

news:11******* **************@ e9g2000prf.goog legroups.com...
On Oct 16, 4:24 am, Jonathan Lane <jonathan.la... @googlemail.com >
wrote:
On Oct 16, 10:07 am, isaac2004 <isaac_2...@yah oo.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,ifyo u 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
1842
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 variable and store each one in the array. How do I do that? I plan (once I learn how) to compare the password input by the user to those stored...
5
2317
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
1393
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 there. Someone told me to look at the 'data stream' but i do not know what that is. Please help (specifics/explanations are always appreciated)
1
2346
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 maintain a queue of flights numbers. the program will be abel to do the following: Add a new flight number to the end of the queue (got it done)...
5
2195
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 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
8
1095
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.. thanks
13
1699
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 = (c == "y") ? p.offsetHeight+2 : 0; var left = (c == "x") ? p.offsetWidth +2 : 0;
1
1512
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 the array. I can't get my head around how i allocate it an array slot and then empty out the array in the checkout. I presume i'd need a loop or...
4
1853
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 one line. such as 60 40 10 10 all getting put into different variables. the problem i am having is
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.