Zum Inhalt springen

Empfohlene Beiträge

Geschrieben

Hallo,

ich lerne in der Schule C und im moment schreiben wir ein kleines Programm zur Adressverwaltung.

Damit ich während der Entwicklung, zum Testen, nicht immer neue Daten eingeben muss will ich Testdaten anlegen.

Ich habe ein Array von Strukturen, die wiederrum Integer und Char-Arrays enthalten.

Wie bekomm ich da Daten rein?

Ich habe bisher:


strncpy(a[0].name, "Meier", 5);

strncpy(a[0].firstname, "Tom", 3);

strncpy(a[0].street, "Hauptstrasse 1", 14);

strncpy(a[0].location, "Hauptstadt", 10);

a[0].zip = 11111;

a[0].phone = 00000111111;

a[0].dateOfBirth.day = 1;

a[0].dateOfBirth.month = 1;

a[0].dateOfBirth.year = 1111;

a[0].available = TRUE;

Aber das funktioniert nicht..

Ich bin für jede Hilfe dankbar!

Gruß

Julian

Geschrieben
Das Problem ist, es kommt auch keine Fehlermeldung. Es passiert einfach nix...
Der Code tut ja auch nichts beobachtbares. Was sollte denn deiner Meinung nach passieren?

Wie benutzt man denn strncpy richtig?
Zunächst sollte man sich vergegenwärtigen, dass strncpy nicht die "sichere" Version von strcpy ist. strncpy dient dazu, Ziel-Arrays mit fester Länge mit Nullbytes aufzufüllen. Falsch benutzt, hat strncpy dieselben Sicherheitsprobleme wie strcpy, und bringt sogar noch zusätzliche Risiken mit.

Der zusätzliche Parameter von strncpy soll angeben, wie viel Platz im Ziel-Array ist. Wenn man da, wie du, einfach nur angibt, wie lang der Quellstring ist, hat man nichts gewonnen.

Zudem hat strncpy die Eigenschaft, dass es keine Nullterminierung anhängt, wenn nicht genug Platz ist. Der Programmierer muss also genau wie bei strcpy sicherstellen, dass im Zielarray genug Platz ist. Der Unterschied ist nur, dass bei unzureichendem Platz bei strcpy sofort beim Schreiben undefiniertes Verhalten auftritt, und bei strncpy erst beim nächsten Lesen.

Ich sehe in deinem Code keinen Grund, überhaupt strncpy zu benutzen.

Geschrieben
Der Code tut ja auch nichts beobachtbares. Was sollte denn deiner Meinung nach passieren?

Ja, das stimmt und ist mir auch bewusst. Ich habe eine Methode, mit der ich die Werte als Benutzereingabe einlese und eine, die sie wieder ausgibt. Das klappt auch.

Also müsste doch, wenn ich den Code aufrufe und dann die Ausgabe-Methode was erscheinen, aber da kommt nix.

Zu strncpy und strcpy:

Ich sehe schon, da fehlt mir noch eine ganze Menge wissen.. :upps Ich werde mir dass in Ruhe anschauen und wenn ich dann noch Frage habe, melde ich mich nochmal.

Ich danke dir für deine Hilfe und Hinweise. Jetzt weis ich wenigstens, was ich mir anschauen muss. Danke :)

Geschrieben

Hier einfach mal mein kompletter Code:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define BOOL int

#define TRUE 1

#define FALSE 0


#define N 10



struct dateOfBirth_type {

    int day;

    int month;

    int year;

};


struct adress_type {

    char name[30];

    char firstname[30];

    char street[100];

    int zip;

    char location[100];

    int phone;

    struct dateOfBirth_type dateOfBirth;

    BOOL available;

};



int menu() {

    int selection;


    printf("%+40s\n\n", "Adressen");


    do {

        system("cls");

        printf("%+45s", "+****************+\n");

        printf("%+45s", "+                +\n");

        printf("%+45s", "+   1: Eingabe   +\n");

        printf("%+45s", "+   2: Ausgabe   +\n");

        printf("%+45s", "+                +\n");

        printf("%+45s", "+   0: Exit      +\n");

        printf("%+45s", "+                +\n");

        printf("%+45s", "+****************+\n");

        printf("%+30s", ">>> ");

        scanf("%d", &selection);

        getchar();


    } while(selection < 0 || selection > 2);


    return selection;

}


