-1

I'm given a text file containing information about a game world and it's collision data in this format.

 Width 5
 Height 5
 10001
 11000
 11100
 11111
 11111

To store the data, I'm given

static int BINARY_MAP_WIDTH;
static int BINARY_MAP_HEIGHT;

and

static int **MapData; // Dynamic array of map data

My FileIO knowldege doesn't go much beyond reading in strings from a file line by line.

So far I have this very roundabout way of reading in just the first two lines.

FILE *Data;

int line = 1; // line number that we're on

Data = fopen(FileName, "rt");

if (!Data)
    return 0;

if (Data)
{
    while (!feof(Data))
    {
        if (line == 1)
            fscanf(Data, "%*[^0-9]%d%n", &BINARY_MAP_WIDTH);

        if (line == 2)
            fscanf(Data, "%*[^0-9]%d%n", &BINARY_MAP_HEIGHT);

        if (line > 2)
            break;

        line++;
    }
}

And to be quite honest, I'm not entirely sure why it's working, but I am getting the correct values into the variables.

I know how to set up the dynamic array, at this point my issue is with reading in the correct values. I'm not sure where to go from here.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • This appears to be an assignment, and while we don't have any rule against assignments in general, the usual question is: have you tried hard enough? Having this question answered with a code snippet will probably hinder your learning process. – Stefano Sanfilippo Feb 19 '15 at 23:09
  • [while-feof-file-is-always-wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). Start by reading the second answer to that question. – user3386109 Feb 19 '15 at 23:10
  • It's an assignment, but not an assignment on FileIO. We went over it briefly the 1st semester and this is a completely different use of it. I'm just trying to understand how fscanf works. Or maybe how I can skip to the 3rd line and start reading in the data from that point. I've looked at various resources online. I'm not asking anybody to write code for me, I just want a sort of a psuedocoded way to handle it. – Jondy Feb 19 '15 at 23:14

1 Answers1

0

Here's a couple things you need to know about fscanf

  1. fscanf will consume as many bytes as necessary to perform the requested conversions and will update the file pointer accordingly. The next call to fscanf will start at the location in the file where the previous fscanf ended.
  2. fscanf returns the number of successful conversions, so you should verify that the return value is equal to the number of conversions requested.

So here's how I would rewrite the code you have so far

#include <stdio.h>

static int mapWidth;
static int mapHeight;

int readFromFile( char *name )
{
    FILE *fp;
    int good = 1;

    if ( (fp = fopen(name, "r")) == NULL )
        return 0;

    if ( fscanf(fp, "%*[^0-9]%d", &mapWidth) != 1 )
        good = 0;
    if ( fscanf(fp, "%*[^0-9]%d", &mapHeight) != 1 )
        good = 0;

    if ( good )
    {
        // the code to read the rest of the file goes here
    }

    fclose( fp );
    return good;
}

int main( void )
{
    if ( readFromFile( "input.txt" ) )
        printf( "%d %d\n", mapWidth, mapHeight );
    else
        printf( "readFromFile failed\n" );
}

The next step is to figure out

  • how to allocate memory for MapData based on the width and height
  • how to read the rest of the lines in a loop, e.g. using fgets or fscanf(..."%s"...)
  • how to parse those lines to fill in the MapData
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • Thank you. I think I got it to do what I want. So basically fscanf() skips all the text, stops on the number (because of the conditions i supplied it) and then continues at that spot on the next call to fscanf()? – Jondy Feb 20 '15 at 00:45