This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Calculator Programming subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. General Coding and Design => Calculator Programming
Author Message
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 12 Jul 2007 02:08:50 pm    Post subject:

Basically i want to write some char arrays to a .bin file. here's how I'm writing to the file, i had no idea what to put in the third argument for fwrite.
in the API it said that that is "size_t nobj" and i had no idea what that meant.

Code:
FILE *fp;

if (fp = fopen("doommaps.bin","w"))
{
  fwrite (map,sizeof(map) , sizeof(map),fp);
  fwrite (map2,sizeof(map2) , sizeof(map2) ,fp);
  fwrite (map3,sizeof(map3) ,sizeof(map3) , fp);
  fwrite (map4,sizeof(map4) ,sizeof(map4) , fp);
  fwrite (map5,sizeof(map5) , sizeof(map5) ,fp);
  fwrite (map6,sizeof(map6) ,sizeof(map6) , fp);    
  fwrite (map7,sizeof(map7) ,sizeof(map7) , fp);    
  fwrite (map8,sizeof(map8) ,sizeof(map8) ,fp);
  fwrite (map9,sizeof(map9) , sizeof(map9) ,fp);
  fwrite (map10,sizeof(map10) , sizeof(map10) ,fp);
  fwrite (map11,sizeof(map11) , sizeof(map11) ,fp);      
  fwrite (map12,sizeof(map12) ,sizeof(map12) , fp);
  fwrite (map13,sizeof(map13) , sizeof(map13) ,fp);
  fwrite (map14,sizeof(map14) , sizeof(map14) ,fp);          
  fwrite (map15,sizeof(map15) , sizeof(map15) ,fp);
  fwrite (map16,sizeof(map16) , sizeof(map16) ,fp);        
}

There's got to be a million things wrong with that. Can anyone help me? the file's coming out to 12k when it should be 9k.

and it needs to be a binary file.

Like i said, i have no idea what i'm doing here. if this were TIGCC i could do it, and have been, but i wanted to try and do something to make my life a little easier.


Last edited by Guest on 12 Jul 2007 02:10:32 pm; edited 1 time in total
Back to top
Arcane Wizard
`semi-hippie`


Super Elite (Last Title)


Joined: 02 Jun 2003
Posts: 8993

Posted: 12 Jul 2007 03:41:32 pm    Post subject:

Type "fwrite" into google, click first result pertaining to c++.

http://www.cplusplus.com/reference/clibrar...dio/fwrite.html

Third argument is the number of elements.
Back to top
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 12 Jul 2007 04:03:06 pm    Post subject:

Windows? Then
[font="Monospace"]
if (fp = fopen("doommaps.bin","wb"))
Back to top
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 12 Jul 2007 04:25:22 pm    Post subject:

weird. i fixed the problem with the writing. the file is the size it's supposed to be and still no results. I get the first two maps loading correctly then after that it's garbage.

I think i'm going to have to stick to what i was doing in the first place.

EDIT: i posted this before i saw sigma's post. Thanks a million! you both totally fixed my problem. now mapfile generation is going to be a breeze

EDIT2:

For that matter, archive file generation is going to be a breeze now actually. I'm going to use this approach with my next projects if i need to create files.


Last edited by Guest on 12 Jul 2007 04:38:09 pm; edited 1 time in total
Back to top
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 13 Jul 2007 08:41:45 pm    Post subject:

Okay now that that is working i have another problem, i want to write text in binary mode to a binary file, sounds simple enough. So i defined a string array and then created a writing system for it, the file created is only about 1.28 k. That struck me a little funny because there is a lot of text here. Anyone see what i'm doing wrong?

the "\r"'s are to tell the file reading system i'll be setting up when to stop reading the current line.


Code:
#include <cstdlib>
#include <iostream>



