Creating a variable name for a file

I'm attempting to split a large binary file into smaller manageable files for analysis. I've written most of the software but I'm stuck in a couple of places.

1. The binary file is split by looking at a couple of bytes to determine when to create a new file or continue appending to the current new file. The question is when I need to create a new file, how can I dynamically sign it a name? My intention is to rename each subfile by: "original_name" + new section id + ".log".

2. The start of each section is determined by a specific pattern (6 bytes of FF's). I'm running into an issue where the pattern check is checking for 5 bytes instead of 6 because the for..loop doesn't increment for one instance.

I've attached the code I have so far. Any help appreciated.
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;
}
Last edited on
how can I dynamically sign it a name?
With a stringstream you may do it like so:
1
2
3
4
std::stringstream fn_ss;
fn_ss << original_name << new_seg_id << ".log";
	if ((new_file = fopen(fn_ss.str().c_str(), "a+b"))==NULL)
...


loop doesn't increment for one instance.
I guess that you mean the loop on line 16. I don't get what happens in there, but if you want the full patteren remove the -1 on line 16?
Thanks for the reply. I successfully implemented the dynamic name assignment as follows.

1
2
3
4
5
         char filename[50];
                   int file_number;
	sprintf(filename, "original_filename%X.log", file_number);
	
	if ((curr_file = fopen(filename, "a+b"))==NULL) .... 


Tested and works.
Topic archived. No new replies allowed.