473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Files to array with pointer.

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.

Mar 21 '06 #1
6 1860
JE

JoeC wrote:
<snip>
The problem I am having is that the program is not
printing the same numbers that are in the file <snip> 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

Mar 22 '06 #2
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.

Mar 22 '06 #3
JoeC wrote:
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
Mar 22 '06 #4
Larry I Smith wrote:
JoeC wrote:
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

<snip>

// 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
Mar 22 '06 #5
I have been learning C++ from Acclerated C++ so I should use and index
instead of an iterator.

Mar 22 '06 #6
JE

JoeC wrote:
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

Mar 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Newsgroup - Ann | last post by:
Hi gurus, I have two files compiled together. One file has the following global definition: char s = "asdf"; Another file has the following declaration: extern char *s;
4
by: dharmesh Gupta | last post by:
Hi, i have a program splitted across two files main_prog.cpp idr.cpp my main_prog.cpp accepts a file name a s parameter. i want to capture this file name in a variable and use it in idr.cpp ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
1
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
1
by: ketema | last post by:
Hello, I was wondering if someone could help me with a function I am trying to write. The purpose of the function is to read in text from a file in the following format: FIRSTNAME LASTNAME...
5
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.