using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
const char* const story [] = {
     "THE STORY SO FAR\r",
     "You're a marine, one of Earth's\r",
     "toughest, hardened in combat and\r",
     "trained for action. Three years ago,\r",
     "you assaulted a superior officer for\r",
     "ordering his soldiers to fire upon\r",
     "civilians. He and his body cast were\r",
     "shipped to Pearl Harbor, while you\r"
     "were transferred to Mars, home of the\r",
     "Union Aerospace Corporation.\r",
     " \r",
     "The UAC is a multi-planetary\r",
     "conglomerate with radioactive waste\r",
     "facilities on Mars and its two moons,\r",
     "Phobos and Deimos. With no action for\r",
     "fifty million miles, your day consisted\r",
     "of suckin' dust and watchin' restricted\r",
     "flicks in the rec room.\r"
     " \r",
     "For the last four years the military,\r",
     "UAC's biggest supplier, has used the\r",
     "remote facilities on Phobos and Deimos\r",
     "to conduct various secret projects,\r",
     "including research on inter-dimensional\r",
     "space travel. So far they have been\r",
     "able to open gateways between Phobos\r",
     "and Deimos, throwing a few gadgets\r",
     "into one and watching them come out the\r",
     "other. Recently however, the Gateways\r",
     "have grown dangerously unstable.\r",
     "Military 'volunteers' entering them \r",
     "have either disappeared or been\r",
     "stricken with a strange form of\r",
     "insanity--babbling vulgarities,\r",
     "bludgeoning anything that breathes,\r",
     "and finally suffering an untimely\r",
     "death of full-body explosion.\r",
     "Matching heads with torsos to send\r",
     "home to the folks became a full-time\r",
     "job. Latest military reports state\r",
     "that the research is suffering a small\r",
     "set-back, but everything is under\r",
     "control.\r",
     " \r",
     "A few hours ago, Mars received a garbled\r",
     "message from Phobos. 'We require immediate\r",
     "military support. Something fraggin' evil\r",
     "is coming out of the Gateways! Computer\r",
     "systems have gone berserk!' The rest was\r",
     "incoherent. Soon afterwards, Deimos simply\r",
     "vanished from the sky. Since then, attempts\r",
     "to establish contact with either moon have\r",
     "been unsuccessful.\r",
     " \r",
     "You and your buddies, the only combat troop\r",
     "for fifty million miles were sent up pronto\r",
     "to Phobos. You were ordered to secure the\r",
     "perimeter of the base while the rest of the\r",
     "team went inside. For several hours, your\r",
     "radio picked up the sounds of combat:\r",
     "guns firing, men yelling orders, screams,\r",
     "bones cracking, then finally, silence.\r",
     "Seems your buddies are dead.\r",
     " \r",
     "IT'S UP TO YOU\r",
     "Things aren't looking too good.\r",
     "You'll never navigate off the planet on\r",
     "your own. Plus, all the heavy weapons\r",
     "have been taken by the assault team\r",
     "leaving you with only a pistol. If only\r",
     "you could get your hands around a railgun\r",
     "rifle or even a shotgun you could take a\r",
     "few down on your way out. Whatever killed\r",
     "your buddies deserves a couple of pellets\r",
     "in the forehead. Securing your helmet,\r",
     "you exit the landing pod. Hopefully you\r",
     "can find more substantial firepower\r",
     "somewhere within the station.\r",
     " \r",
     "As you walk through the main entrance of\r",
     "the base, you hear animal-like growls\r",
     "echoing throughout the distant corridors.\r",
     "They know you're here. There's no turning\r",
     "back now.\r"
};

int I;

FILE *fp;

if (fp = fopen("doomtext.bin","wb"))
{
       for (I=0; I<sizeof(story); I++)
       {
         fwrite (story,sizeof(story[I]),1,fp);  
       }
  
}

    return EXIT_SUCCESS;
}


Last edited by Guest on 13 Jul 2007 08:42:47 pm; edited 1 time in total
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 14 Jul 2007 02:28:39 am    Post subject:

