Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to access the value that a pointer is pointing to



Thanks for that. Patrick. (And thanks, too, to Lawrence for your answer.)

On 29 Apr 2006, at 16:49, Patrick Seemann wrote:

Am 29.04.2006 um 16:09 schrieb Phil Faber:

The following code successfully copies a file, byte-by-byte. It uses fread(&oneByte,1,1,fp1) to read in a byte and fwrite(&oneByte, 1,1,fp2) to write that byte back to disc.

I want to be able to analyse the byte before it writes back to disc but can't see how to see its value. oneByte is a pointer to the value - not the value itself. I need to do something like:

oneByteASCII=(value of byte at pointer 'oneByte')

You already got the value of the byte in 'oneByte' (which is not a pointer but an integer variable) so you may just do with it whatever you want.

But oneByte has a value of 2067035792! Which is why I assumed it was a pointer. I was expecting an 8-bit byte number from 0 to 255; 65, for example, being the ASCII value for 'A'.


You may also consider
- declaring oneByteASCII as 'char' and assigning it with 'oneByteASCII = (char) oneByte;'

The problem with this is that I actually want the ASCII *numerical value* of the byte, not its equivalent textual value. In many cases I won't be reading a text file anyway.


Can anyone explain why I'm not getting a number from 0 to 255 and what I need to do to achieve that result?

        // Used to store currently read byte
        int oneByte, oneByteASCII;

// Provide dialogue box to select file
NSOpenPanel *panel = [NSOpenPanel openPanel];
int result = [panel runModalForDirectory:nil file:nil types:nil];


        // Put file name into inputFile
        NSString *inputFile = [panel filename];
        NSMutableString *outputFile;

outputFile = [NSMutableString stringWithFormat: @"%@-A", inputFile];

        // Prepare file pointer to open selected file
        FILE *fp1;

        // Prepare file pointer to write to output file
        FILE *fp2;

        // Set file pointer to selected file
        fp1 = fopen([inputFile UTF8String],"rb");
        fp2 = fopen([outputFile UTF8String],"wb");

        // Read a byte
        fread(&oneByte,1,1,fp1);

        while(feof(fp1)==0)

{
// ** This is not legal code but shows what I'm trying to achieve here! **
oneByteASCII=(value of byte at pointer 'oneByte');


                // Write the byte
                fwrite(&oneByte,1,1,fp2);

                // Read next byte
                fread(&oneByte,1,1,fp1);
        }

        // Close files
        fclose(fp1);
        fclose(fp2);

_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >-[NSAttributedString initWithData:...] compresses spaces? (From: Glen Simmons <email@hidden>)
 >How to access the value that a pointer is pointing to (From: Phil Faber <email@hidden>)
 >Re: How to access the value that a pointer is pointing to (From: Patrick Seemann <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.