Read in n number of bytes from a file...

Jun 7, 2012 at 6:27pm
For every 188 bytes of data in a text file I need to pass the data to an encryption library for encryption (I know how to do the encryption bit). I don't know how to make my program read in in increments of 188 bytes for processing.

Assume my encrypt/decrypt functions work (because they do). In bold is where my attempt begins...how does that look for a start? And how do I go from here to specifiy every 188 bytes..?

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
int main(int argc, char* argv[])
{
    ifstream fin;

    fin.open("testfile.txt", fstream::in);  //Opens the text file - fstream::binary as well??
       
    if(!fin.is_open())       //If the file opening fails, ERROR message prints
    {
         printf("ERROR: The file open failed.\n");  
         system("PAUSE");
         exit(1);
    }  

	else
	{
		printf("File opened successfully!\n");
	}

	char message[BLOCKSIZE];
	char key[16];
	int length;
	char * buffer;

	// get length of file:
	fin.seekg (0, fstream::end);
	length = fin.tellg();  
	fin.seekg (0, fstream::beg);

	cout << "Length of file: " << length << "\n";

	// allocate memory:
	buffer = new char [length];

	// read data as a block:
	fin.read (buffer,length);  //replace length with 188 in some sort of loop??
	fin.close();

	strcpy(message, buffer.c_str());

	delete[] buffer;


	srand ( time(NULL) );

	// generate a random key
	for (int i=0;i<16;i++) {
	key[i]=rand() % 256;
	}

	int msglen=BLOCKSIZE;

	printf("original message: %s\n",message);
	TestEncryptor(key,16*8,(BYTE*)message,msglen);
	printf("encrypted message: %s\n",message);
	TestDecryptor(key,16*8,(BYTE*)message,msglen);
	printf("decrypted message: %s\n",message);

	gets(message);
	return 0;

	fin.close();
}
Last edited on Jun 8, 2012 at 6:02pm
Topic archived. No new replies allowed.