Click here to Skip to main content
15,923,120 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C++
private:
	void split_it()
	{
		FILE *fpi, *fpo;//file pointers for the input and output files respectively.
		char * dr = new char[1024];
		char inputfilename[100] = "", tmp[20] = ".part", tname[80] = "", c;
		int n = 2, j = 0, l = 0, m = 0, rem = 0;
		long divided = 0;
		double k = 0;
		n = int(No_of_Parts; Value);
		size_t size;

		for each (char t in Input_Path - > Text)
		{
			inputfilename[j] = t;
			j++;
		}

		j = 0;

		if ((fpi = fopen(inputfilename, "rb")) == NULL)
		{
			System::Windows::Forms::MessageBox::Show("Invalid File Name:");
			exit(0);
		}

		fseek(fpi, 0, SEEK_END);
		k = ftell(fpi);
		rewind(fpi);

		for (j = strlen(inputfilename); j> = 0; j--)
		{
			if (inputfilename[j] == '.')
			{
				for (m = j - 1; inputfilename[m] != '/'; m--)
				{
					tname[l] = inputfilename[m];
					l++;
				}
			}
		}
		strrev(tname);

		for (j = 0; j< n; j++)
		{
			c = j + '0';
			output[j].outputfiles = new char[20];
			strcpy(output[j].outputfiles, inputfilename);
			tmp[strlen(tmp)] = c;
			strcat(output[j].outputfiles, tmp);

			strcpy(tmp, ".part");
		}

		divided = long(k) / n;

		rem = long(k) - (divided*n);

		m = divided / 512;

		for (j = 0; j< (n - 1); j++)
		{
			l = 0;
			fpo = fopen(output[j].outputfiles, "wb+");

			while (l != m)
			{
				size = fread(dr, 1, 512, fpi);
				fwrite(dr, size, 1, fpo);
				l++;
			}

			m = divided - (m * 512);
			size = fread(dr, 1, m, fpi);
			fwrite(dr, size, 1, fpo);
			fclose(fpo);
			m = divided / 512;
		}

		l = 0;
		fpo = fopen(output[j].outputfiles, "wb+");

		while (l != m)
		{
			size = fread(dr, 1, 512, fpi);
			fwrite(dr, size, 1, fpo);
			l++;
		}

		m = divided - (m * 512);
		m = m + rem;
		size = fread(dr, 1, m, fpi);
		fwrite(dr, size, 1, fpo);
		fclose(fpi);
		fclose(fpo);

	}

I m created the CLR c++ Project in visual studio.Added the new Windows Form.
Form application was working correctly.But when i call the above function to split the selected file using the OpenFileDialog component.it splits the file correctly,then the application Exiting automatically instead of returning back.
Posted
Updated 10-Jan-16 6:56am
v2
Comments
Kornfeld Eliyahu Peter 10-Jan-16 12:56pm    
Have you debug it?
Suvendu Shekhar Giri 10-Jan-16 13:18pm    
As @Kornfeld asked, have you debug it. If yes, what are your observations?
KarstenK 10-Jan-16 13:21pm    
Please use a debugger and if you find the problematic function - check the return code and google for the function.
AK96 10-Jan-16 21:23pm    
Yes I debugged it.The Application exits when the execution of the last statement of this function.that is after the execution of fclose(fpo);
But the file is splitting correctly.
I mixed this logic code with the interface code.
is it is the problem?

1 solution

Well, you should try to make better code... I would suggest you to read book on writing solid and maintainable code.

Here are some suggestions:
- Split code in multiple, self-documented question.
- Avoid hard-code constant
- Avoid memory leaks.
- Give meaningful names to variables.
- Uses existing functions to change the format of a string.
- Make reusable code (don't mix UI code with business logic).
- Declare variable on their first use.
- Avoid explicit memory management.
- Uses C++ style cast
- Avoid hard-code buffer size that might not always be big enough.
- Avoid mixing C, C++ and C++/CLI code without good reason.
- Properly handle Unicode characters.
- ...

As someone else mentioned, you should use a debugger.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900