User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,545 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,335 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1640 | Replies: 17 | Solved
Reply
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Help Parallel Array/Reading In From A File

  #1  
Oct 14th, 2007
I have a file that is representing an image, here's the puddle.img file:
10 22
2222888222882222228882
2222888222882222228882
3333333333333333333333
1111111111111111111111
1111144444111111111111
1111444444411551111111
1114643434345551111111
1111333333311551111111
1111133333111111115114
1111111111111111115543

The first two numbers are the size of the image, rows and then columns. Then I have to pull in the image, and before the user sees it, it is rendered, such as a 0 = a space, 1 = -, 2 is =, 3 is a O and so forth. So they'll see this after they enter the name of the image file:

====WWW===WW======WWW=
====WW===WWWW=====WWW=
OOOOOOOOOOOOOOOOOOOOOOOO
-------------------------------------------
-----ZZZZZ------------------------------
----ZZZZZZZ--XX-----------------------
---ZBZOZOZOXXX----------------------
----OOOOOOO--XX---------------------
-----OOOOO------------------------X--Z
-------------------------------------XXZO

Basically 1-9 each mean something else when rendered before outputting to the screen.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

string filename;
int rows;
int cols;
int k = 0;
int m = 0;
string row;
const int ROW_SIZE = 40;
const int COL_SIZE = 70;
int image [ROW_SIZE][COL_SIZE];

int main()
{
  ifstream inFile;
  cout << "Enter image file name:" << endl;
  getline(cin, filename);
  inFile.open(filename.c_str());



return 0;
}

I tried messing with a getline to get the row and column number, but getline can't be used with integers, then if I use a cin I think it gets stuck on whitespace and just sits there.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Parallel Array/Reading In From A File

  #2  
Oct 14th, 2007
> ... but getline can't be used with integers ...
it is easier to process each element in your image as a char rather than an int. eg.
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <cassert>
  6. #include <iterator>
  7. using namespace std;
  8.  
  9. struct replace_chars
  10. {
  11. string operator() ( const string& str ) const
  12. {
  13. string result = str ;
  14. for( size_t i=0 ; i<result.size() ; ++i )
  15. {
  16. switch( result[i] )
  17. {
  18. case '0' : result[i] = ' ' ; break ;
  19. case '1' : result[i] = '-' ; break ;
  20. case '2' : result[i] = '=' ; break ;
  21. case '3' : result[i] = 'O' ; break ;
  22. // etc
  23. }
  24. }
  25. return result ;
  26. }
  27. };
  28.  
  29. int main()
  30. {
  31. string filename ;
  32. getline( cin, filename ) ;
  33. ifstream file( filename.c_str() ) ;
  34. size_t rows, cols ;
  35. file >> rows >> cols >> ws ; // skip over white spaces
  36. assert( rows>0 && cols>0 ) ;
  37. vector< string > image ;
  38. const string digits = "0123456789" ;
  39. string line ;
  40. while( getline( file, line ) && ( image.size() < rows ) )
  41. {
  42. assert( line.size() == cols ) ;
  43. assert( line.find_first_not_of(digits) == string::npos ) ;
  44. image.push_back( line ) ;
  45. }
  46. assert( image.size() == rows ) ;
  47. transform( image.begin(), image.end(),
  48. ostream_iterator<string>(cout,"\n"),
  49. replace_chars() ) ;
  50. }
Last edited by vijayan121 : Oct 14th, 2007 at 9:49 pm.
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: Parallel Array/Reading In From A File

  #3  
Oct 14th, 2007
You can use >> operator to get the row/column values as ints, then read the data as individual chars
inFile >> rows >> columns;
infile.ignore( 10, '\n');  //ignores up to 10 characters, or until newline
//or
infile.ws( );  //eats up the whitespace
Then begin to read your data ( as characters, as vijayan suggests)
inFile >> image[r][c];  //inside a loop that correctly corresponds to the row and column values you read in.
I am in mourning for my country.
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Help Re: Parallel Array/Reading In From A File

  #4  
Oct 15th, 2007
Thanks for the help so far, I believe I'm now ignoring the whitespace or that first line when I'm going to try and read the image. I was told something about using nested for loops to draw in the image, you'll see it in the code I have now, I'm still messing around with it trying to get it to work. If I had used structs before or had any familiarity with them I would've gone that route, but I've never used them before. I'm trying to solve this particular problem through parallel arrays.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

