Not able to write jpegs to file

I have written a tar program and now I can take files add them to a tarfile and extact them into another directory. The problem is that it doesn't work with jpegs. I imagine in it has to do with how I am reading and writing because it is a binary file. I have used "rb" "wb+" ect for accessing the files. I can not figure out what I am missing here. Any help would be nice, thanks

My code -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <utime.h>

using namespace std;
struct stat sb;
typedef struct{
	char *Name;
	char *Contents;
}inFile;

inFile *in_File;
int numArgs;
FILE *fp;
char *test;
char stdinTar[200000000];
char tar[3];
char *name;
int c;
int strl;
int statstrl;
int count;
//Vars for incoming file stats - 
struct utimbuf buf;
char fStats[500];
char fcontents[2000000];
char fsizes[2000000];
char inName[100];
long int insize;
long int inatime;
long int inmtime;
int inpermissions;
int isContent = 0;

//Get file info for tarfile.tar
void getFinfo(struct stat sb,FILE *fp,char *name){
	//File info - name,size,atime,mtime,permissions
	printf("%s\t",name);
	printf("%ld\t%ld\t%ld\t%d\n",sb.st_size,sb.st_atime,sb.st_mtime,sb.st_mode);
	test = (char *) malloc(sb.st_size+1);
	fread(test,sb.st_size,1,fp);
	printf("%s",test);
}

//Need to close files

void getFinfo(struct stat sb,FILE *fp,char *name);

int main(int argc, char **argv, char **envp){
	if(strcmp(argv[1],"-x") == 0){
			//Scan stdin to get file info for new files.	
			while((c = fgetc(stdin)) != EOF){
				//if loop will be entered when file contents
				//are reached.
				if(isContent == 1){	
					if(count != insize -1){
						//add chars until file size is reached(insize)
						//then I will be at end of contents and ready for
						//next set of file stats.
						fcontents[count] = c;
						count++;//used to determine when size of contents is reached
					}else{
						fcontents[count] = NULL;
						isContent = 0;//prevents chars from being but into fcontents
						count =0;//Ready count for adding stats into fStats
						//temp, I need to write f contents to a file.
						printf("%s\n%s\n","This is the file contents - ",fcontents);
						fp = fopen(inName,"ab+");
						fwrite(&fcontents,strlen(fcontents),1,fp);
						fclose(fp);
					}	
				//Set file permisions and time	
				}else if(c == '\n'){
					//Reached end of stat line, adjust new file.
					fStats[count] = NULL;
					count = 0;//Ready count for add fcontents
					//temp
					printf("%s\n%s","This is the stats - ",fStats);
					statstrl = strlen(fStats);

					//Scan stat data into vars
					sscanf(fStats,"%s%ld%ld%ld%d",inName,&insize,&inatime,&inmtime,&inpermissions);
					fp = fopen(inName,"wb+");//Create writeable readable file.
					//set filepermissions
					chmod(inName,inpermissions);
					//Set time
					buf.actime = inatime;
					buf.modtime = inmtime;
					if(utime(inName, &buf) == -1){
						perror("utime() failed, ERRNO");
						exit(1);
					
					}
					fclose(fp);
					//Set to one so that if condition is met above and fcontents are added.
					isContent = 1;
					
					//printf("\n%s\n%ld\n%ld\n%ld\n%d\n",inName,insize,inatime,inmtime,inpermissions);
				}else{
					//add stats for file, increase counter.
					fStats[count] = c;
					count++;
				}
			}
			//	strl = strlen(stdinTar);
			//	for(int i=0; i<strl; i++){
			//		if(stdinTar[i] != '\n'){
			//			//printf("%d", stdinTar[i]);
			//		}
			//	}
				
			//int mythis = getc(stdin);
			//printf("%s\n%d\n","wwhis is stdin size - ",mythis);
			//fseek(fp,70, SEEK_SET);

			printf("%s%d","Tssfdjkadf;aer - ",count);
			
}else{	
	//Error check argv arrays.
	if(argv[1] == NULL){
		printf("%s\n","usage: ./my_tar option [files . . .]\n\t\toption = -c | -x ");	
		exit(1);
	}

	//Check files to see if they are regular.
	for(int i = 1; i < argc; i++){
		if(stat(argv[i],&sb) != -1  && (S_ISREG(sb.st_mode))){
			//Open current file
			fp = fopen(argv[i],"rb");
			//Allocate memory for file name
			name = (char *) malloc(strlen(argv[1]));
			//Store filename
			name = argv[i];
			//Get file information for tarfile
			getFinfo(sb,fp,name);
		}else{
			fprintf(stderr, "Skipping, this is not a regular file -%s\n", argv[i]);
		}
		}
		
	//Write to standard output
	//Free memory
	//Unpack tarfile

//----------------------------Time File changing code -------------------------------------------
//Get time by accessing st_atime and st_mtime. Then use those var values to put into actime as below.

			//struct utimbuf buf;
			//buf.actime = 1422748304;
			//buf.modtime = 1422748304;
			//if(utime(argv[i], &buf) == -1){
			//	perror("utime() failed, ERRNO");
			//	exit(1);
		//	}




			//fread(&stdinTar,200000002,1,stdin);
			//printf("%s",stdinTar);













	}
}
And you don't run tar because?
For learning purposes
Perhaps you should get it working the way it was intended to work before pulling it apart.
Topic archived. No new replies allowed.