Thread: whats wrong wid dis code!!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Unhappy whats wrong wid dis code!!

    Hi all,

    Please help me wid the code below..it gives a segmentation fault on execution..
    compilation is clean..m running it on gcc (linux)..

    its a simple program to copy text from a file and store in a local variable
    cannot figure out wat 2 do

    Code:
    #include<stdio.h>
    #include<string.h>
    #define IN 1
    #define OUT 0
    
    main()
    {
    FILE *fp;
    char *ch = "";
    char *str = "";
    int c, state;
    state = OUT;
    
    printf("HI all");
    fp = fopen("output.txt", "r");
    if(fp == NULL)
    {
        puts("\nCannot open source");
        return(0);
    }
    
    while ((c = fgetc(fp)) != EOF)
    {
        if (c == ' ' || c == '\n' || c == '\t')
        {
            state = OUT;
        }
        else
        {
            state = IN;
            *ch = (char) c;
            strcat(str,ch);
        }
    }
    fclose(fp);
    printf("%s",str);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main()
    should be
    int main(void) - read FAQ

    char *ch = ""; - points to some write protected memory
    char *str = ""; - same here - you cannot write to this destination


    strcat(str,ch); - you need some buffer before writing, you have none

    to add 1 char to buffer - you do not need the strcat, simple assignment will do...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    char *ch = "";
    char *str = "";
    ...
            *ch = (char) c;
            strcat(str,ch);
    You are trying to write something to non-modifiable location (string literals cannot be overwritten) where you don't have any room anyway.

    Try to get some modifiable buffer with sufficient size:
    Code:
    char str[1000] = {0};
    There should also be better ways to append one character at a time to a buffer.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    12
    thanks 4 d help BUT

    I changed it to....using a buffer and then strcat..removed d above str="" and ch=""...
    it still gives d seg fault
    Code:
            char ch[200];
            ch[i] = (char) c;
            strcat(str,ch);
            i++;
    and any way2 append a buffer or even one char at atime 2 a string..m out f ideas

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char str[200]
    size_t len = 0;
    while ((c = fgetc(fp)) != EOF)
    {
        if (c == ' ' || c == '\n' || c == '\t')
        {
            state = OUT;
        }
        else
        {
            state = IN;
            str[len] = c;
            len++;
            str[len] = 0;
            if(len >= sizeof (str) - 1)
               break;
        }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. plz help wid this tic tac toe code
    By sudoku in forum C Programming
    Replies: 2
    Last Post: 10-27-2008, 02:15 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM