Click to See Complete Forum and Search --> : I am starting to really hate this program...


Charred_Phoenix
01-20-2003, 06:55 PM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

int main(void) {
char choice[9];
printf("\t\t\tCharred_Phoenix's Image Zipper\t\t\t\n\n");
choice:
printf("Join or Seperate?");
scanf("%s", choice);
printf("\n");
if(strcmp("join", choice)==0) {
join();
}
else if(strcmp("seperate", choice)==0) {
seperate();
}
else {
printf("Not a valid choice please enter either 'join' or 'seperate'\n");
goto choice;
}
return 0;
}

int join(void) {
FILE *fobo;
char firstfile[255];
char secondfile[255];
char thirdfile[255];
char fourthfile[255];
char fifthfile[255];
char filecon[45472];
char filesource[90000];
char destfile[250];
char gifbuff[100];
int filedes;
int filedes2;
int b_read;

printf("Enter up to 5 source files:");
scanf("%s %s %s %s %s", firstfile, secondfile, thirdfile, fourthfile, fifthfile);
printf("\nEnter destination filename:");
scanf("%s", destfile);
strcat(destfile, ".gif");
fobo = fopen(firstfile, "r");
fscanf(fobo, "%s", filesource);
fclose(fobo);
strcpy(filecon, "\n____________________\n");
strcat(filecon, filesource);
strcat(filecon, "\n....................\n");

fobo = fopen(secondfile, "r");
fscanf(fobo, "%s", filesource);
fclose(fobo);
strcat(filecon, filesource);
strcat(filecon, "\n....................\n");

fobo = fopen(thirdfile, "r");
fscanf(fobo, "%s", filesource);
fclose(fobo);
strcat(filecon, filesource);
strcat(filecon, "\n....................\n");

fobo = fopen(fourthfile, "r");
fscanf(fobo, "%s", filesource);
fclose(fobo);
strcat(filecon, filesource);
strcat(filecon, "\n....................\n");

fobo = fopen(fifthfile, "r");
fscanf(fobo, "%s", filesource);
fclose(fobo);
strcat(filecon, filesource);
strcat(filecon, "\n....................\n");

creat(destfile, 0700);
while(b_read > 0) {
filedes = open("bar.gif",O_RDONLY, 0700);
filedes2 = open(destfile, O_WRONLY, 0700);
b_read = read(filedes, gifbuff, 100);
write(filedes2, gifbuff, b_read);
}
write(filedes2, filecon, 45472);

return 0;
}

int seperate(void) {
FILE *fobo;
char file1con[10000];
char file2con[10000];
char file3con[10000];
char file4con[10000];
char file5con[10000];
char filename[255];


printf("Enter image-zip filename:");
scanf("%s", filename);
strcat(filename, ".gif");
fobo = fopen(filename, "r");
fscanf(fobo, "FF\n____________________\n%s\n.................... \n%s\n....................\n%s\n.................. ..\n%s\n....................\n%s\n................ ....\n", file1con, file2con, file3con, file4con, file5con);
fclose(fobo);

fobo = fopen("1", "w");
fprintf(fobo, "%s", file1con);
fclose(fobo);

fobo = fopen("2", "w");
fprintf(fobo, "%s", file2con);
fclose(fobo);

fobo = fopen("3", "w");
fprintf(fobo, "%s", file3con);
fclose(fobo);

fobo = fopen("4", "w");
fprintf(fobo, "%s", file4con);
fclose(fobo);

fobo = fopen("5", "w");
fprintf(fobo, "%s", file5con);
fclose(fobo);

printf("Unzip sucessful");
return 0;
}


This, evil, evil program attempts to print bar.gif's contents to the screen, can anyone tell me why?
Thx

wapcaplet
01-20-2003, 07:05 PM
Perhaps it would help if you told us what the program is supposed to do...

Charred_Phoenix
01-20-2003, 07:30 PM
You mean what it's meant to do or why it's meant to do it?

It's meant to print into a file the following:

Contents of bar.gif
____________________
Contents of file 1
....................
Contents of file 2
....................
Contents of file 4
....................
Contents of file 5
....................

As for why it's meant to do it, that's a longer story so i'll wait to see if you want to hear it.

Spawn913
01-20-2003, 09:21 PM
are file1, file2, file3, file4, file5 text files? If they aren't, you shouldn't be relying on fscanf and strcat on getting and copying their contents. this also implies that if you do change the fscanf's and fprintf's to read's and write's, then you should be using open instead of fopen.

as for your program trying to print the contents of bar.gif on the screen, it would be helpful if you place error checking on your code. at least in the fopen (or open) calls, check the return values, i.e.

(just an example)
f=open ("file1", O_CREAT | O_RDWR, S_IRWXU);
if (f<0) perror("opening file1");

tell us what happens then.

wapcaplet
01-20-2003, 09:54 PM
Yeah I'd concur, I don't think those are the appropriate functions for doing binary I/O. Haven't done C stuff in a while but I believe read() and write() are the more low-level ones that you'd probably want to be using. fscanf and strcat and the other string-manipulation ones *sometimes* work, but they tend to make interpretations about what kind of "text" is being represented, which you don't want if you're processing binary stuff.

Charred_Phoenix
01-20-2003, 10:44 PM
The files 1-5 /are/ text file currently, but i plan to change that, i just haven't got round to making it read/write in those parts cos then I have to completely redo seperate() and make it use a huge bunch of if/elses rather than casts.

Charred_Phoenix
01-22-2003, 03:55 AM
Well I seem to have forgotten how to use read and write as I now can't even get the following code working:


#include <stdio.h>
#include <fcntl.h>

int main(void) {
int fd1;
int fd2;
int b_read;
int b_written;
char gifbuf[10000000];

fd1 = open("bar.gif", O_RDONLY, 0700);
creat("duck.gif", 0700);
fd2 = open("duck.gif", 0700);
b_read = 2;

while(b_read > 0) {
b_read = read(fd1, gifbuf, 100);
b_written = write(fd2, gifbuf, b_read);
if(b_written != b_read) {
printf("Error in Write");
}
}
return 0;
}



Any help appreciated.