Append new primes to the end of a file.

This commit is contained in:
Nicolás A. Ortega 2017-02-13 09:47:47 +01:00
parent 2b5541d2c3
commit b20733f3ac
No known key found for this signature in database
GPG Key ID: 614272579C2070D1
3 changed files with 10 additions and 5 deletions

View File

@ -21,17 +21,17 @@ int inputPrimes(char *file, List *list) {
return 0; return 0;
} }
int outputPrimes(char *file, List *list) { int outputPrimes(char *file, List *list, size_t startPos) {
// Assert safeguards // Assert safeguards
assert(file); assert(file);
assert(list); assert(list);
FILE *out = fopen(file, "w"); FILE *out = fopen(file, "a");
if(!out) return 1; if(!out) return 1;
printf("Saving primes to `%s'...\n", file); printf("Saving primes to `%s'...\n", file);
puts("0%"); puts("0%");
for(size_t i = 0; i < list->end; ++i) { for(size_t i = startPos; i < list->end; ++i) {
if(!mpz_out_raw(out, list->list[i])) return 3; if(!mpz_out_raw(out, list->list[i])) return 3;
if(i == list->end / 4) puts("25%"); if(i == list->end / 4) puts("25%");
else if(i == list->end / 2) puts("50%"); else if(i == list->end / 2) puts("50%");

View File

@ -21,10 +21,12 @@ int inputPrimes(char *file, List *list);
* @brief Output primes from a List into an Indivisible file. * @brief Output primes from a List into an Indivisible file.
* @param file File to output primes to. * @param file File to output primes to.
* @param list List to read primes from. * @param list List to read primes from.
* @param startPos The position in the List of primes at which to append
* to the file.
* @returns If 0 then load was successful, if 1 then failed to open, * @returns If 0 then load was successful, if 1 then failed to open,
* if 2 failed to close, if 3 failed when writing. * if 2 failed to close, if 3 failed when writing.
*/ */
int outputPrimes(char *file, List *list); int outputPrimes(char *file, List *list, size_t startPos);
/** /**
* @brief Export primes from a List to a plain text file. * @brief Export primes from a List to a plain text file.

View File

@ -128,6 +128,8 @@ int main(int argc, char *argv[]) {
struct stat s; struct stat s;
if(stat(dfile, &s) == 0) newFile = false; if(stat(dfile, &s) == 0) newFile = false;
} }
// Last position added from file
size_t startPos = 0;
int exitCode = 0; int exitCode = 0;
@ -169,6 +171,7 @@ int main(int argc, char *argv[]) {
} else { } else {
// Load primes from file // Load primes from file
int err = inputPrimes(dfile, &primes); int err = inputPrimes(dfile, &primes);
startPos = primes.end;
if(err == 0) { if(err == 0) {
printf("Loaded %zu primes.\n", primes.end); printf("Loaded %zu primes.\n", primes.end);
@ -244,7 +247,7 @@ int main(int argc, char *argv[]) {
printf("Found %zu primes.\n", primes.end); printf("Found %zu primes.\n", primes.end);
if(dfile) { if(dfile) {
int err = outputPrimes(dfile, &primes); int err = outputPrimes(dfile, &primes, startPos);
if(err == 0) { if(err == 0) {
puts("Successfully saved primes."); puts("Successfully saved primes.");
} else { } else {