Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem using file pointers (http://www.go4expert.com/forums/using-file-pointers-t8445/)

niso 24Jan2008 16:31

problem using file pointers

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: ......


/**********reversing list***********
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));
    while(n++ < max-1)
        ptr->next=(struct link *)malloc(sizeof(struct link));
    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)
        *newHead = (*ptr)->next ;
    (*ptr)->next->next = *ptr ;
    (*ptr)->next = NULL;
print_list(struct link *head)
    struct link *a=head;
    if(!a) return ;
        printf("  %d ", a->key);
    printf("  %d ", a->key);
main(int argc, char*argv[])
    struct link *head;
    if(!argc) return ;

Salem 24Jan2008 22:01

Re: problem using file pointers
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.

niso 28Jan2008 12:40

Re: problem using file pointers

i need output like this....

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!!!!!!!!!!!!!!!

Salem 28Jan2008 17:03

Re: problem using file pointers
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.

All times are GMT +5.5. The time now is 22:51.