0

The code below won't compile from terminal window. Could you please help me understand why? This is the message I get when I try to compile:

/usr/bin/ld: /tmp/cclbzcmr.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): 
undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

The idea is simple, given the arguments from the command line, program should create files with the same names as those arguments and write the text the user enters in each of these files.


#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    int i;
    char text[1000];
    FILE *p;
    for(i=1;i<argc;i++){
            char address[100]="/home/nikte/Documents/C++/MaraWorkspace/Zadatak1/";
            printf("Enter your text:\n");
            scanf("%s",text);
            strcat(address,argv[i]);
            p=fopen(address,"w");
            if(p==NULL) printf("It is not possible to create your file %s\n",argv[i]);
            else  fprintf(p,"%s",text);
            fclose(p);
    }
    return 0;
}

Thanks in advance!

rpoleski
  • 988
  • 5
  • 12
Marina
  • 1
  • 2
  • 4
    1. Pick your language (C or C++). 2. Use the right file extension 3. Use the right compiler (gcc or g++ respectively) – Mat May 25 '20 at 19:27
  • 1
    Does this answer your question? [Undefined Symbol \_\_\_gxx\_personality\_v0 on link](https://stackoverflow.com/questions/203548/undefined-symbol-gxx-personality-v0-on-link) – Thomas Sablik May 25 '20 at 19:33
  • Do you have write-access to that location? – Hack06 May 25 '20 at 19:36

1 Answers1

0

Your program is written using a lot of C functions, but we can write it a lot more cleanly in C++:

#include <string>
#include <iostream>
#include <fstream>

int main(int argc, char** argv) {
    std::string text;

    for(int i = 1; i < argc; i++) {
        std::cout << "Enter your text: " << std::flush;
        // Read a line from standard input
        std::getline(std::cin, text);

        // Make the file
        std::ofstream file(argv[i]);
        // Write the text
        file << text << '\n';
        // File gets closed automatically 
    }
}

If it's in a file called main.cpp, you can compile it as g++ main.cpp -o my_program and then run it with ./my_program file1.txt file2.txt, and it'll create the files in whatever directory you're currently working in!

Alecto Irene Perez
  • 10,321
  • 23
  • 46
  • You don't need that `std::flush` after the prompt. By default, operations on `std::cout` automatically flush `std::cin`. – Pete Becker May 26 '20 at 14:21