Add file io solutions.
This commit is contained in:
1 parent
3b5d111351
commit
45336b083c
3 files changed
+143
-41
No files matched your search
+102
-11
@@ -3,8 +3,30 @@
|
||||
#include <string.h>
|
||||
#include "namesarchive.h"
|
||||
|
||||
// Fügt einen Namen hinzu. Im Fehlerfall soll 0, ansonsten 1 zurückgegeben werden.
|
||||
static char names[MAX_PERSONS][MAX_NAME_LEN];
|
||||
static unsigned int numberOfPersons = 0;
|
||||
|
||||
static int findPersonIdx(const char *name);
|
||||
static void removePersonByIdx(unsigned int idx);
|
||||
static int compareNames(const void *arg1, const void *arg2);
|
||||
|
||||
// Fügt einen Namen hinzu. Im Fehlerfall (Archiv ist voll) soll 0, ansonsten 1 zurückgegeben werden.
|
||||
// Ist der angegebene Name länger als die zulässige Namenslänge, wird der Name abgeschnitten hinzugefügt. Rückgabe ist dann 1.
|
||||
int addName(const char *name)
|
||||
{
|
||||
if(numberOfPersons < MAX_PERSONS)
|
||||
{
|
||||
strncpy(names[numberOfPersons], name, MAX_NAME_LEN);
|
||||
names[numberOfPersons][MAX_NAME_LEN-1] = '\0';
|
||||
numberOfPersons++;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Wie addName. Fügt Namen aber direkt sortiert hinzu. Voraussetzung ist ein bereits sortiertes Archiv.
|
||||
int addNameSorted(const char *name)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -12,29 +34,98 @@ int addName(const char *name)
|
||||
// Entfernt den angegebenen Namen. Gibt bei Erfolg 1 zurück, ansonsten 0 (Name konnte nicht gefunden werden).
|
||||
int removeName(const char *name)
|
||||
{
|
||||
return 0;
|
||||
int nameIdx = findPersonIdx(name);
|
||||
|
||||
if(nameIdx >= 0)
|
||||
{
|
||||
removePersonByIdx(nameIdx);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int findPersonIdx(const char *name)
|
||||
{
|
||||
for(int i = 0; i < numberOfPersons; i++)
|
||||
{
|
||||
if(strcmp(names[i], name) == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void removePersonByIdx(unsigned int idx)
|
||||
{
|
||||
for(unsigned i = idx+1; i < numberOfPersons; i++)
|
||||
{
|
||||
strncpy(names[i-1], names[i], MAX_NAME_LEN);
|
||||
names[i-1][MAX_NAME_LEN-1] = '\0';
|
||||
}
|
||||
numberOfPersons--;
|
||||
}
|
||||
|
||||
int compareNames(const void *arg1, const void *arg2)
|
||||
{
|
||||
const char (*name1)[MAX_NAME_LEN] = arg1;
|
||||
const char (*name2)[MAX_NAME_LEN] = arg2;
|
||||
|
||||
return strcmp(*name1, *name2);
|
||||
}
|
||||
|
||||
// Sortiert die Namen im Archiv aufsteigend.
|
||||
void sortNames()
|
||||
{
|
||||
qsort(names, numberOfPersons, sizeof(names[0]), compareNames);
|
||||
}
|
||||
|
||||
// Gibt die Namen zeilenweise aus.
|
||||
void printNames()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Leert das Archiv
|
||||
void clearArchive()
|
||||
{
|
||||
|
||||
for(int i = 0; i < numberOfPersons; i++)
|
||||
{
|
||||
printf("%s\n", names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Lädt den Inhalt einer Textdatei zeilenweise in das Archiv. Gibt 1 bei Erfolg zurück.
|
||||
int loadArchive(const char *path)
|
||||
{
|
||||
return 0;
|
||||
FILE *file = fopen(path, "r");
|
||||
char buffer[MAX_NAME_LEN];
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
while(fgets(buffer, sizeof(buffer), file) != NULL)
|
||||
{
|
||||
if(buffer[strlen(buffer)-1] == '\n')
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
|
||||
addName(buffer);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
return file != NULL;
|
||||
}
|
||||
|
||||
// Speichert das Archiv zeilenweise in einer Textdatei. Gibt 1 bei Erfolg zurück.
|
||||
int saveArchive(const char *path)
|
||||
{
|
||||
return 0;
|
||||
FILE *file = fopen(path, "w");
|
||||
|
||||
if(file != NULL)
|
||||
{
|
||||
for(int i = 0; i < numberOfPersons; i++)
|
||||
{
|
||||
fprintf(file, "%s\n", names[i]);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
return file != NULL;
|
||||
}
|
||||
Reference in new issue
Block a user