473,385 Members | 1,445 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.

What the hell is going on??????????????????

This has wasted about 4 hours of my time to narrow this stupid thing down
and I have no clue to why its screwing up. The problem is, I'm trying to
write a file and if I mess with the data array fwrite does not write the
exact size that I tell it to(even if I hard code it to write m bytes, it
write's n bytes)...

Heres example code that demonstrates the problem.... I'm using console under
VS.NET

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

//using namespace std;

int Width()

{

return 640;

}

int Height()

{

return 480;

}

int Max_Color()

{

return 255;

}

int main(int argc, char *argv[])

{
unsigned char *Image3 = NULL;
printf("Allocating buffer...\n");

Image3 = new unsigned char[Width()*Height()];

if (Image3 == NULL) { printf("Cannot allocate memory!"); return 0; }

unsigned char max = 1;
printf("Doing loop...\n");

for(int j=0;j<Height();j++)

for(int i = 0;i<Width();i++)

{

// Here is the error, if I try to change the data, I get different file
sizes for the fwrite.... this makes no sense to me, as I would think that if
I tell fwrite to write m bytes, it should write m bytes(well, atleast not
more than m bytes)...

Image3[i+Width()*j] = i;

//Image3[i+Width()*j] = 0*i;
}


printf("Opening file...\n");

FILE *pgm = fopen("beta.tst","wt");

if (pgm == NULL) {printf("cannot write file"); return false; }

// Put pgm header

//fprintf(pgm, "P5\n#Test\n%d\n%d\n%d\n", Width(), Height(), Max_Color());

// Write data
printf("Writing Data...\n");

fwrite(Image3, 1, Width()*Height(), pgm);

/*

for(int j=0;j<Height();j++)

for(int i=0;i<Width();i++)

fputc(Get_Pixel(i,j), pgm);

*/
fclose(pgm);

printf("Press enter!");

char ch = getchar();

return true;

};


Jul 22 '05 #1
2 1260
Jon Slaughter wrote:
This has wasted about 4 hours of my time to narrow this stupid thing down
and I have no clue to why its screwing up. The problem is, I'm trying to
write a file and if I mess with the data array fwrite does not write the
exact size that I tell it to(even if I hard code it to write m bytes, it
write's n bytes)...

Heres example code that demonstrates the problem.... I'm using console under VS.NET

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>
VS.NET and Outlook Express collude to screw up linefeeds.

You need to open Notepad, paste code into it, copy the code, and paste it
into Outlook Express.
//using namespace std; int Width()
{
return 640;
} int Height()
{
return 480;
}
Uh, making those static constants would be mostly harmless...
int Max_Color()
{
return 255;
}

int main(int argc, char *argv[])
{
unsigned char *Image3 = NULL;

printf("Allocating buffer...\n");

Image3 = new unsigned char[Width()*Height()];
Why not use vector?
if (Image3 == NULL) { printf("Cannot allocate memory!"); return 0; }
You need to read /Accelerated C++/, and not write "C-style C++". printf()
has different risks and benefits to std::cout, and 'new' throws an exception
if it fails.
// Here is the error, if I try to change the data, I get different file
sizes for the fwrite.... this makes no sense to me, as I would think that if I tell fwrite to write m bytes, it should write m bytes(well, atleast not
more than m bytes)... FILE *pgm = fopen("beta.tst","wt");
You opened the file for text, with a 't'.
fwrite(Image3, 1, Width()*Height(), pgm);


Remember when I said to copy your source into Notepad? fopen(...,"t") has a
related issue. Because MS (and IBM etc.) are superior to Unix, they end
linefeeds with \r\n. However, C and C++ come from Unix-land, where linefeeds
end with \n

The great thing about Standards is there are so many to choose from.

The tension between these utterly trivial conventions causes endless bugs
(mostly in MS-land). For example, Outlook Express inserted extra linefeeds
for no apparent reason when you pasted. The tension also causes bugs in C++
programs.

When you use fopen(...,"t"), you get "end-of-line text translation". Your
binary data contain a \x0A, which FILE translated into a \r\n, adding a
character.

You are waiting for me to stop pontificating and tell you the fix.
fopen(..., "wb") for binary.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2
"Jon Slaughter" <Jo***********@Hotmail.com> wrote in message
news:10*************@corp.supernews.com...
This has wasted about 4 hours of my time to narrow this stupid thing down
and I have no clue to why its screwing up. The problem is, I'm trying to
write a file and if I mess with the data array fwrite does not write the
exact size that I tell it to(even if I hard code it to write m bytes, it
write's n bytes)...
FILE *pgm = fopen("beta.tst","wt");


FILE *pgm = fopen("beta.tst","wb");

Read about 'text mode' and 'binary mode'.

-Mike
Jul 22 '05 #3

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

Similar topics

0
by: Jean-Louis Vill | last post by:
=============================================================================== I installed PHP from an RPM, but Apache isn't processing the PHP pages! What's going on here? Somebody can help me ?...
1
by: Geoff Hague | last post by:
http://www.captainsoftheworld.com/test/index.html http://www.captainsoftheworld.com/test/HarrisRoadCinemas.css My problem involves the parent DIV (called #FrontPageListings) and the two other...
6
by: lkrubner | last post by:
I've got a site on a hosted virtual server. I'd like to know the truth of what is going on, but I can't get past my browsers cache. There is some kind of error in my code that occurs on a given...
9
by: Nicolae Fieraru | last post by:
Hi All, I try to use Request.ServerVariables("HTTP_REFERER") in a script and I discovered it doesn't work as expected. I use XP with SP2 and I created a link on a web site which points to an asp...
9
by: andrew | last post by:
Hi, I posted this message recently, but received no response, so I figured I'd try again. I can't find anybody who can tell me what's going on here. I'm having a problem with fieldsets in IE...
3
by: phonl | last post by:
VB.NET 2005 and ADO2.NET After years of VB6 and ADO coding, I'm checking out ADO2.NET. I spent several hours trying to figure out why the line: objDataAdapter.Update(objDataSet, "myTable") ...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
2
by: DaTurk | last post by:
This is probably a very silly, and simple question. If I'm coding in CLI, and I want to copy an array to an array, not a deep copy, just something of the nature arr1 = arr2, what is going on? I...
2
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
Ok, I'm out of practice in web work, I've never done any webwork in 2.0, and I have to come up with a VB/Dot Net 2.0 web site in a week or two. Business as usual. First, I know I can't trust...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.