I'v written the program below,
but I'v been told its too "intense"
Any ideas on how I can get the same results but with a simpler program?

#define MAX_SIZE 10000



char buf[MAX_SIZE], name[1000];



int min(int a, int b) {

	return a<b?a:b;

}



int main(int argc, char** argv) {



	FILE *out, *in;

	int size, i, cnt, temp;



	if(argc == 3 && strcmp(argv[1], "-e") == 0) {

		in = fopen(argv[argc-1], "rb");

		while(1) {

			if(fgets(buf, MAX_SIZE, in) == NULL)

				break;

			printf("%s\n", buf);

			fflush(stdout);

			sscanf(buf, " %d$%s", &size, name);

			//temp = strlen(name);

			//name[temp] = '_';

			//name[temp+1] = 0;

			printf("%s %d\n", name, size);

			fflush(stdout);

			out = fopen(name, "wb");

			while(size > 0) {

				cnt = fread(buf, 1, min(MAX_SIZE, size), in);

				fwrite(buf, 1, cnt, out);

				size -= cnt;

			}

			fflush(out);

			fclose(out);

		}



	}else if(argc > 3 && strcmp(argv[1], "-c") == 0){



		out = fopen(argv[argc-1], "wb");

		for(i=2; i<argc-1; i++) {

			in = fopen(argv[i], "rb");

			fseek(in, 0, SEEK_END);

			size = ftell(in);

			fprintf(out, "%d$%s\n", size, argv[i]);

			fseek(in, 0, SEEK_SET);

			while((cnt = fread(buf, 1, MAX_SIZE, in)) != 0)

				fwrite(buf, 1, cnt, out);

			fclose(in);

		}

		fclose(out);

	}



	return 0;

}

Well, could you post what is your requirement? and what was told to be "intense" in your code?

You could probably simplify the file operations. What is this program supposed to do?

The program reads x amount of files to an archive.
Then extracts the files.

-c to indicate that you want to archive files, then specify the names of the files, finally indicate the name of the archive file.
-e to indicate that you want to extract files then specify the name of the archive file to be extracted.

Example (assuming the executable is called "archive"):
archive -c file1.txt file2.txt file3.txt my_archive

archive -e my_archive

Then your code is presently minimal. Any simplification (without altering behavior) would be too minor to justify the claim of "too intense". Can you be more specific about what "too intense" means?

I'm not quite sure what they meant by it either.

Another issue I have with the code, I can't seem to get rid of the buffer size on the output, once the files have been extracted using -e

You could include <stdlib.h> and then malloc the memory for buff[]. When you no longer needed it, you could then free(buff).

That would save a lot of the static memory space, which is much more scarce than heap memory, (where malloc() gets it's memory for you).

I'm not sure how to implement malloc

From the help file of Turbo C:

malloc Allocates memory.

Syntax:
void *malloc(size_t size);

Prototype in:
alloc.h stdlib.h

Remarks:
malloc allocates a block of size bytes from
the memory heap. It allows a program to
allocate memory explicitly as it's needed, and
in the exact amounts needed.

The heap is used for dynamic allocation of
variable-sized blocks of memory. Many data
structures, such as trees and lists, naturally
employ heap memory allocation.

<DOS data deleted>

Return Value:
On success, malloc returns a pointer to the
newly allocated block of memory.

If not enough space exists for the new block,
it returns null. The contents of the block are
left unchanged.

If the argument size == 0, malloc returns
null.

Portability:
malloc is available on UNIX systems and is
defined in ANSI C.

See Also:
realloc
calloc free

Example:

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

 int main(void)
 {
    char *str;

    /* allocate memory for string */
    if ((str = malloc(10)) == NULL)
    {
       printf("Not enough memory to allocate buffer\n");
       exit(1);  /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);

    return 0;
 }

I'm not sure that using malloc() will make your program "less intense". It will use less stack space, which is in limited supply, however.

Talk to your advisor or teacher and have him/her explain what they mean by "too intense", in more detailed. Read between the lines also, and see what it is that they really want here - because it may not be malloc(), at all.

Thanks for your help.

I have been able to create the same kind of program, but it uses switchs and while if statements instead of a buffer.
BUT I can't seem to get it to save multiple files.

I don't know how archive logic works, but I would read up on the matter, and be sure I understood it (and could do a simple 2 or 3 file archive on paper), before trying to program it.

I can't solve a problem with a program, unless I really understand the problem, and know the way to solve it. The program can give you the answer, but it can't give you the right algorithm to get to the answer.

There is at least one open source compression program you can google up, but I don't know how much good it will do, without the info on the algorithm it uses.

@leedsfanatic request for remove/editing thread rejected. This is public forum not private paid service.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.