Thread: segmentation fault

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    16

    segmentation fault

    I am trying to read the contents of a file into a char array and then print that array and so far it actually works but it not only prints the array but also "Segmentation fault. (core dumped)". Here is the code I am using.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[]){
        if (argc != 3){
            printf("Usage: %s <input filename> <file to write to>\n", argv[0]);
        }
        else {
            FILE *ifile = fopen(argv[1], "r");
            FILE *ofile = fopen(argv[2], "w");
            if (!ifile){
                printf("Error: Could not open input file \n");
            }
            else {
                if(!ofile){
                    printf("Error: Could not write to file %s", argv[2]);
                }
                else {
                    int length = 0;
                    int i = 0;
                    while(fgetc    (ifile) != EOF){
                        length++;
                    }
                    char input[] = "";
                    rewind(ifile);
                    for(i = 0; i < length; i++){
                        input[i] = fgetc(ifile);
                    }
                    printf("%s",input);
                }
            }
        }
        return 0;
    }
    I am guessing it does not work because I didn't initialize the length of the input[] array correctly, but how would I go about doing that?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have have
    Code:
    char input[] = "";
    so automatically, the length is computed by the compiler to preserve enough space to fit what is assigned to it, thus ""
    What you need is
    Code:
    char input[length] = "";
    If would suggest you to make sure that length is a positive number. You could do that after line 22.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're right, your input array lacks a length, and lacks the code to place a \0 at the end of it.

    If your compiler supports variable length arrays, you can do this
    char input[length+1];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    ok so i tried
    Code:
    char input[length] = "";
    but i am getting a compiler error:
    Code:
    :23:26: error: variable-sized object ‘input’ may not be initialized

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Salem explained the problem, but you posted the same time so I guess you didn't notice it

    You have to dynamically allocate your memory, or set from start a constant number that is logical for your operation ( not too small, not too big )
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    i also tried length+1 but that doesnt work either
    so do i have to allocate memory? or is there a way to enable variable lengths (i am using gcc)

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    length + 1 was meant for the terminating null. You have to remember this for your dynamic allocation too!

    I provided you two options... Not one

    If you interesting read here for the gcc
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    what option would i have to use for gcc though? i read it but it says nothing about variable size

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Maybe this is better.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    nope, still doesn't work. i now tried every version of c (c99/c90/c89/etc) and it doesnt work with any of them it is still giving me the error
    Code:
    main.c: In function ‘int main(int, char**)’:main.c:23:30: error: variable-sized object ‘input’ may not be initialized
    when using
    Code:
    char input[length + 1] = "";
    and when i am using
    Code:
    char input[length + 1];
    it gives me this:
    Code:
    /tmp/ccEoODxU.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status
    i also tried using the
    Code:
    -pedantic
    flag but that changed nothing
    what am i doing wrong

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Go to post number 5.

    (If i were you I would do that. It is not such a big deal )
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > __gxx_personality_v0
    What is your command line for compiling?
    Because this suggests you're using g++ to compile your C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    but how would i dynamically allocate memory? sorry, i am kind of new to all this

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    /tmp/ccEoODxU.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status
    Are you compiling this using g++ instead of gcc? I think that's the cause of this error.

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    16
    no, i am using gcc with
    Code:
    gcc -Wall main.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Getting Segmentation fault
    By godofdoom in forum C Programming
    Replies: 6
    Last Post: 12-21-2010, 02:09 PM
  2. Segmentation fault
    By browser in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 03:13 PM
  3. Segmentation Fault
    By Dansas in forum C Programming
    Replies: 3
    Last Post: 11-21-2008, 02:12 PM
  4. segmentation fault
    By pshirishreddy in forum C Programming
    Replies: 3
    Last Post: 11-08-2008, 02:46 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread