Hi,

i have a problem with getline function in cpp program on os x

Error:
main.cpp:158: error: 'getline' was not declared in this scope

i read that in os x are some issues with getline() function but i am newbie and i dont know what to do.
is possible to change compiler or...when i run program on ubuntu it runs ok but on mac i receive this error.

Thanks for help.

here is the code:

do {
                printf("Napis cestu:");
            } while ((readd = getline(&posielam, &len, stdin)) == -1);

            if (readd > 0)
                posielam[readd - 1] = '\0';

Are you using a C compiler or C++. Your error message indicates a .cpp file?

oh sorry,i did post this in c thread....i have a cpp file ....i tried both..cpp and c compiler but not progress.

#include <signal.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#define _GNU_SOURCE 
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <list>

//*********************************************************************************************************

void Help(const char* program_name) {

    printf("\n\t*************HELP*************\n");
    printf("(C) Juraj Stefanicka\n");
    printf("\nTento program sluzi na rekurzivne prehladavanie adresarovej strukturi zadanej argumentom programu.\n");
    printf("\nSpustanie programu: \n\t%s [-c | --count] [<pociatocny_priecinok>]\n\n", program_name);
    printf("Ak nezadate ziadny prepinac program vypise vsetky regularne subory nachadzajuce sa v adresari a v jeho podaresaroch.\n");
    printf("-c alebo --count: program vypise pocet vsetkych regularnych suborov nachadzajucich sa v adresari a v jeho podaresaroch.\n\n\n");
}

//*********************************************************************************************************

char *path_cat(const char *str1, char *str2) {
    size_t str1_len = strlen(str1);
    size_t str2_len = strlen(str2);
    char *result;

    result = (char *) calloc((str1_len + str2_len + 2), sizeof (char));

    strcpy(result, str1);
    result[str1_len] = '/';
    strcat(result, str2);
    return result;
}

//*********************************************************************************************************

int hladaj(char *cesta, bool c) {

    int pocetReg = 0;
    char *cesta2;
    DIR *dir = opendir(cesta);

    if (dir == 0) { // ak sa nepodari otvorit priec.
        fprintf(stderr,"\nNepodarilo sa otvorit cestu %s\nSkontrolujte ci ste ju zadali v spravnom tvare(napr. /home)\n", cesta);
        return 0;
    }

    struct dirent *d; //defines a file system independent directory entry

    //kym nieco nacita
    while ((d = readdir(dir)) != 0) {
        cesta2 = path_cat(cesta, d->d_name);
        //porovnava ci je to adresar, ale nie aktualny ani nadradeny
        if (d->d_type == DT_DIR && strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) {

            if (access(cesta2, R_OK) != 0) {    // ak nemame prava na citanie priecinka
                fprintf(stderr, "Nemame prava na citanie priecinka %s\n", cesta2);
                return 0;
            }//a znova vola funkciu hladaj uz s novou cestou
            else {
                pocetReg = pocetReg + hladaj(cesta2, c);
                //uvolni premennu cesta2
                free(cesta2);
            }
        } else if (d->d_type == DT_REG) {
            if (!c)
                printf("%s\n", cesta2);
            else
                pocetReg++;
        }
    }
    //zatvori adresar
    closedir(dir);
    return pocetReg;
}

//*********************************************************************************************************

char* decko() {
    int rura[2];
    char* cesta [PATH_MAX + 1];

    if (pipe(rura) == -1) {
        perror("Error: 'pipe'");
        exit(EXIT_FAILURE);
    }

    int childPid = fork();

    switch (childPid) {
        case -1:
            perror("Error: 'fork'");
            exit(EXIT_FAILURE);
        case 0: //dieta
            close(rura[0]);

            static char* posielam = NULL;
            size_t len; // dve povinne premenne pre getline
            size_t readd;

            do {
                printf("Napis cestu:");
            } while ((readd = getline(&posielam, &len, stdin)) == -1);

            if (readd > 0)
                posielam[readd - 1] = '\0';

            write(rura[1], posielam, readd * sizeof (char));

            close(rura[1]);
            delete [] posielam;
            exit(EXIT_SUCCESS);
            break;
        default: //rodic
            close(rura[1]);

            int child_stat;
            wait(&child_stat);

            if (child_stat == EXIT_SUCCESS) {
                read(rura[0], cesta, sizeof (cesta));
                //printf("\nprijal som %s\n", cesta);
                wait(NULL); //to iste ako waitpid(-1,NULL,0);
                close(rura[0]);
                return (char*) cesta;
            } else {
                wait(NULL); //to iste ako waitpid(-1,NULL,0);
                close(rura[0]);
                return NULL;
            }
    }
}

//*********************************************************************************************************

int main(int argc, char *argv[]) {

    const char* program_name = argv[0]; // nazov programu
    bool boloC = false;

    const char* short_options = "hc"; //dvojbodka nema byt
    const struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"count", 0, NULL, 'c'}, // takto - same nuly
        {NULL, 0, NULL, 0}
    };

    if (argc == 1) { //bez ziadnych prepinacov a aj bez zadaneho priec. zavolaj detsky proces na ziskanie poc_priec

        hladaj(decko(), boloC);

    } else {

        int option_name = -1;

        do {
            option_name = getopt_long(argc,
                    argv,
                    short_options,
                    long_options,
                    NULL);

            if (argc > 3) {
                printf("\n\nPRILIS VELA ARGUMENTOV\n");
                Help(program_name);
                exit(EXIT_FAILURE);
            }
            else {

                switch (option_name) {

                    case 'h':
                        Help(program_name);
                        exit(EXIT_SUCCESS);
                    case 'c':
                    {
                        boloC = true;
                        if (optind == argc) //ak za prepinacom uz nic nenasleduje
                            printf("\nPocet nejdenych regularnych suborov je %d\n", hladaj(decko(), boloC));

                        else
                            printf("\nPocet nejdenych regularnych suborov je %d\n", hladaj(argv[optind], boloC));

                        break;
                    }
                    case '?':
                        Help(program_name);
                        printf("\n\n\n?");
                        exit(EXIT_FAILURE);
                    case -1:
                        break;
                    default:
                        printf("%argument: 's'\n", option_name);
                        Help(program_name);
                        exit(EXIT_FAILURE);
                }
            }

        } while (option_name != -1);

        if (((argc - optind) == 1) && (!boloC)) { // bez prepinaca len s poc_priec
            hladaj(argv[optind], boloC);
        }
    }

    return (EXIT_SUCCESS);
}

oh sorry,i did post this in c thread....i have a cpp file ....i tried both..cpp and c compiler but not progress.

You have a C++ library in your includes '#include <list>' so you have to use the C++ compiler.

You have this on line 104 - 110

static char* posielam = NULL;
/*...*/
do {
printf("Napis cestu:");
} while ((readd = getline(&posielam, &len, stdin)) == -1);

Your reading len characters(it should be len and not &len) into a c-string that points to NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.