•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation:
Rep Power: 9
Solved Threads: 163
> ... 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.
it is easier to process each element in your image as a char rather than an int. eg.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <vector> #include <cassert> #include <iterator> using namespace std; struct replace_chars { string operator() ( const string& str ) const { string result = str ; for( size_t i=0 ; i<result.size() ; ++i ) { switch( result[i] ) { case '0' : result[i] = ' ' ; break ; case '1' : result[i] = '-' ; break ; case '2' : result[i] = '=' ; break ; case '3' : result[i] = 'O' ; break ; // etc } } return result ; } }; int main() { string filename ; getline( cin, filename ) ; ifstream file( filename.c_str() ) ; size_t rows, cols ; file >> rows >> cols >> ws ; // skip over white spaces assert( rows>0 && cols>0 ) ; vector< string > image ; const string digits = "0123456789" ; string line ; while( getline( file, line ) && ( image.size() < rows ) ) { assert( line.size() == cols ) ; assert( line.find_first_not_of(digits) == string::npos ) ; image.push_back( line ) ; } assert( image.size() == rows ) ; transform( image.begin(), image.end(), ostream_iterator<string>(cout,"\n"), replace_chars() ) ; }
Last edited by vijayan121 : Oct 14th, 2007 at 9:49 pm.
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
You can use >> operator to get the row/column values as ints, then read the data as individual chars
Then begin to read your data ( as characters, as vijayan suggests)
inFile >> rows >> columns; infile.ignore( 10, '\n'); //ignores up to 10 characters, or until newline //or infile.ws( ); //eats up the whitespace
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.
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
#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.
>> 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?
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?
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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?
c Syntax (Toggle Plain Text)
for (k =0; k < rows; k++) for (m =0; m < cols; m++) cout<< image[k][m];
Last edited by twomers : Oct 15th, 2007 at 5:05 pm.
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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:
I'm going to read up some more on switches, try and figure that out with what I have so far.
#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.
Sorry! My bad:
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.
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.
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Reading a file into a Parallel Array (C++)
- Setting an array and reading in a txt file backwards (C++)
- Trying to creating an array from a text file (C++)
- Help with a 2D array from a text file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Getting an array from a txt file (C)
- Error Message Concerning Reading File From A Drive (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Getting started with C++
- Next Thread: How to play mp3 and sfx sound files in windows mobile 5.0 ?



Linear Mode