473,385 Members | 2,014 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,385 software developers and data experts.

Problem with read() function...

72
Got an assignment where I need to convert between pgm and ppm. Now I know about the homework guideline so i went ahead to try to do up everything first and I managed to get it right. Or so it seems.

The problem starts to come in when I try to convert bigger files. The top part of the image will be converted correctly but the bottom part would be left blank. As if I did not read in the full image info.

what i did was to get the length of the data and create an unsigned char array to store it using the length. Next I would use the read() function to read into the array the info required

file.read( reinterpret_cast<char *>(img), (w)*sizeof(unsigned char));

But it seems that after a few read()s, it will just stop there and the rest will not be read in.

Really hope someone can help here as I have spent the last day or so trying to figure it out.

Thanks In Advance. :)
May 22 '07 #1
9 2442
weaknessforcats
9,208 Expert Mod 8TB
I'm confused. An unsigned char array has unsigned chars as elements. That is, the elements are one byte.

Are you saying the array itself is larger enough to accommodate aby data item? And that you repeated load/unload thuis array as you read the file?

How are you getting the lentgh of the data?

I would have expected an fseek() from 0 origin to SEEK_END followed by a
tell() to see how may bytes from 0 the file is. I would then read the entire file in one read() call.
May 22 '07 #2
KWSW
72
nothing wrong with the array length...

i even used the seekg and tellg to ensure the array length is correct and i am reading in the correct length...

its like read() doesn't want to read in everything after awhile... :(
May 22 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Probably time to see a little more code.
May 22 '07 #4
KWSW
72
Probably time to see a little more code.
my code:

int pos = file.tellg();
file.seekg(0,ios::end);
int size = file.tellg() - pos;
file.seekg(pos);

img = (unsigned char*) new unsigned char[size];
file.read( reinterpret_cast<char *>(img), (size)*sizeof(unsigned char));
May 22 '07 #5
KWSW
72
Found this function gcount to which shows how many unformatted chars the previous input stream got.

so i went and did:

cout << " count: " << file.gcount() << " total: " << size << endl;

well the numbers dun tally for big files that i have to read in... smaller files and they tally...
May 22 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. int pos = file.tellg();
  2. file.seekg(0,ios::end);
  3. int size = file.tellg() - pos;
  4. file.seekg(pos);
  5.  
  6. img = (unsigned char*) new unsigned char[size];
  7. file.read( reinterpret_cast<char *>(img), (size)*sizeof(unsigned char));
  8.  
looks odd.

First, when seek to end of the file from an origin of 0, then the file length is return value from tellg() and not tellg() - pos, whatever pos is.

Second, you are allocating an array of size elements, which is an int. Is size able to be stored in an int or have you exceeded the maximum int value?? You say things work until the file gets big. Maybe you need an unsigned int? Maybe the file needs to read in in swatches.

Third, new return an unsigned char* pointer from new unsigned char[size]. That means you don't need the cast.

Fourth, (size)*sizeof(unsigned char) is really size * 1 or size.

Fifth, it looks like img is an unsigned char* so I don't see how it converts to char*. Certainly, unsigned char values don't always fit in a char. Some of your data bits may be used as sign bits and that screws up the data.
May 22 '07 #7
KWSW
72
This code:
Expand|Select|Wrap|Line Numbers
  1. int pos = file.tellg();
  2. file.seekg(0,ios::end);
  3. int size = file.tellg() - pos;
  4. file.seekg(pos);
  5.  
  6. img = (unsigned char*) new unsigned char[size];
  7. file.read( reinterpret_cast<char *>(img), (size)*sizeof(unsigned char));
  8.  
looks odd.

First, when seek to end of the file from an origin of 0, then the file length is return value from tellg() and not tellg() - pos, whatever pos is.

Second, you are allocating an array of size elements, which is an int. Is size able to be stored in an int or have you exceeded the maximum int value?? You say things work until the file gets big. Maybe you need an unsigned int? Maybe the file needs to read in in swatches.

Third, new return an unsigned char* pointer from new unsigned char[size]. That means you don't need the cast.

Fourth, (size)*sizeof(unsigned char) is really size * 1 or size.

Fifth, it looks like img is an unsigned char* so I don't see how it converts to char*. Certainly, unsigned char values don't always fit in a char. Some of your data bits may be used as sign bits and that screws up the data.
thanks for the input... gonna give it one more try than its off to bed for me... its past 1am here...

my last attempt:

img = new unsigned char[width];

/* convert unsigned chars to integers for processing */
for(int i = 0; i < height; i++)
{
file.read(img, width);

for(int j = 0; j < w; j++)
{
pVal = (int)img[j];
/* other codes to store the pixel info */
}

}

where width is the number of pixels in the width of the image and likewise for height.

TIA for helping me :)
May 22 '07 #8
KWSW
72
darn still getting the same problem of read() not reading in everything even though the size to read and the buffer is correct...
May 23 '07 #9
KWSW
72
darn still getting the same problem of read() not reading in everything even though the size to read and the buffer is correct...
hmm... managed to do a work around...

it seems that if i read in the binary block after i read in the header info of the pgm/ppm file, the read() will not read in everything if the file is too big.

Thus i read in the binary block first than go back to the start of the file to read in the header info...
May 23 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jacek | last post by:
I have problem with socket_read function. Script connect to telnet and read about hundred lines. Example: $out = socket_read($socket,4096)<------"length =4096" This read only 90 line - why?...
6
by: Marcin Kalicinski | last post by:
Hi all, I have a problem with 'volatile' use in C++. The function get_clocks() below tries to use a 16 bit hardware counter to count time. The counter overflows very often. But an interrupt is...
8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
0
by: mario.lat_ | last post by:
Hallo to all, I have write a little script for connecting to cisco router BUT I have a problem: I have to send to router all the commands and then I have to read the output. If I send a command1...
1
by: Smita Prathyusha | last post by:
I am facing a problem in writing to COM1. I am using a Win 32 Console mode Program in VC++ the following is the code: If anyone can help me out it will be of great help : // SC_Using_Classes.cpp...
0
by: iprogrammer | last post by:
i have a problem when i try to run my windows service ..which is "Error 1053: The service did not respond to the start or control request in a timely fashion" >after this i cannot anything with...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.