#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12

    input types from input files?


    I'm working on a problem that i've run out of resources to look. I am writing an assignment and cannot figure out how to differentiate between the inputs in an input file. I am allocating a 2d dynamic array that should hold the values of something that looks like a tic tac toe game. The input file starts with two ints (rows,and columns) and the following lines are X's,O's and blanks. What function can i use to get the blank spaces as well as the characters into the array, and then determine if the file is /eof or has encountered another int(the start of the next 2d array). Any ideas? Would a usage of getline() work ?
    Thanks,

    Jonathan Cobb
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Try reading the whole file to a char buffer and reading each character individually.

    Is it a text file?
    Can you find its size?
    Is the size inc space for a terminator but excluding the Row and Col numbers ((Row*Col)+1)*sizeof(char) ?

    then find the spaces with isspace()


    It may be easier (if possible) to change the way you write the file to make it easier to read back. ie write in binary using a structure.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Austin TX yall
    Posts
    42
    Rep Power
    12
    the file is .txt and it could be anything. as of now it looks as such.http://www.cs.swt.edu/~js55/input1.txt

    the size can vary.
    thus far this is what i am working with. You will notice that i tried and commented out a function that is supposed to be with ctype.h called isalpha that is not working.
    Also what type should i be inputting? int or char. If i do int will it be the ascii code that i can then later use for determination as to whether they are x, o or blanks?

    int main()
    {

    char* array_PTR;
    int nrows;
    int ncolumns;
    char value;
    // int size;
    ifstream fin;
    fin.open("c:/temp/input1.txt");
    // Get the array size from the file
    fin >> nrows;
    fin >> ncolumns;
    // Try to create a 2D array of integers of this size
    // Note int[size][size] will NOT work
    array_PTR = new char[nrows*ncolumns];

    // Check if the computer has successfully reserved this memory
    // ie it has enough spare memory to reserve 4*size bytes for you
    if (array_PTR==0)
    {
    cout << "*** Memory could not be allocated ***";
    exit(1);
    }
    cout << "Created an array of " << nrows << " x " << ncolumns << " characters." << endl;

    // We can now use this array using a
    // one-to-one mapping and the reserved memory space
    for(int i=0; i<nrows; i++)
    {
    for(int j=0; j<ncolumns; j++)
    { fin >> value;
    //if isalpha(value);{
    //cout << "isalpha";
    array_PTR[i*nrows+j]= value;
    //else cout << "not a char";
    //}
    }

    }

    for( i=0; i<nrows; i++)
    {
    for(int j=0; j<ncolumns; j++)
    {
    cout << array_PTR[i*nrows+j]; // output all of the array
    }
    }


    // Unreserve memory once you have finished using it
    delete [] array_PTR;
    array_PTR=0;
    cout << "Released memory reserved for array." << endl;

    return 0;
    }
    Last edited by jonnyfive; September 12th, 2003 at 01:57 AM.
    Thanks,

    Jonathan Cobb

IMN logo majestic logo threadwatch logo seochat tools logo