void input(struct adress_type a[]) {


    int i;

    for(i = 0; i < N; i++) {

        if(a[i].available == FALSE || (a[i].available != FALSE && a[i].available != TRUE)) {

            printf("Name: ");

            gets(a[i].name);

            printf("Vorname: ");

            gets(a[i].firstname);

            printf("Strasse: ");

            gets(a[i].street);

            printf("Plz: ");

            scanf("%d", &a[i].zip);

            getchar();

            printf("Ort: ");

            gets(a[i].location);

            printf("Telefon: ");

            scanf("%d", &a[i].phone);

            getchar();

            printf("Geburtsdatum (Tag): ");

            scanf("%d", &a[i].dateOfBirth.day);

            getchar();

            printf("Geburtsdatum (Monat): ");

            scanf("%d", &a[i].dateOfBirth.month);

            getchar();

            printf("Geburtsdatum (Jahr): ");

            scanf("%d", &a[i].dateOfBirth.year);

            getchar();


            a[i].available = TRUE;


            break;

        }

    }

}


void output(struct adress_type a[]) {

    int i;


    system("cls");


    printf(" %s %+15s %+10s\n", "Vorname", "Nachname", "Ort");

    printf("%s", "----------------------------------\n");

    for(i = 0; i < N; i++) {

        if(a[i].available == TRUE) {

            printf(" %s %+7s %s %+8s %s\n", a[i].firstname, "", a[i].name, "", a[i].location);

        }

    }

    getchar();

}


void createTestData(struct adress_type a[]) {


    strncpy(a[0].name, "Meier", 5);

    strncpy(a[0].firstname, "Tom", 3);

    strncpy(a[0].street, "Hauptstrasse 1", 14);

    strncpy(a[0].location, "Hauptstadt", 10);

    a[0].zip = 11111;

    a[0].phone = 00000111111;

    a[0].dateOfBirth.day = 1;

    a[0].dateOfBirth.month = 1;

    a[0].dateOfBirth.year = 1111;

    a[0].available = TRUE;


    /*strncpy(a[1].name, "Schmidt", 7);

    strncpy(a[1].firstname, "Ben", 3);

    strncpy(a[1].street, "Banhofstr. 5", 12);

    strncpy(a[1].location, "Hauptstadt", 10);

    a[1].zip = 11111;

    a[1].phone = 00000125456;

    a[1].dateOfBirth.day = 12;

    a[1].dateOfBirth.month = 12;

    a[1].dateOfBirth.year = 1212;

    a[1].available = TRUE;


    strncpy(a[2].name, "Becker", 6);

    strncpy(a[2].firstname, "Leon", 4);

    strncpy(a[2].street, "Wegweg 99", 9);

    strncpy(a[2].location, "Nichthauptstadt", 15);

    a[2].zip = 99999;

    a[2].phone = 125484545487878;

    a[2].dateOfBirth.day = 9;

    a[2].dateOfBirth.month = 4;

    a[2].dateOfBirth.year = 2201;

    a[2].available = TRUE;*/

}


int main() {

    int selection = -1;

    struct adress_type adresses[N];


    while(selection != 0) {

        selection = menu();


        switch(selection) {

            case 1:

                input(adresses);

                break;

            case 2:

                output(adresses);

                break;

            case 9:

                createTestData(adresses);

                break;

        }

    }


    return 0;

}

Geschrieben

Ich habe mal alles überarbeitet und es sieht jetzt so aus:


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define BOOL int

#define TRUE 1

#define FALSE 0


#define N 10



struct dateOfBirth_type {

    int day;

    int month;

    int year;

};


struct adress_type {

    char name[30];

    char firstname[30];

    char street[100];

    int zip;

    char location[100];

    int phone;

    struct dateOfBirth_type dateOfBirth;

    BOOL available;

};



int menu() {

    int selection;


    printf("%+40s\n\n", "Adressen");


    do {

        system("cls");

        printf("%+45s", "+******************+\n");

        printf("%+45s", "+                  +\n");

        printf("%+45s", "+   1: Eingabe     +\n");

        printf("%+45s", "+   2: Ausgabe     +\n");

        printf("%+45s", "+   3: Testdaten   +\n");

        printf("%+45s", "+                  +\n");

        printf("%+45s", "+   0: Exit        +\n");

        printf("%+45s", "+                  +\n");

        printf("%+45s", "+******************+\n");

        printf("%+30s", ">>> ");

        scanf("%d", &selection);

        getchar();


    } while(selection < 0 || selection > 3);


    return selection;

}


void input(struct adress_type a[]) {


    int i;

    for(i = 0; i < N; i++) {

        if(a[i].available == FALSE || (a[i].available != FALSE && a[i].available != TRUE)) {

            printf("Name: ");

            gets(a[i].name);

            printf("Vorname: ");

            gets(a[i].firstname);

            printf("Strasse: ");

            gets(a[i].street);

            printf("Plz: ");

            scanf("%d", &a[i].zip);

            getchar();

            printf("Ort: ");

            gets(a[i].location);

            printf("Telefon: ");

            scanf("%d", &a[i].phone);

            getchar();

            printf("Geburtsdatum (Tag): ");

            scanf("%d", &a[i].dateOfBirth.day);

            getchar();

            printf("Geburtsdatum (Monat): ");

            scanf("%d", &a[i].dateOfBirth.month);

            getchar();

            printf("Geburtsdatum (Jahr): ");

            scanf("%d", &a[i].dateOfBirth.year);

            getchar();


            a[i].available = TRUE;


            break;

        }

    }

}


void output(struct adress_type a[]) {

    int i;

    int j;


    system("cls");


    printf(" Vorname");

    for(j = 0; j < 10; j++) {

        printf(" ");

    }

    printf("Nachname");

    for(j = 0; j < 10; j++) {

        printf(" ");

    }

    printf("Ort\n");


    for(j = 0; j < 50; j++) {

        printf("-");

    }

    printf("\n");


    for(i = 0; i < N; i++) {

        if(a[i].available == TRUE) {

            printf(" %s", a[i].firstname);

            for(j = 0; j < (17 - strlen(a[i].firstname)); j++) {

                printf(" ");

            }

            printf("%s", a[i].name);

            for(j = 0; j < (18 - strlen(a[i].name)); j++) {

                printf(" ");

            }

            printf("%s\n", a[i].location);

        }

    }

    getchar();

}


void createTestData(struct adress_type a[]) {


    strcpy(a[0].name, "Meier");

    strcpy(a[0].firstname, "Tom");

    strcpy(a[0].street, "Hauptstrasse 1");

    strcpy(a[0].location, "Hauptstadt");

    a[0].zip = 11111;

    a[0].phone = 00000111111;

    a[0].dateOfBirth.day = 1;

    a[0].dateOfBirth.month = 1;

    a[0].dateOfBirth.year = 1111;

    a[0].available = TRUE;


    strcpy(a[1].name, "Schmidt");

    strcpy(a[1].firstname, "Ben");

    strcpy(a[1].street, "Banhofstr. 5");

    strcpy(a[1].location, "Hauptstadt");

    a[1].zip = 11111;

    a[1].phone = 00000125456;

    a[1].dateOfBirth.day = 12;

    a[1].dateOfBirth.month = 12;

    a[1].dateOfBirth.year = 1212;

    a[1].available = TRUE;


    strcpy(a[2].name, "Becker");

    strcpy(a[2].firstname, "Leon");

    strcpy(a[2].street, "Wegweg 99");

    strcpy(a[2].location, "Nichthauptstadt");

    a[2].zip = 99999;

    a[2].phone = 125484545487878;

    a[2].dateOfBirth.day = 9;

    a[2].dateOfBirth.month = 4;

    a[2].dateOfBirth.year = 2201;

    a[2].available = TRUE;

}


int main() {

    int selection = -1;

    struct adress_type adresses[N];


    while(selection != 0) {

        selection = menu();


        switch(selection) {

            case 1:

                input(adresses);

                break;

            case 2:

                output(adresses);

                break;

            case 3:

                createTestData(adresses);

                break;

        }

    }


    return 0;

}

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...