You need to fclose(fp), otherwise it won’t output the last portion it buffered. By the way, you don’t need a loop to write all this to a file, just say fwrite (story,strlen(story),1,fp) and be done with it.

And I don’t know why, but a variable called capital I just hurts my eyes. Neutral
Back to top
Goplat


Advanced Newbie


Joined: 26 Jun 2007
Posts: 95

Posted: 14 Jul 2007 12:00:50 pm    Post subject:

CoBB wrote:
You need to fclose(fp), otherwise it won’t output the last portion it buffered.
Exiting a program automatically closes all files, actually.

Anyway, the reasons why the code didn't work:
* sizeof is the size of something in bytes, so to get the number of elements in an array, you take the total size and divide by the size of a single element. Like this: for(i = 0; i < sizeof(story) / sizeof(story[0]); i++)
* You want to write the string that the i'th element of the array points to, not the array itself. So the first argument to fwrite should be story[i], not just story.
* Since each element is a pointer, sizeof(story[i]) will just be the size of a pointer. To get the length of the string it points to, use strlen.

Code:
for (i = 0; i < sizeof(story) / sizeof(story[0]); i++)
    fwrite(story[i], strlen(story[i]), 1, fp);
Since writing strings is common, there's a function called fputs to do it more easily.
Code:
for (i = 0; i < sizeof(story) / sizeof(story[0]); i++)
    fputs(story[i], fp);
Back to top
CoBB


Active Member


Joined: 30 Jun 2003
Posts: 720

Posted: 14 Jul 2007 01:51:04 pm    Post subject:

Oh my, I’m completely blind, many-many times... I should read properly (or at least disable my mental code autocorrector Razz) before I post stupid things. Mad Mea culpa.
Back to top
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 14 Jul 2007 03:43:49 pm    Post subject:

thanks a bunch, the outputted file looks about right now. 2.75 KB. I didn't really know you could send only two elements to fputs, otherwise i would have done that to start with. for some reason when i looked it up it had three elements you had to pass to it and i didn't understand one.
Back to top
sgm


Calc Guru


Joined: 04 Sep 2003
Posts: 1265

Posted: 17 Jul 2007 09:50:01 pm    Post subject:

Goplat wrote:
* sizeof is the size of something in bytes

More precisely, it is the size of something in [font="Monospace"]char
s.
Back to top
Goplat


Advanced Newbie


Joined: 26 Jun 2007
Posts: 95

Posted: 18 Jul 2007 11:09:48 am    Post subject:

The C++ standard says that sizeof gives the size in bytes, and also that sizeof(char) is always 1. So a char is always one byte.
Back to top
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 18 Jul 2007 04:04:22 pm    Post subject:

hmm, well a char is equal to a possiblility of numbers up to 256 so that would be 0001 0000 0000 in binary, so wouldnt it really be three bytes?
Or am I confusing myself? (i put the spaces inbetween the bytes to make it easier to see)


Last edited by Guest on 18 Jul 2007 04:06:17 pm; edited 1 time in total
Back to top
Goplat


Advanced Newbie


Joined: 26 Jun 2007
Posts: 95

Posted: 18 Jul 2007 04:17:08 pm    Post subject:

AaroneusTheGreat wrote:
hmm, well a char is equal to a possiblility of numbers up to 256
Typically, "unsigned char" can hold a number from 0 to 255; "signed char" can hold a number from -128 to 127. Plain "char" with no qualifier is one of those two (usually signed). That takes 8 bits, which is what a byte is on almost every architecture. (There used to be weird computers like the PDP-10 with 9-bit bytes, but you hardly ever see these any more)
Back to top
AaroneusTheGreat


Advanced Member


Joined: 01 May 2007
Posts: 290

Posted: 18 Jul 2007 11:47:10 pm    Post subject:

Ack. Whoops i was thinking in nibbles. My bad. Cool I gotcha now.
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement