tapi run-nya pake cmd
pada tau gak cara run C++ lewat cmd?? kalo belum tanya tuh ama om google.
programnaya sebagai berikut. tapi ini masih acak-acakan gitu. trus belum ada komentnya. mungkin nanti punya waktu luang akan dibenerin. oke silakan dilihat..
file: compress.cpp
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void compress( char* data, int count, FILE* outfile );
- char* readFileData( char* filename, int* count_ptr );
- int main( int num_args, char* arg_values[] )
- {
- if (num_args != 2)
- {
- printf( "Usage: rle filename (produces filename.rle)\n" );
- printf( " rle filename.rle (produces filename.plain)\n" );
- exit(1);
- }
- char* input_filename = arg_values[1];
- // read the file data into an array
- int count;
- char* data = readFileData(input_filename,&count);
- // Call compress() or decompress().
- FILE* outfile;
- int len = strlen(input_filename);
- if (len < 4 || (strcmp(input_filename+(len-4),".rle") != 0 && strcmp(input_filename+(len-4),".txt")==0))
- {
- char output_filename[80];
- strcpy( output_filename, input_filename );
- strcat( output_filename, ".rle" );
- printf( "Compressing %s to %s\n", input_filename, output_filename );
- outfile = fopen( output_filename, "wb" );
- compress( data, count, outfile );
- }
- else {}
- // Close the output file to ensure data is saved.
- fclose(outfile);
- // Free the array we allocated
- delete data;
- return 0;
- }
- void compress( char* data, int loop, FILE* outfile )
- {
- char character,x;
- char next_character;
- int count=1;
- // TODO: compress the data instead of just writing it out to the file
- for (int i=0; i<loop; ++i)
- {
- if(!character){
- character=data[i];
- }
- else{
- next_character=data[i];
- if(character==next_character){
- count++;
- }
- else if(count==1){
- putc( character, outfile );
- character=next_character;
- }
- else if(count<=3){
- for(int j=i-count; j<i; j++){
- putc( data[j], outfile );
- }
- count=1;
- character=next_character;
- }else{
- putc('*', outfile);
- putc(character, outfile);
- x=count+'0';
- putc(x, outfile);
- count=1;
- character=next_character;
- }
- if(i+1==loop){
- if(count==1){
- putc( character, outfile );
- putc( data[i], outfile );
- }
- else if(count<=3){
- for(int j=i-count+1; j<=i; j++){
- putc( data[j], outfile );
- }
- }
- else{
- putc('*', outfile);
- putc(character, outfile);
- x=count+'0';
- putc(x, outfile);
- }
- }
- }
- //putc( data[i], outfile ); // write out a single byte of data
- }
- }
- char* readFileData( char* filename, int* count_ptr )
- {
- // Returns a pointer to an array storing the file data.
- // Sets the variable pointed to by 'count' to contain the file size.
- // Exits the program if the filename doesn't exist.
- FILE* infile = fopen(filename,"rb");
- if ( !infile )
- {
- printf( "No such file \"%s\"!\n", filename );
- exit(1);
- }
- // Get file size by going to the end of the file, getting the
- // position, and then going back to the start of the file.
- fseek( infile, 0, SEEK_END );
- int count = ftell(infile);
- fseek( infile, 0, SEEK_SET );
- // read the data from the file
- char* data = new char[count];
- fread( data, 1, count, infile );
- fclose(infile);
- *count_ptr = count;
- return data;
- }
file: decompress.cpp
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void decompress( char* data, int count, FILE* outfile );
- char* readFileData( char* filename, int* count_ptr );
- int main( int num_args, char* arg_values[] )
- {
- if (num_args != 2)
- {
- printf( "Usage: rle filename (produces filename.rle)\n" );
- printf( " rle filename.rle (produces filename.plain)\n" );
- exit(1);
- }
- char* input_filename = arg_values[1];
- // read the file data into an array
- int count;
- char* data = readFileData(input_filename,&count);
- // Call compress() or decompress().
- FILE* outfile;
- int len = strlen(input_filename);
- if(len < 4 || strcmp(input_filename+(len-4),".rle") == 0)
- {
- char output_filename[80];
- strncpy( output_filename, input_filename, len-4 );
- output_filename[len-4] = 0;
- //strcat( output_filename, ".txt" );
- printf( "Decompressing %s to %s\n", input_filename, output_filename );
- outfile = fopen( output_filename, "wb" );
- decompress( data, count, outfile );
- }
- else{}
- // Close the output file to ensure data is saved.
- fclose(outfile);
- // Free the array we allocated
- delete data;
- return 0;
- }
- void decompress( char* data, int loop, FILE* outfile )
- {
- char character;
- int count;
- // TODO: decompress the data instead of just writing it out to the file
- for (int i=0; i<loop; ++i)
- {
- if(data[i]=='*'){
- character = data[i+1];
- count = atoi (&data[i+2]);
- for(int j=0; j<count; j++){
- putc(character, outfile);
- }
- i+=2;
- }
- else{
- putc( data[i], outfile ); // write out a single byte of data
- }
- }
- }
- char* readFileData( char* filename, int* count_ptr )
- {
- // Returns a pointer to an array storing the file data.
- // Sets the variable pointed to by 'count' to contain the file size.
- // Exits the program if the filename doesn't exist.
- FILE* infile = fopen(filename,"rb");
- if ( !infile )
- {
- printf( "No such file \"%s\"!\n", filename );
- exit(1);
- }
- // Get file size by going to the end of the file, getting the
- // position, and then going back to the start of the file.
- fseek( infile, 0, SEEK_END );
- int count = ftell(infile);
- fseek( infile, 0, SEEK_SET );
- // read the data from the file
- char* data = new char[count];
- fread( data, 1, count, infile );
- fclose(infile);
- *count_ptr = count;
- return data;
- }
jangan lupa ninggalin komentarnya yah.. ditunggu loh..
oke sekian semoga bermanfaat.
gan,, gimana tuh pas di klik kebuka tapi langsung kok malah cepet ilang .. gmn tuh ga ? mohon pencerahannya
BalasHapusrun nya lewat apaan?? kalo pake kayak borland harus di tambah
Hapus#include
dan tambahin
getch();
diantara delete data; ama return 0; di main function
biar ketahan gitu.
tapi kalo runnya pake cmd pasti ketahan.