Hallo Leute, ich habe ein kleines (für mich unlösbares ) Problem.
Ich habe Programm zum verwalten von Adressen geschrieben.
Leider stürzt das Programm immer ab, nachdem man das 2. oder 3. mal eine suche gestartet hat.
Das Programm selbst sieht so aus:
Die Funktionen wie folgt:
und die Textdatei im Format:
Muster, Mustermann, musterstr. 1,12345,Musterort
Muster2,Mustermann2,musterstr. 2, 67890, Musterort2
usw.
Wäre wirklich sehr dankbar wenn jemand ne Idee hätte.
Rätsel da nun schon seit Tagen dran.
Achso ich benutze Dev-Cpp 4.9.9.2, falls das Relevant ist.
Dank und Gruß
Webrabbit
Ich habe Programm zum verwalten von Adressen geschrieben.
Leider stürzt das Programm immer ab, nachdem man das 2. oder 3. mal eine suche gestartet hat.
Das Programm selbst sieht so aus:
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "rf_function.h"
/*
Ä = \x8e
ä = \x84
Ö = \x99
ö = \x94
Ü = \x9a
ü = \x81
ß = \xe1
*/
int file_exists(char *); // prototype of function "file_exists"
long filesize(FILE *); // prototype of function "filesize"
int count(int arr[]); // prototype of function "count"
void initialise(int* intvar, int size); // prototype of function "initialise"
int main(void){
system("color 3f");
char choice, another = '\0';
int found[101];
initialise(found, 100);
int a, k, z, arraylen = 0, x = 0 , size = 0 , pos = 1, i = 0, fileend = 0, b, e =0;
struct address{
char forename[25];
char name[25];
char street[35];
char strno[4];
char zipcode[10];
char city[30];
};
struct address person[1000];
FILE * addresses; // Set File
fileend = filesize(addresses);
char* data; // reserve size for filedata
data = (char *) malloc(fileend);
char search[40];
char* String;
int f = 0, element = 0;
i = 0;
system("cls");
//start program output
printf("\n\n\n");
printf("\t###############################################\n");
printf("\t# Programm Adresskartei #\n");
printf("\t###############################################\n\n");
printf("\tBitte w\x84hlen Sie eine Option: \n\n");
if(file_exists("adressen.txt")){
printf("\t1. Neue Kartei anlegen\n");
printf("\t2. Alle Karteien anzeigen\n");
printf("\t3. Kartei Suchen\n");
printf("\t4. Kartei l\x94schen\n");
printf("\t5. Programm beenden\n\n");
}//END if(file_exists("adressen.txt"))
else{
addresses = fopen("adressen.txt", "w");
fclose(addresses);
printf("1. Neue Kartei anlegen\n");
}//END else(file_exists("adressen.txt"))
do{// repeat the menu selection until right choice
choice = getch();
if(i >= 1 ){
printf("Bitte geben Sie eine Zahl zwischen 1 und 4 ein.\n");
}//END if(i >= 1)
i++;
}//END do
while(choice < 49 || choice > 53);
switch(choice){ //main
case 49:
printf("\tNeue Kartei anlegen\n");
printf("\tName:"); scanf("%s", &person[0].name);
printf("\tVorname:"); scanf("%s", &person[0].forename);
printf("\tStrasse:"); scanf("%s", &person[0].street);
printf("\tHausnummer:"); scanf("%s", &person[0].strno);
printf("\tPLZ:"); scanf("%s", &person[0].zipcode);
printf("\tOrt:"); scanf("%s", &person[0].city);
if(addresses != NULL){
addresses = fopen("adressen.txt", "a");
fseek(addresses, 0, SEEK_END); //set pointer to end of file
fprintf(addresses, "%s,", person[0].forename);
fprintf(addresses, "%s,", person[0].name);
fprintf(addresses, "%s", person[0].street);
fprintf(addresses, " %s,", person[0].strno);
fprintf(addresses, "%s,", person[0].zipcode);
fprintf(addresses, "%s\n", person[0].city);
printf("\n\tDie Adresse wurde erfolgreich eingetragen");
}
fclose(addresses);
printf("\nMoechten Sie eine weiter Option durchf\x81hren? j/n\n");
another = getch(); //read userinput -> abort program?
if(another == 74 || another == 106){
main();
}
break;
case 50:
pos=1;
printf("\tAusgabe aller Karteien:\n\n");
addresses = fopen("adressen.txt", "r");
while(fscanf(addresses,"%i",0L) != EOF){
fgets(data,fileend,addresses);
printf("\t%i. %s",pos,data);
pos++;
}
printf("\n\n");
fclose(addresses);
printf("\n\tMoechten Sie eine weitere Option durchf\x81hren? j/n\n");
another = getch(); //read userinput -> abort program?
if(another == 74 || another == 106){
main();
}
break;
case 51:
printf("\tBitte geben Sie einen Suchbegriff ein: \n\t(Vorname, Name, Strasse, PLZ oder Ort) \n\n\tSuchwort: ");
scanf("%s", &search); // get word to search
int found[101];
initialise(found, 100);
printf("\n");
addresses = fopen("adressen.txt", "r"); // open file
k = 0;
z = 1;
size = 0;
size = sizeof(data); // get size of data
String = (char *) malloc(size); // reserve
while(fgets(data,fileend,addresses) != NULL){
// add "A,\0" to string -> explode
String[0] = 'A';
String[1] = ',';
String[2] = '\0';
strcat(String,data);
char* pToken = strtok(String, ",");
if (pToken){
a = 1;
while ((pToken = strtok(NULL, ","))){
if(strcmp(pToken, search) == 0){
++k;
found[k] = z;
}//if(strstr(pToken, search)!= NULL
switch(a){
case 1:
strcpy(person[z].forename, pToken);
break;
case 2:
strcpy(person[z].name, pToken);
break;
case 3:
strcpy(person[z].street, pToken);
strcpy(person[z].strno, "000");
break;
case 4:
strcpy(person[z].zipcode, pToken);
break;
case 5:
strcpy(person[z].city, pToken);
break;
default:
//nothing
;
}//switch(a)
a++;
}//END while ((pToken = strtok(NULL, ","))
}//END if (pToken)
z++;
}//END while(fgets(data,fileend,addresses) != NULL)
//##### Print addresses of found values. #####
e = count(found);
if(e <= 0){
printf("\n\tKeine \x9abereinstimmung gefunden.");
}//END if(found == 0)
else{
//printf("Found :%i",count(found));
for(b = 1; b <= e; b++){
printf("\n\n\t%s ", person[found[b]].forename);
printf("%s\n", person[found[b]].name);
printf("\t%s\n", person[found[b]].street);
printf("\t%s ", person[found[b]].zipcode);
printf("%s", person[found[b]].city);
}//END for(z = 1; z <= k; z++)
}//END else(found == 0)
fclose(addresses);
printf("\n\n\t Moechten Sie eine weitere Option durchf\x81hren? j/n\n");
another = getch(); //read userinput -> abort program?
if(another == 74 || another == 106){
main();
}
else{
break;
}
case 52:
printf("Kartei l\x94schen\n");
printf("\n\n\tMoechten Sie eine weitere Option durchf\x81hren? j/n\n");
another = getch(); //read userinput -> abort program?
if(another == 74 || another == 106){
main();
}
break;
case 53:
//system("exit");
default:
//geht nicht, wird durch die schleife abgefangen
;
}//END switch
return 0;
}//END main()
Die Funktionen wie folgt:
Code:
int file_exists(char *dateiname){
FILE *datei_ptr;
int result;
datei_ptr = fopen(dateiname, "r");
if(datei_ptr != NULL){
result = -1;
fclose(datei_ptr);
}
else{
result = 0;
}
return result;
}
long filesize(FILE *addresses){
int end = 0;
if(file_exists("adressen.txt")){
addresses = fopen("adressen.txt", "r");
fseek(addresses, 0L, SEEK_END);
end = ftell(addresses);
rewind(addresses);
//printf("%i",end);
fclose(addresses);
}
return end;
}
int count(int arr[]){
int i = 0, elements = 0;
for(i = 0; i <= 101; i++){
if(arr[i] != 0){
elements++;
}
}// for(i = 0; i <= 101; i++)
return elements;
}
void initialise (int *intvar, int size){
int z ;
for(z = 0; z <= size; z++){
intvar[z] = 0;
}
}
und die Textdatei im Format:
Muster, Mustermann, musterstr. 1,12345,Musterort
Muster2,Mustermann2,musterstr. 2, 67890, Musterort2
usw.
Wäre wirklich sehr dankbar wenn jemand ne Idee hätte.
Rätsel da nun schon seit Tagen dran.
Achso ich benutze Dev-Cpp 4.9.9.2, falls das Relevant ist.
Dank und Gruß
Webrabbit