Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Delete a byte. | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 6614/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 08-28-05 01:29 PM Link | Quote
Presumably a really simple question, but I can't seem to find an answer. How can I delete a byte from the middle of a file (in Binary mode, in C)?
beneficii

Lakitu
Level: 36

Posts: 440/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 08-28-05 03:02 PM Link | Quote
Originally posted by HyperHacker
Presumably a really simple question, but I can't seem to find an answer. How can I delete a byte from the middle of a file (in Binary mode, in C)?


My solution would be to store the file size in memory (like an int variable), load the file into memory, starting at the byte after the byte you're trying to delete shift every byte one byte left, decrement the file size, and then save the file.

EDIT: Here's some code that probably does what you are describing. It's untested, but I think that it probably works:


#include <malloc.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>

int deletebyte(const char *filename, int address) {
int size, handle, *data;
FILE *nfo;

handle = open(filename, O_RDONLY); /* getting file size */
size = filelength(handle);
close(handle);

data = malloc(sizeof(int) * size); /* creating data space in memory to store file*/

nfo = fopen(filename, "rb"); /* loading the file */
for(int i = 0; i < size; i++)
data[i] = fgetc(nfo);
fclose(nfo);

for(int i = address + 1; i < size; i++) /* starting at the byte following the specified address */
data[i - 1] = data[i]; /* shifting each byte left */

size--; /* decrementing the size */

nfo = fopen(filename, "wb"); /* reopening the file to write to it */
for(int i = 0; i < size; i++) /* writing to it */
fputc(data[i], nfo);
fclose(nfo);

/* now that we did it, it's time to return our value and free our dynamically allocated array */

free(data);

return size; /* returning our new file size */
}

/* if you want to use this function call it: the first parameter is the file name and the second parameter is the address that you want to delete; the function returns an int, the new filesize of the file. */


(edited by beneficii on 08-28-05 06:18 AM)
(edited by beneficii on 08-28-05 06:19 AM)
(edited by beneficii on 08-28-05 06:20 AM)
(edited by beneficii on 08-28-05 06:21 AM)
(edited by beneficii on 08-28-05 06:44 AM)
Dish

Spiny
Level: 38

Posts: 540/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 08-28-05 08:47 PM Link | Quote
As beneficii suggested --- I dont' know of a way to remove a single byte rather than reading/adjusting/rewriting the whole file either =/

Although I just thought I'd chime in and say that for large chunks of data, fread() and fwrite() are more preferable to fgetc() and fputc()
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 6616/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 08-29-05 12:00 AM Link | Quote
Bah, I was hoping I wouldn't have to resort to copying the file (even if only into memory).
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Delete a byte. | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.005 seconds.