1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
int append_to_file(FILE *f,long sec_start, long sec_end)
{
FILE *curr_file;
long bytes_read, bytes_to_read;
char buf[1200];
int read_pos = 0;
if ((curr_file = fopen("old_file.log", "a+b"))==NULL)
{
printf("Cannot open file.\n");
return 1;
}
else
printf("Old File Opened Correctly.\n");
fseek(f,sec_start,SEEK_SET);
bytes_to_read = sec_end - sec_start;
bytes_read = fread(buf, sizeof buf, bytes_to_read, f); //
fseek(f,0L,SEEK_CUR);
read_pos = fgetc(f);
printf("read position: %i \n", read_pos);
if (bytes_read == sizeof buf)
{
fwrite(buf, sizeof buf, bytes_to_read, curr_file);
read_pos = read_pos + bytes_read;
}
else
if(feof(f))
fwrite(buf, sizeof buf, bytes_read, curr_file);
else
printf("Encountered Error during fread.\n");
}
int create_new_file(FILE *f,long sec_start, long sec_end)
{
FILE *new_file;
long bytes_read, bytes_to_read;
char buf[1200];
int read_pos = 0;
if ((new_file = fopen("new_file.log", "a+b"))==NULL)
{
printf("Cannot open file.\n");
return 1;
}
else
printf("New File Opened Correctly.\n");
fseek(f,sec_start,SEEK_SET);
bytes_to_read = sec_end - sec_start;
printf("bytes to read: %li \n", bytes_to_read);
getchar();
bytes_read = fread(buf, sizeof buf, bytes_to_read, f); //
printf("bytes read: %li \n", bytes_read);
getchar();
fseek(f,0L,SEEK_CUR);
read_pos = fgetc(f);
printf("read position: %i \n", read_pos);
getchar(); // PAUSE DISPLAY CONSOLE
if (bytes_read == sizeof buf)
{
fwrite(buf, sizeof buf, bytes_to_read, new_file);
read_pos = read_pos + bytes_read;
}
else
if(feof(f))
fwrite(buf, sizeof buf, bytes_read, new_file);
else
printf("Encountered Error during fread.\n");
}
int main()
{
FILE *f;
int c, i, matchcount = 0;
int pattern[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
int pattern_size = 6;
int bytes_read;
long sec_start, sec_end;
bool seg_unchanged = false;
bool sector_copy = false;
bool get_id = false;
char buf[1024];
FILE *curr_file, *new_file;
if ((f = fopen("original_data.log", "rb"))==NULL)
{
printf("Cannot open file.\n");
return 1;
}
else
printf("File Opened Correctly.\n");
if (seg_unchanged)
// append to old file
append_to_file(f,sec_start,sec_end);
else
// create new file
create_new_file(f,sec_start,sec_end);
//get_sec_end(f,pattern);
return 0;
}
|