Jody
May 2nd, 1999, 02:56 PM
date.txt file contains:
0001688239=11:02:40:12/12/1999
1163533198=03:20:01:02/07/1963
0728661599=10:00:00:11/23/1976
0583230589=15:30:10:07/04/1981
0133185599=12:00:00:10/11/1995
append.c
// append.c appends files to a file
//User enters this on Run line: append.c <file1> <app2file>
// User enters append date.txt sortdate to have text from date.txt appended to sortdate.txt file
// I WANT THE LINES IN DATE.TXT SORTED SO THEY ARE IN CHRONOLOGICAL ORDER when placed in the output file.
//HOW DO I DO THIS????
//I will offer a $25 Amazon.com gift cert. to the first person to write this code for me. I’ve run out of time.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
char temp[BUFSIZE];
void append(FILE *source, FILE *dest);
int main(int argc, char *argv[])
{
FILE *fa, *fr;
int file;
if (argc < 3)
{
fprintf(stderr, "Usage: %s source-file(s) destination-file\n", argv[0]);
exit(1);
}
if ((fa = fopen(argv[argc - 1], "a")) == NULL)
{
fprintf(stderr, "Can't open %s]n", argv[argc -1]);
exit(2);
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) !=0)
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
for (file = 1; file < argc - 1; file++)
{
if (strcmp(argv[argc -1], argv[file]) == 0)
fputs("Can't append file to itself\n", stderr);
else if ((fr = fopen(argv[file], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[file]);
else
{
if (setvbuf(fr, NULL, _IOFBF, BUFSIZE) !=0)
{
fputs("Can't create output buffer]n", stderr);
continue;
}
append(fr, fa);
if (ferror(fr) !=0)
fprintf(stderr, "Error in reading file %s.\n", argv[file]);
if (ferror(fa) !=0)
fprintf(stderr, "Error in writing file %s.\n", argv[argc -1]);
fclose(fr);
}
}
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
extern char temp[]; // use the external temp array
while ((bytes = fread(temp, sizeof(char), BUFSIZE,source)) > 0)
fwrite(temp, sizeof (char), bytes, dest);
}
0001688239=11:02:40:12/12/1999
1163533198=03:20:01:02/07/1963
0728661599=10:00:00:11/23/1976
0583230589=15:30:10:07/04/1981
0133185599=12:00:00:10/11/1995
append.c
// append.c appends files to a file
//User enters this on Run line: append.c <file1> <app2file>
// User enters append date.txt sortdate to have text from date.txt appended to sortdate.txt file
// I WANT THE LINES IN DATE.TXT SORTED SO THEY ARE IN CHRONOLOGICAL ORDER when placed in the output file.
//HOW DO I DO THIS????
//I will offer a $25 Amazon.com gift cert. to the first person to write this code for me. I’ve run out of time.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
char temp[BUFSIZE];
void append(FILE *source, FILE *dest);
int main(int argc, char *argv[])
{
FILE *fa, *fr;
int file;
if (argc < 3)
{
fprintf(stderr, "Usage: %s source-file(s) destination-file\n", argv[0]);
exit(1);
}
if ((fa = fopen(argv[argc - 1], "a")) == NULL)
{
fprintf(stderr, "Can't open %s]n", argv[argc -1]);
exit(2);
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) !=0)
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
for (file = 1; file < argc - 1; file++)
{
if (strcmp(argv[argc -1], argv[file]) == 0)
fputs("Can't append file to itself\n", stderr);
else if ((fr = fopen(argv[file], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[file]);
else
{
if (setvbuf(fr, NULL, _IOFBF, BUFSIZE) !=0)
{
fputs("Can't create output buffer]n", stderr);
continue;
}
append(fr, fa);
if (ferror(fr) !=0)
fprintf(stderr, "Error in reading file %s.\n", argv[file]);
if (ferror(fa) !=0)
fprintf(stderr, "Error in writing file %s.\n", argv[argc -1]);
fclose(fr);
}
}
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
extern char temp[]; // use the external temp array
while ((bytes = fread(temp, sizeof(char), BUFSIZE,source)) > 0)
fwrite(temp, sizeof (char), bytes, dest);
}