New to fread()

This is a discussion on New to fread() within the C++ Programming forums, part of the General Programming Boards category; In my game engine (Allegro)... it is a tile map thing... Right now it is in an array like.. tile[0][0] ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    New to fread()

    In my game engine (Allegro)... it is a tile map thing... Right now it is in an array like..

    tile[0][0] = tile3;
    tile[1][0] = tile10;

    How can i read something like that from a file... thanks

    (Sorry... im bad at explaining what i want :P)

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    100
    ok... in other words... i have a file that has

    tile[0][0] = t_grass
    tile[1][0] = t_grass2

    ect...

    how can i read that file get the variables tile[0][0] to equal t_grass and tile[1][0] to equal t_grass2
    thamksssaf

  3. #3
    Unregistered
    Guest
    Uhhh....

    Code:
    tile[0][0] = t_grass;
    tile[1][0] = t_grass2;
    I don't understand what you're trying to do...

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    100
    read a text file... the text file has

    tile[0][0] = t_grass
    tile[1][0] = t_grass2

    How do i use c++ to READ it

  5. #5
    Registered User snowy101's Avatar
    Join Date
    May 2002
    Posts
    22

    this might help

    Elfjucie im not very good with c++ programming yet but i think i can give you some advice just gotta answer a few questions for me one if what your type inside the txt file or is that part of your code two you need to have commands to open the file and read it then close the file if you want more info just give me your e-mail and ill give you a few pointers what version of c++ are youusing and what type borland, visual..ect ?



    _____________________

    when candy is made so is a smile

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    You should use ifstream instead (fread() is a c function). Use it like:
    Code:
    ifstream ifs("filename.txt");
    char linebuf[80];
    
    ifs >> linebuf;
    this reads the first word (space-delimited) into the line buffer, and sets the file stream to the beginning of the next word. This means that linebuf == "tile[0][0]" if your textfile is exactly as you specified, and the next time you use:
    Code:
    ifs >> linebuf;
    it will put "=" into linebuf.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    I won't help you with fread or any file input... go read about it... but I can help you with how to manage your file and allt that...

    first of.. your map file need a header. this header tells you width and height of the map. read this in to memory and allocate the map array to match this size.

    every tile on the map is represented by a number, in this case a byte since we know there is max 256 tiles.

    your map-file will look something like this in an hex editor:

    1010 0101 0105 0501
    0105 060F 0303 0501

    the first two numbers is 16 and 16. height and width of the map. the rest is map data. you know that you should read 16 bytes into the x-axis of the maparray, and then increase the maparrays y-axis by one and read in more data....

    for(y = 0; y < MapHeight; y++){
    for(x = 0; x < MapWidth; x++){
    MapArray[y][x] = GetTileFromFile(x*y + x);
    }
    }

    a nicer way of doing this would be to binary write the the whole maparray to the file and have its dimension in the header. then you could read the whole map array back into an equally large maparray in one chunk...

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    100
    with ifstream... how can i make it read the next line?

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    100
    Can someone help please.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    100
    nevermind... i got it....

  11. #11
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    does anyone remember my tile engine a long time ago that was ran off of if statements? Man I was so stupid back then. I made a terribly SLOW tile engine that took like 2 pages to write, now I can make a fast one in < 40 lines. (I could make a smaller one, but I'm talking about one with some extra features and junk)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 01:45 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 12:56 PM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 05:30 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2007, 12:25 AM
  5. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 09:08 AM

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