Files to array with pointer.

J

JoeC

I am trying to write a graphic library. Yes, I know that it is byoined
the scope of this group but I broke the file down to reading a text
file of numbers. The problem I am having is that the program is not
printing the same numbers that are in the file

Here is my code:

#include<windows.h>

#include<iostream>
#include<fstream>

using namespace std;

int main(){
int ud, lr; // Dimentions of the graphic
ifstream fin("graphic.txt");
int gr[128]; //holds values for the bitmap inprogram is BYTE but int
will do
int * pgr = &gr[0]; //rnd access iterator to the array
int ix = 0; //if I want to use an index
if (fin){ // reads file provided there is more to read
fin>>ud>>lr; // reads the dims of the graphic this is ok
while(fin){ //fills the array
fin>>*pgr; //reads the array with a rnd access iterator (realy
a pointer)
pgr++; //advances the pointer
}
} else {
cout<<"could not open file"<<endl;//if the file dosn't open
}
cout<<ud<<endl;//show height works
cout<<lr<<endl;//shows witdth works
pgr = &gr[0]; //reste the pointer to the beginning of the array
for(int lp = 0; lp < 16; lp++){ //just a loop to cycle thought the
array

cout<<*pgr<<endl;//prints the values of the array
//These values are different than what is in the file.

pgr++; //advances the pointer
}

system("pause"); //admin
return 0; //you should know what this is.
}

Here is my txt file graphic.txt

16 16
0 0 255 255 0 0 255 255 0 0 255 0 255 0 0 255

This code is similar that I posted but I remove all the graphic
refrence for my experimentation and learning. I think the problem has
something to do with how I am using the pointers.
 
J

JE

JoeC wrote:
The problem I am having is that the program is not
printing the same numbers that are in the file
Here is my txt file graphic.txt

16 16
0 0 255 255 0 0 255 255 0 0 255 0 255 0 0 255

1. OK, that's what you expect...what output did you get?
2. May be your newsreader, but the code is not formatted very well for
readability.
3. Be sure to check for, and handle, error states.
4. Don't be cavalier about potentially overrunning arrays.

Best regards, JE
 
J

JoeC

It looks like when I use decimal mumbers they print out like I want but
when I try to use hex mubers 0xffff they don't work so well. I will
keep working on my programs.
 
L

Larry I Smith

JoeC said:
I am trying to write a graphic library. Yes, I know that it is byoined
the scope of this group but I broke the file down to reading a text
file of numbers. The problem I am having is that the program is not
printing the same numbers that are in the file

Here is my code:

#include<windows.h>

#include<iostream>
#include<fstream>

using namespace std;

int main(){
int ud, lr; // Dimentions of the graphic
ifstream fin("graphic.txt");
int gr[128]; //holds values for the bitmap inprogram is BYTE but int
will do
int * pgr = &gr[0]; //rnd access iterator to the array
int ix = 0; //if I want to use an index
if (fin){ // reads file provided there is more to read
fin>>ud>>lr; // reads the dims of the graphic this is ok
while(fin){ //fills the array
fin>>*pgr; //reads the array with a rnd access iterator (realy
a pointer)
pgr++; //advances the pointer
}
} else {
cout<<"could not open file"<<endl;//if the file dosn't open
}
cout<<ud<<endl;//show height works
cout<<lr<<endl;//shows witdth works
pgr = &gr[0]; //reste the pointer to the beginning of the array
for(int lp = 0; lp < 16; lp++){ //just a loop to cycle thought the
array

cout<<*pgr<<endl;//prints the values of the array
//These values are different than what is in the file.

pgr++; //advances the pointer
}

system("pause"); //admin
return 0; //you should know what this is.
}

Here is my txt file graphic.txt

16 16
0 0 255 255 0 0 255 255 0 0 255 0 255 0 0 255

This code is similar that I posted but I remove all the graphic
refrence for my experimentation and learning. I think the problem has
something to do with how I am using the pointers.

Well, the following code works as expected on my system
(using GNU g++ on linux - SuSE 9.3):

// joe.cpp - joe's (reformatted) test code
// to compile: g++ -o joe joe.cpp
// to execute: ./joe
// "graphics.txt" must be in the same directory as joe.cpp
#include<iostream>
#include<fstream>

using namespace std;

int
main( )
{
int ud, lr;
int gr[128];
int *pgr = &gr[0];
int ix = 0;
ifstream fin( "graphic.txt" );

if ( fin )
{
fin >> ud >> lr;

while ( fin )
{
fin >> *pgr;
// 'ix' counts the number of items actually read
++ix;
++pgr;
}
}
else
{
cout << "could not open file" << endl;
}

cout << "ud: " << ud << endl;
cout << "lr: " << lr << endl;
cout << "ix: " << ix << endl;
pgr = &gr[0];

for ( int lp = 0; lp < ix; ++lp )
{
cout << *pgr << endl;
++pgr;
}

return 0;
}

This program produces this output:

ud: 16
lr: 16
ix: 17
0
0
255
255
0
0
255
255
0
0
255
0
255
0
0
255

Regards,
Larry
 
L

Larry I Smith

// joe.cpp - joe's (reformatted) test code
// to compile: g++ -o joe joe.cpp
// to execute: ./joe
// "graphics.txt" must be in the same directory as joe.cpp
#include<iostream>
#include<fstream>

using namespace std;

int
main( )
{
int ud, lr;
int gr[128];
int *pgr = &gr[0];
int ix = 0;
ifstream fin( "graphic.txt" );

if ( fin )
{
fin >> ud >> lr;

This read's one time too many. 'ix' ends up as 17 instead of 16.
This should be:
while ( fin >> *pgr )
// while ( fin )
And remove the 'fin >> *pgr;' line:
// fin >> *pgr;
// 'ix' counts the number of items actually read
++ix;
++pgr;
}
}
else
{
cout << "could not open file" << endl;
}
<snip>

Larry
 
J

JoeC

I have been learning C++ from Acclerated C++ so I should use and index
instead of an iterator.
 
J

JE

JoeC said:
It looks like when I use decimal mumbers they print out like I want but
when I try to use hex mubers 0xffff they don't work so well. I will
keep working on my programs.

Are you setting the extractor format flags for hex? e.g.,
fin.setf(ios_base::hex, ios_base::basefield);

Best regards, JE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top