simple fopen question

This is a discussion on simple fopen question within the C Programming forums, part of the General Programming Boards category; After completing a simple operation with fopen basically getting a file that is passed as an argument through command prompt ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    simple fopen question

    After completing a simple operation with fopen basically getting a file that is passed as an argument through command prompt and printing the whole thing. How exactly do i selectively get stuff from the text file? For example, if i had the following in a text file:

    Employee Total Earnings
    ------------|-------------|
    John Doe 40290.0
    Sarah Lee 33900.0

    How exactly do i buffer the total earning rows to a linked list and then add them?

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Read each line with fgets, parse it using a method of your choice, possibly sscanf, add the data to your linked list.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Ok i'm kind of confused. I want to load the file into a linked list, thanks for providing some hints. How exactly do i differentiate the customerName and totalEarning? I'm assuming through a struct, i.e. one variable for customer name, total value?

    Also, i'm having trouble with the following example i found:
    Just to know that there are 3 values in the list, i want to print hello world! 3 times or the actual value stored in the link list.


    Code:
    #include <stdio.h>
    int main()
    {
    /*
    Build the list {1, 2, 3} in the heap and store
    its head pointer in a local stack variable.
    Returns the head pointer to the caller.
    */
    struct node {
    int data;
    struct node* next;
    };
    
    struct node* BuildOneTwoThree() {
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));
    head->data = 1; // setup first node
    head->next = second; // note: pointer assignment rule
    second->data = 2; // setup second node
    second->next = third;
    third->data = 3; // setup third link
    third->next = NULL;
    // At this point, the linked list referenced by "head"
    // matches the list in the drawing.
    struct node* current = head;
    while (current != NULL) {
    // do something with *current node
    printf("Hello World");
    current = current->next;
    }
    return head;
    }
    /*
    Given a linked list head pointer, compute
    and return the number of nodes in the list.
    */

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Please indent your code . . . it makes it much easier to read. If you had, you may have noticed that one of your functions is inside another one:
    Code:
    int main() {
        struct node {
            int data;
            struct node* next;
        };
    
        struct node* BuildOneTwoThree() {
    Oops.

    And you should free() your data when you're done with it.

    This is C++/C99 style variable declaration:
    Code:
    struct node* BuildOneTwoThree() {
        struct node* head = NULL;
        struct node* second = NULL;
        struct node* third = NULL;
        head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
        second = malloc(sizeof(struct node));
        third = malloc(sizeof(struct node));
        /* ... */
        struct node* current = head;
    Not to mention the single line comments.

    You'll probably also want a function prototype for BuildOneTwoThree() if you put it after main().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You know you don't need that one, two, three stuff; you can use a for loop.

    Code:
    while (current != NULL) {
        // do something with *current node
        // like print it out?
        printf("Data: %d\n", current->data);
        
        printf("Hello World");
        current = current->next;
    }
    
    // Don't forget to free the data . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21