Thread: what is wrong with this code

  1. #1
    Unregistered
    Guest

    Exclamation what is wrong with this code

    anyone can help?


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

    #define MAX 80
    typedef char * STRNG;
    char *code[MAX];

    STRNG get_line(FILE * inf)
    {
    STRNG s;
    char tok = getc(inf);
    int count = 0;
    s = malloc(80);
    while(tok!='\n')
    {
    s[count++] = tok;
    tok = getc(inf);
    }
    s[count] = '\0';
    return s;
    }

    int main(int argc, char *argv[])
    {
    char* filename;
    FILE *stream;
    filename =argv[1];
    int n=0;

    stream=fopen(filename,"r");
    if(stream ==NULL)
    { printf("Enter a valid parameter.Program will exit.\n");
    exit(1);
    }

    code[n]=get_line(stream);

    do{
    n++;
    }while(strlen(code[n])>1);
    printf("%d\n",n);


    return 1;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have this:
    > char *code[MAX];

    Which is an array of character pointers.
    And then this...

    > code[n]=get_line(stream);

    Allocate space before reading into a pointer.

    [edit]
    Ah, nevermind. I see what you're doing now. That should work in theory.
    [/edit]

    Quzah.
    Last edited by quzah; 11-20-2001 at 06:12 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    how am i suppose to do that?
    sorry am very new in c
    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've edited my original reply. I see that you're doing your allocation manually. However, I'm wondering why you don't just do something like:

    code[n] = malloc( 80 );
    fgets( code[n], 80, stream );

    Additionally, it is a good idea to actually state what the current output is when you run the program, and the way it's expected to run. It's bad form to expect everyone to compile your random code, or read it all, and pinpoint what is wrong. The general form is something like:

    "Hi, I'm trying to do..."
    "here is my code..."
    "here is what happens..."
    "here is what i want to happen..."

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest

    Post

    i changed my code in main into this:

    int main(int argc, char *argv[])
    {
    FILE *stream;
    stream=fopen(argv[1],"r");
    if(stream ==NULL){
    printf("Enter a valid parameter.Program will exit.\n");
    exit(1);
    }
    else{
    while(!feof(stream)){
    code[n]=get_line(stream);
    printf("%s\n",code[n]);
    i++;

    }
    printf("%d\n",i);/*Here is the problem.I can't make it print out the final value */
    }
    any sugestions?
    return 1;
    }

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Talking

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

    #define MAX 80
    typedef char *STRNG;
    char *code[MAX];

    STRNG get_line(FILE *inf)
    {
    STRNG s;
    char tok = getc(inf);
    char *test = NULL;
    int count = 0;
    s = malloc(80);

    test = &tok;

    printf("Token is: %s\n", test);

    while(tok!='\n')
    {
    printf ("\nCount is: %i %c", count, tok);

    s[count++] = tok;
    printf (" %i\n", count);
    tok = getc(inf);
    }
    s[count] = '\0';
    return s;
    }


    int main(int argc, char *argv[])
    {
    char* filename;
    char *Ptr_String_NULL;
    char Store_NULL = '\0';
    FILE *stream;
    int Index = 0;

    /* You had declaration of int n=0 after */
    /* filename assignment. */

    int n=0;

    filename =argv[1];

    printf ("\nFilename is: \'%s\'\n", filename);
    printf ("argc is: %i\n\n", argc);
    stream=fopen(filename,"r");
    if(stream ==NULL)
    {
    printf("Enter a valid parameter.Program will exit.\n");
    exit(1);
    }

    /* Here is the thorn in your side. You need to always initialize
    * every pointer in your array to NULL ( '\0' ) so that when you
    * call 'strlen' it can determine the end of the string. strlen
    * counts the characters in a string until it hits a '\0'. If
    * it never runs into that "end of the string", it seg faults.
    */

    Ptr_String_NULL = &Store_NULL;

    for (Index = 0; Index < MAX; Index++)
    {
    code[Index] = Ptr_String_NULL;
    }

    /* You need to realize that you never increment n in this case
    * and so the array of character pointers "code" will never have
    * more than one element (char pointer). That pointer will always
    * be for the first line of the file you passed to the function.
    */

    /* another words, you are only assigning code[0] in this case */

    code[n]=get_line(stream);


    do
    {
    n++;
    }while(strlen(code[n])>0);
    printf("%d\n",n);



    printf ("string is: %s\n", code[0]);

    return 1;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's that deja-vu feeling again...

    http://www.cprogramming.com/cboard/s...&threadid=5622

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. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM