techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog


Technical Interview Questions

Visual Basic Interview Question
ASP .NET Interview Questions
C++ Interview Questions
C Interview Questions
                              .........More

Programming Source Codes
C/C++ Source Codes
C# Source Codes
                              .........More

Aptitude Interview Questions
C/C++ Aptitude Questions
C Aptitude Questions
                              .........More

Tutorials
C Tutorial
C++ Tutorial
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

 

 

  

C/C++ Source Codes

copy a file partly.(only required portion).more USEFUL for AUDIO or VEDIO files

/*filecopy.c : can copy files partly*/

#include <stdio.h>
#include <stdlib.h>

int file_copy(char *oldname, char *newname, long bytes1, long bytes2);
long get_bytes(float percent, char *source);
main()
{
char source[80], destination[80];
float percent1, percent2;

/*Get the file names*/
puts("Enter source file:");
printf("\t");
gets(source);
puts("\nEnter destination file \(WARNING: If the file exists, it will be OVERWRITTEN\): ");
printf("\t");
gets(destination);

/*Get the positions in percentage*/
puts("\nEnter start percentage (recommended: 0): ");
printf("\t");
scanf("%f", &percent1);
do
{
puts("\nEnter end percentage:");
printf("\t");
scanf("%f", &percent2);
} while ( percent2 < percent1 );


if ( file_copy( source, destination, get_bytes(percent1, source), get_bytes(percent2, source) ) == 1 )
puts("\nCopy operation SUCCESSFUL");
else
fprintf(stderr, "\nError during copy operation");

fflush(stdin);
puts("\nPress any key to exit . . . ");
getchar();
return(0);
}

/*Function for actual copying of file, return 1 on success, 0 otherwise*/
int file_copy( char *oldname, char *newname, long bytes1, long bytes2)
/*bytes1 denotes the start position in bytes, bytes2 denotes the end position*/
{
FILE *fold, *fnew;
int c;

if ( ( fold = fopen( oldname, "rb" ) ) == NULL )
return 0;

if ( ( fnew = fopen( newname, "wb" ) ) == NULL )
{
fclose ( fold );
return 0;
}

/*Set file pointer to proper start location, as specified by user*/
if ( ( fseek(fold, bytes1, SEEK_SET) != 0 ) )
{
fprintf(stderr, "\nError using fseek().");
exit(1);
}

while (1)
{
c=fgetc(fold);

/*Continue copying until end of file or until the requested limit has been reached*/
if ( !feof( fold ) && ftell(fold) <= bytes2)
fputc( c, fnew );
else
break;
}

fclose ( fnew );
fclose ( fold );

return 1;
}


/*This function finds how many bytes need to copied, calculating it from the percentage inputted by the user*/
long get_bytes(float percent, char *source)
{
long bytes;
FILE *fold;

if (percent<=100)
{
if ( ( fold = fopen( source, "rb" ) ) == NULL )
{
puts("Error opening source file");
exit(1);
}
if ( (fseek(fold, 0, SEEK_END))!=0)
{
fprintf(stderr, "\nError using fseek().");
exit(1);
}

bytes=ftell(fold);
bytes*=(percent/100);
}
else
{
fprintf(stderr, "Error in input");
exit(1);
}

fclose(fold);
return bytes;
}


<<<----- Return to C/C++ Source Code Questions Page.


Have a Question ? post your questions here. It will be answered as soon as possible.

Check Object Oriented Interview Questions for more Object Oriented Interview Questions with answers

Check Data Structure Interview Questions for more data structure interview questions with answers