File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; ok i am just starting to learn about file reading/writing. Of all the tutorials i have read none tell you ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Consider this time waiting for an answer an excellent opportunity to go read the FAQ then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    Last edited by elad; 10-01-2004 at 11:34 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    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)

    thx for your patients

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote 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)

    thx for your patients
    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.
    What is C++?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    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?

    thx agian for your patients

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.
    Last edited by elad; 10-02-2004 at 02:16 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    Thx elad that helped alot

    and thx everyone else

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    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. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    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. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    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)
    Last edited by c++.prog.newbie; 10-04-2004 at 09:06 AM.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21