Results 1 to 4 of 4

Thread: Opening and Reading a file, easy question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile Opening and Reading a file, easy question

    Hi All,

    Just a quick question.
    I have recently created this mini grid game using the a char [10] [10]. I want to make it so that it reads from a file at the start of my game and load the values into the [10][10] char.
    the actual file, info.dat will have all 100 chars on one line as i thought it would be easier to retrieve, so the file would look something like..
    B EDF EGD JHD etc
    the program must read and include the spaces into the 10*10 Char as well, so basically it just reads on char from the file then writes it to the first array then the second one and so on right until it's added all of the letters/spaces to the 10*10 char.
    I've worked out most of the stuff I just need some help to fill in the gaps....

    #include <fstream.h>

    int disp[10][10]
    ifsteam thefile;

    int main()
    {
    thefile.open("info.dat");
    //add a for statement that keeps going until it's read all 100 chars
    //add another for statement to keep going until the end of the row then move to the next one.
    thefile.close();


    Any help would be greatly appreciated!

    Thanks.

    Regards,

    Smithy.

  2. #2
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    Code:
    #include <fstream.h> 
    #include <iostream>
    
    int main() 
    {
    int disp[10][10]; 
    char ch; 
    int i;
    
    ifstream in("info.dat", ios::in | ios::binary);
    ofstream out("info.dat",ios::out | ios::binary);
    
    for(i=0;i<100;i++){
        in.get(ch);
        out.put(disp[i][i]);
    }
    
    in.close();
    out.close();
    return 0;
    }
    Matt

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It should be fstream not fstream.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    East Ballina,NSW,Australia
    Posts
    121

    Smile Thanks

    Hi,

    Its OK now, I've used a different method which works quite nicely.

    thanks for your efforts though!

    Regards,

    Smithy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width