|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
"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]; |
|
![]() |
| Outils de la discussion | |
|
|