1. ## File I/O

ok i am just starting to learn about file reading/writing. Of all the tutorials i have read none tell you how to input data into an array.

say you have a file with the following data in it

xxxxx
xxxxx
xxxxx

and you want to put that into an array map[x][y]
so
Code:
```map[0][] = xxxxx
map[1][] = xxxxx
map[2][] = xxxxx```
would it be
Code:
```while(!file.eof())
{
file >> map[x][y];
x++ ; y++;
}```
(i know that f.eof() is not supposed to be used but i havent found anything else to use(i havent got to reading the FAQ's yet but i know its in there))

thx

2. Consider this time waiting for an answer an excellent opportunity to go read the FAQ then

3. you handle data entry into an array the same way, whether the data comes from user input, programmer input, or a file. Here's one approach for a 2D array of ints.
Code:
```
//declare variables
const int FIRST = 5;
const int SECOND = 5;
int myArray[FIRST][SECOND];

int x, y, z;

//will use x and y to locate where to put input
x = 3;
y = 2;

//will use file input
//associate file with stream
ifstream fin("test.txt");

//make sure file opened
if(!fin)
{
cerr << "failed to open file" << endl;
exit(0);
}
else
{
//read in first item in file, if not empty
if(fin >> z)
//assign z to location in myArray
myArray[x][y] = z;
}```
There are several other factors you will probably want to take into account, such as: reading more than one item from file into the array in a sequential fashion, making sure you are not overwriting the bounds of the array, etc.

4. when you do fin >> z does that just read the first character/int or does it read the first line?
say you have zzzzz would it read just z or zzzzz? if it just reads z would i have to go (fin>>z++) for the next character?
(im not at my home computer right now)

5. Originally Posted by c++.prog.newbie
when you do fin >> z does that just read the first character/int or does it read the first line?
say you have zzzzz would it read just z or zzzzz? if it just reads z would i have to go (fin>>z++) for the next character?
(im not at my home computer right now)

fin >> z would read up to the first space I believe (note that a varialbe of char type can only hold one character).

So if you have "zzzzz" in a file it would read it, but if it was "zz zz".. it would only read "zz".

Now as far as numbers.. again it will read up to the first space.

6. depends on what z is.

If z is an int then fin >> z will input all digits relative to the int. If z is a char then fin >> z will only input the first char. If z is a string then fin >> z will input all char up to the first whitespace char.

So, if z is a char and input is wwwwww, then fin >> z will result in z being w. Whereas if z is a string then fin >> z will place wwwwww, with a null char immediately after the last w, in z. Eventhough you will never "see" the terminating null char at the end of z, you need to be sure there is room for it when you declare memory for the string.

7. ok so if i do what is posted above with array[x][y] would that store "12345" in x =1 y=1 or x=1 y=1-5 (if you get that)

and then how would i get to the second line of the file?

8. assuming array[][] is an array of int and 12345 is a single int (twelve thousand three hundred forty five) then 12345 could be stored in any element of array[][] by selecting the appropriate indices. If it's array[x][y] with x = 1 and y = 1 then 12345 would be stored in the second column of the second row.

If array[][] were an array of char and 12345 were a string, then each char (that is each digit) would be stored in a separate element (would have it's own set of indices). If it's array[x][y] and x is 1 and y starts at 0, then the elements would be as follows:
array[1][0] = '1';
array[1][1] = '2';
array[1][2] = '3';
array[1][3] = '4';
array[1][4] = '5';
array[1][5] = '\0';

Reading a file depends on how it is set up. If you have a single int on a single line in the file and you know how many lines there are in the file then you could do something like this:

for(x = 0; x < lines; ++x)
fin >> array[x];

Often you don't know how many things there are in a file but you know the set up of the file. Let's say the file again is a single int per line. In that case, you want to place a new value in the array as long as you haven't come to end of file and you haven't used all array elements :

while(fin >> z && x < sizeOfArray)
array[x++] = z;

There is (almost) an infinite variety of syntax that can be used based on the setup of the file, where you want the informtation to go, etc. The basic principle is that you use loop(s) to control how many times you read in some information.

9. Thx elad that helped alot

and thx everyone else

10. ok everything is working good. but how do i get the program to use a different character for the end of the line other than a " " such as a end of line ("\n") instead.

my input file would look like
Code:
```|                          |
|           f              |```
but when the program outputs it to screen it looks like
Code:
```|
|
|
f
|```
any suggestions (if you understand the question)

thx alot

11. Goal: print text file to screen in same format as in file. File format is 13 char per line, 2 lines per file.
Code:
```Version 1.  //know everything about the file
char z;
for(int j = 0; j < 2; ++j)
{
for(int i = 0; i < 13; ++i)
{
fin >> z;
cout << z;
}
}

Version 2.  Don't know everything about the file.
char z;
while(fin >> z)
{
fin >> z;
cout << z;
}```
Suspect you have added a newline char to file contents after each non-space char rather than just reading in each char and displaying it to screen.

12. so in the above does it read 1 character at a time all the way through? and could i replace char z with Map[j][i];

thx (im not at my home comp right now)

13. Yes, my last post reads file 1 char at a time and outputs it to screen. If Map[j][i] is a char, then code should work the same. Other versions are probably available as well. If Map[j][i] is some other type, then modifications/variations will need to be made. It all depends on exactly what you are trying to do with what.

14. ok yes Map[][] is a char. what i am trying to do is read the file and save it to an array for use in a text RPG were you can move a character around the array
Code:
```+-------------+
|             |
|  O          |
|             |
|             |
|             |
+-------------+```
oh and while i got you here is there a way to change the value of one of the chars in the array temporarily? (so the person can move around and when he moves what was in the spot before him stays there)

15. Receive user input in a holding variable

Store char currently at user input in a different holding variable.

assign user input to appropriate spot in array.

display array.

clear screen.

replace second holding variable in array

repeat as many times as you want with a loop

Probably shorter/more elegant solutions available as well.