string filename;
int rows;
int cols;
int k = 0;
int m = 0;
string bum;
string row;
const int ROW_SIZE = 40;
const int COL_SIZE = 70;
int image [ROW_SIZE][COL_SIZE];
int main()
{
  ifstream inFile;
  cout << "Enter image file name:" << endl;
  getline(cin, filename);
  inFile.open(filename.c_str());
  inFile >> rows >> cols;
  inFile.ignore( 10, '\n');
  inFile >> image[k][m];
  while (inFile){
  for (k =0; k < rows; k++)
      for (m =0; m < cols; m++)
  inFile >> image[k][m];
  }
  cout << image[2][3];
return 0;
}

I can tell at the moment what I have isn't doing the trick, if I try to cout just a certain piece of the image, even before converting it to whatever characters it should come out as, it always couts a 0. So what I have isn't doing the trick.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Parallel Array/Reading In From A File

  #5  
Oct 15th, 2007
>> int image [ROW_SIZE][COL_SIZE];
OK. You want to display the image as characters ... do you need to keep it in memory? If you do you can either change the type of the image to a character array. It'll read in all the numbers as single characters. Then, once it's all read in, you can convert a '0' to a '-' or whatever, hece using the same variable.
Alternatively you could just create a second array of the same size...

It might be worth dynamically allocating enough memory for the image at runtime ... You familiar with new/delete?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Re: Parallel Array/Reading In From A File

  #6  
Oct 15th, 2007
I do need to keep it in memory because after I output what it looks like after being rendered, you know with the whole 0= 1=, stuff, the user can then brighten or darken the image, which will increment all the integers in the image +1 or -1 depending if they chose to brighten or darken it. I changed my array to char like you said, and now it is working, when I cout a specific cell the correct integer actually comes out. How can I get the entire image posted to the screen? More for loops?
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Parallel Array/Reading In From A File

  #7  
Oct 15th, 2007
  1. for (k =0; k < rows; k++)
  2. for (m =0; m < cols; m++)
  3. cout<< image[k][m];
  4.  
Last edited by twomers : Oct 15th, 2007 at 5:05 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Help Re: Parallel Array/Reading In From A File

  #8  
Oct 15th, 2007
Hmm, the for loops to output the image to screen just throw it all out in a big mess, it loses its format of rows of 10 and columns of 22. I'll keep looking at it, here's my updated code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

string filename;
int rows;
int cols;
int k = 0;
int m = 0;
string bum;
string row;
const int ROW_SIZE = 40;
const int COL_SIZE = 70;
char image [ROW_SIZE][COL_SIZE];
int main()
{
  ifstream inFile;
  cout << "Enter image file name:" << endl;
  getline(cin, filename);
  inFile.open(filename.c_str());
  inFile >> rows >> cols;
  inFile.ignore( 10, '\n');
  inFile >> image[k][m];
while (inFile){
  inFile >> image[k][m];
  for (k =0; k < rows; k++)
      for (m =0; m < cols; m++)
  inFile >> image[k][m];
  }
  for (k =0; k < rows; k++)
     for (m =0; m < cols; m++)
        cout<< image[k][m];
//  switch( image[k][m] )
//   {
//     case '0' : image[k][m] = ' ' ; break;
//     case '1' : image[k][m] = '-' ; break;
//     case '2' : image[k][m] = '=' ; break;
//     case '3' : image[k][m] = 'O' ; break;
//     case '4' : image[k][m] = 'Z' ; break;
//     case '5' : image[k][m] = 'X' ; break;
//     case '6' : image[k][m] = 'B' ; break;
//     case '7' : image[k][m] = '@' ; break;
//     case '8' : image[k][m] = 'W' ; break;
//     case '9' : image[k][m] = '#' ; break;
//   }

return 0;
}

I'm going to read up some more on switches, try and figure that out with what I have so far.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Parallel Array/Reading In From A File

  #9  
Oct 15th, 2007
Sorry! My bad:
  for (k =0; k < rows; k++) {
      for (m =0; m < cols; m++)
        cout<< image[k][m];
  cout<< "\n";
}

What I posted earlier just throws everything on one line but this should put a new like after every row. stupid mistake of mine. Sorry.
Last edited by twomers : Oct 15th, 2007 at 5:20 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: DemonSpeeding is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DemonSpeeding DemonSpeeding is offline Offline
Light Poster

Re: Parallel Array/Reading In From A File

  #10  
Oct 15th, 2007
I read up on switches, and it says they're used for strings, not chars, so I'm going to have to do a bunch of if and else if statements then? Also this is what it's putting out for the image:

2288822288222222888222
2288822288222222888233
3333333333333333333311
1111111111111111111111
1114444411111111111111
1144444441155111111111
1464343434555111111111
1133333331155111111111
1113333311111111511411
11111111111111115543

It's almost right but there's something off about it. The first two digits are being cut off for some strange reason when it goes to output it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:03 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC