PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > stream to a 2d array(beginner )
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
stream to a 2d array(beginner )

Réponse
 
LinkBack Outils de la discussion
Vieux 16/10/2007, 10h07   #1
isaac2004
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut stream to a 2d array(beginner )

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

  Réponse avec citation
Vieux 16/10/2007, 11h59   #2
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

"isaac2004" <isaac_2004@yahoo.com> wrote in message
news:1192525656.076872.143890@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 any.


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.



  Réponse avec citation
Vieux 16/10/2007, 12h24   #3
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

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
  Réponse avec citation
Vieux 16/10/2007, 12h24   #4
Jonathan Lane
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

On Oct 16, 10:07 am, isaac2004 <isaac_2...@yahoo.com> 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];
>
> then some looping construct and that is what I am having a problem
> with, Thank you for any.


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.

  Réponse avec citation
Vieux 16/10/2007, 18h05   #5
isaac2004
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

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

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

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

  Réponse avec citation
Vieux 16/10/2007, 21h17   #6
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

"isaac2004" <isaac_2004@yahoo.com> wrote in message
news:1192554310.636266.51910@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.com> 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];

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

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


  Réponse avec citation
Vieux 16/10/2007, 21h40   #7
isaac2004
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> "isaac2004" <isaac_2...@yahoo.com> wrote in message
>
> news:1192554310.636266.51910@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.com> 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];

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

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

  Réponse avec citation
Vieux 17/10/2007, 11h04   #8
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )

"isaac2004" <isaac_2004@yahoo.com> wrote in message
news:1192567247.740542.165080@v29g2000prd.googlegr oups.com...
> On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> "isaac2004" <isaac_2...@yahoo.com> wrote in message
>>
>> news:1192554310.636266.51910@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.com> 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];

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

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


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.



  Réponse avec citation
Vieux 17/10/2007, 11h05   #9
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: stream to a 2d array(beginner )


"isaac2004" <isaac_2004@yahoo.com> wrote in message
news:1192567247.740542.165080@v29g2000prd.googlegr oups.com...
> On Oct 16, 1:17 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
>> "isaac2004" <isaac_2...@yahoo.com> wrote in message
>>
>> news:1192554310.636266.51910@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.com> 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];

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

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


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

char in_array[10][10];


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h22.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,24342 seconds with 17 queries