problem using file pointers

Discussion in 'C' started by niso, Jan 24, 2008.

  1. niso

    niso New Member

    Joined:
    Jan 24, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    i have done a program to reverse the list..and here the users gives the input(i.e.,if i give ./a.out 3 my output will be like this orginal:1 2 3 and reverse:3 2 1 ......)
    but how my program should take the list as input from a file :confused: .

    (note:The name of the input file should be taken as a command line argument. This file would contain a line of white-space separated list of number. eg(19 1 4 3 18)


    can any one help me out :cryin: ......

    Code:
    /**********reversing list***********
    #include<stdio.h>
     
    struct link {
        int key;
        struct link *next;
    };
     
    struct link * create_llist(int max)
    {
        struct link *head;
        struct link *ptr;
        int n = 0 ;
     
        if(max<1)return NULL;
        ptr=(struct link *)malloc(sizeof(struct link));
        head=ptr;
     
        while(n++ < max-1)
        {
            ptr->key=n;
            ptr->next=(struct link *)malloc(sizeof(struct link));
            ptr=ptr->next;
        }
        ptr->key=n;
        ptr->next=NULL;
        return head;
    }
     
    void reverse_llist(struct link *head,struct link **newHead)
    {
        struct link **ptr=&head;
     
        if(*ptr == NULL || (*ptr)->next == NULL) return ;
     
        if((*ptr)->next->next != NULL)
            reverse_llist((*ptr)->next,newHead);
        else
            *newHead = (*ptr)->next ; 
        (*ptr)->next->next = *ptr ;
        (*ptr)->next = NULL;
    }
     
     
    print_list(struct link *head)
    {
        struct link *a=head;
        if(!a) return ;
        while(a->next)
        {
            printf("  %d ", a->key);
            a=a->next;
        }
        printf("  %d ", a->key);
        printf("\n");
    }
     
    main(int argc, char*argv[])
    {
        struct link *head;
        if(!argc) return ;
        head=create_llist(atoi(argv[1]));
        printf(“Original:\t”);
        print_list(head);
        printf(“Reversed:\t”);
        reverse_llist(head,&head);
        print_list(head);
    }
     
    Last edited by a moderator: Jan 24, 2008
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Use fopen() to open the file for input.
    Use fscanf() to read each successive integer from the file. I would suggest using the %d format.

    > ptr=(struct link *)malloc(sizeof(struct link));
    If you had included stdlib.h (as you should have), then a cast of the result would not be necessary, at least for any ANSI/ISO C compiler.
    If, after removing the cast, you get warnings about converting void*, then that means you need to stop compiling your code with a C++ compiler, and use a C compiler. Typically, this involves renaming "prog.cpp" into "prog.c".

    Also, main should be explicitly declared as returning int, and have an explicit return 0; at the end.
     
  3. niso

    niso New Member

    Joined:
    Jan 24, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    i need output like this....


    for:Example:
    If the input file is input.txt, and following are its contents:
    19 1 3 4 18

    After reading input from the file, call to print_list should display:
    19 → 1 → 3 → 4 → 18

    Call to reverse_llist and then calling print_list should print the following:
    18 → 4 → 3 → 1 → 19



    so where i want to modify in my program..........
    help me!!!!!!!!!!!!!!!
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    So when you run your test data through your program, what do you see?

    - does it crash (all the time, some of the time, specific test case)
    - does it produce bad output (all the time, some of the time, specific test case)
    - does it produce the right data in the wrong order
    - does it loop forever / not at all.
    - etc etc

    We need more than "here's my code, it doesn't work". We provide help, not a rescue service.

    Being able to debug your own code is a vital skill, as is being able to clearly communicate the exact nature of the problem.

    For example, the first test would be a list with only 1 element.
    If the test passes, fine, then move onto the next test.
    If not, then you have a really simple case to step through the code with a debugger to find the problem.

    Try it with a 2-element list.
    Try it with a 3-element list. By the time you get to this one, it should be OK for any list.

    So a report like, "it works for 1 element forwards and backwards, 2 elements forwards and then it just crashes at the start of printing 2 elements in reverse" is a meaningful description of a problem and shows us that you've done some work yourself.

    If you don't know how to use a debugger, then now is a good time to get some practice in.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice