944,057 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3252
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 14th, 2007
0

Parallel Array/Reading In From A File

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. string filename;
  8. int rows;
  9. int cols;
  10. int k = 0;
  11. int m = 0;
  12. string row;
  13. const int ROW_SIZE = 40;
  14. const int COL_SIZE = 70;
  15. int image [ROW_SIZE][COL_SIZE];
  16.  
  17. int main()
  18. {
  19. ifstream inFile;
  20. cout << "Enter image file name:" << endl;
  21. getline(cin, filename);
  22. inFile.open(filename.c_str());
  23.  
  24.  
  25.  
  26. return 0;
  27. }

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007
Oct 14th, 2007
0

Re: Parallel Array/Reading In From A File

> ... 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.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Oct 14th, 2007
0

Re: Parallel Array/Reading In From A File

You can use >> operator to get the row/column values as ints, then read the data as individual chars
C++ Syntax (Toggle Plain Text)
  1. inFile >> rows >> columns;
  2. infile.ignore( 10, '\n'); //ignores up to 10 characters, or until newline
  3. //or
  4. infile.ws( ); //eats up the whitespace
Then begin to read your data ( as characters, as vijayan suggests)
C++ Syntax (Toggle Plain Text)
  1. inFile >> image[r][c]; //inside a loop that correctly corresponds to the row and column values you read in.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. string filename;
  8. int rows;
  9. int cols;
  10. int k = 0;
  11. int m = 0;
  12. string bum;
  13. string row;
  14. const int ROW_SIZE = 40;
  15. const int COL_SIZE = 70;
  16. int image [ROW_SIZE][COL_SIZE];
  17. int main()
  18. {
  19. ifstream inFile;
  20. cout << "Enter image file name:" << endl;
  21. getline(cin, filename);
  22. inFile.open(filename.c_str());
  23. inFile >> rows >> cols;
  24. inFile.ignore( 10, '\n');
  25. inFile >> image[k][m];
  26. while (inFile){
  27. for (k =0; k < rows; k++)
  28. for (m =0; m < cols; m++)
  29. inFile >> image[k][m];
  30. }
  31. cout << image[2][3];
  32. return 0;
  33. }

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

>> 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?
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

  1. for (k =0; k < rows; k++)
  2. for (m =0; m < cols; m++)
  3. cout<< image[k][m];
Last edited by twomers; Oct 15th, 2007 at 5:05 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6.  
  7. string filename;
  8. int rows;
  9. int cols;
  10. int k = 0;
  11. int m = 0;
  12. string bum;
  13. string row;
  14. const int ROW_SIZE = 40;
  15. const int COL_SIZE = 70;
  16. char image [ROW_SIZE][COL_SIZE];
  17. int main()
  18. {
  19. ifstream inFile;
  20. cout << "Enter image file name:" << endl;
  21. getline(cin, filename);
  22. inFile.open(filename.c_str());
  23. inFile >> rows >> cols;
  24. inFile.ignore( 10, '\n');
  25. inFile >> image[k][m];
  26. while (inFile){
  27. inFile >> image[k][m];
  28. for (k =0; k < rows; k++)
  29. for (m =0; m < cols; m++)
  30. inFile >> image[k][m];
  31. }
  32. for (k =0; k < rows; k++)
  33. for (m =0; m < cols; m++)
  34. cout<< image[k][m];
  35. // switch( image[k][m] )
  36. // {
  37. // case '0' : image[k][m] = ' ' ; break;
  38. // case '1' : image[k][m] = '-' ; break;
  39. // case '2' : image[k][m] = '=' ; break;
  40. // case '3' : image[k][m] = 'O' ; break;
  41. // case '4' : image[k][m] = 'Z' ; break;
  42. // case '5' : image[k][m] = 'X' ; break;
  43. // case '6' : image[k][m] = 'B' ; break;
  44. // case '7' : image[k][m] = '@' ; break;
  45. // case '8' : image[k][m] = 'W' ; break;
  46. // case '9' : image[k][m] = '#' ; break;
  47. // }
  48.  
  49. return 0;
  50. }

I'm going to read up some more on switches, try and figure that out with what I have so far.
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

Sorry! My bad:
C++ Syntax (Toggle Plain Text)
  1. for (k =0; k < rows; k++) {
  2. for (m =0; m < cols; m++)
  3. cout<< image[k][m];
  4. cout<< "\n";
  5. }

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 15th, 2007
0

Re: Parallel Array/Reading In From A File

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
DemonSpeeding is offline Offline
31 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Getting started with C++
Next Thread in C++ Forum Timeline: How to play mp3 and sfx sound files in windows mobile 5.0 ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC