3 Replies - 1352 Views - Last Post: 03 November 2011 - 07:02 PM Rate Topic: -----

#1 dekker13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 12-September 11

Reading whitespace characters into 2D array?

Posted 03 November 2011 - 05:54 PM

By default, file input ignores white space. However I need to copy that file exactly the way it looks in the picture provided in the link >> http://i44.tinypic.com/n6t2ci.png

This is part of a larger project of recursively counting those blobs.
I need to read in a text file and count how many "blobs" there are (6 blobs in this file). I'm reading it in but when I print it to standard output...all the "X"'s are together and not like the picture from the text file. I have a slight idea of what I would need to do but I'm getting strange results. Hints?

---------------------------------------
int main (void)
{

    ifstream inFile;
    inFile.open("blob.txt");

    if (!inFile)
    { 
        cout << "Nothing here. " << endl;
    }

    char matrix [ROWS][COLUMNS];


int rowArea = 0;
int colArea = 0;

    for (int i=1; i<ROWS; i++)
    {
        for (int j=1; j<COLUMNS; j++)
        {
            inFile >> matrix[i][j];
        }
    }

    for (int r = 1; r < ROWS; r++)
        {
            for (int c = 1; c < COLUMNS; c++)
            {  
                cout << matrix[r][c]; 
            }
                                   
            cout << endl;
        }
 

    return 0;
}

This post has been edited by jimblumberg: 03 November 2011 - 08:05 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Reading whitespace characters into 2D array?

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Reading whitespace characters into 2D array?

Posted 03 November 2011 - 06:05 PM

Use

inFile.get(matrix[i][j]);



or

inFile.read(&matrix[i][j],1);



They are more low level functions that read 1 character or a given number of characters respectively.
Was This Post Helpful? 1
  • +
  • -

#3 dekker13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 12-September 11

Re: Reading whitespace characters into 2D array?

Posted 03 November 2011 - 06:26 PM

Excellent! Thank you buddy! :bananaman: I did not know there was a function with ifstream.
Was This Post Helpful? 0
  • +
  • -

#4 dekker13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 12-September 11

Re: Reading whitespace characters into 2D array?

Posted 03 November 2011 - 07:02 PM

I added that code and printed the file out. The output looks spaced out.
http://i42.tinypic.com/63zsz5.png

Will that affect the recursive function that counts the blobs I mentioned in the original post?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1