help with linklist with I/O file, and word node

Discussion in 'C Programming' started by spidey12345, Oct 23, 2006.

  1. spidey12345

    spidey12345 Guest

    can anybody help me with this

    i have a baddata.txt file that has certain data like this:

    " blah blah ere werew ss a s ef
    df ww erew asf"

    and i want to store each word as a struct node with link list
    which will be like in my head:

    blah->blah->ere->werew->ss->a->s->ef->df->ww->erew->asf

    then prints out to a new file

    "blah blah ere werew ss a s ef df ww erew asf"

    and yes i have to use link list

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

    #define MAX_CHAR = 80 // 80 characters per line
    #define MAX_WORD = 30
    #define ON 1
    #define OFF 0

    // ====================================== user defined type
    ======================================

    struct dirty_data {
    char word[30];
    char line[80];
    };


    struct rec_node // node with the dirty_data type and a pointer
    {
    dirty_data a_rec;

    rec_node *next; // can point to a word node
    };




    // ======================================= Function Prototypes
    ===================================

    void Read_one_word(FILE* infile, dirty_data& a_rec);
    void Build_unordered_linked_list (rec_node* start);
    void write_word_to_file(rec_node* p);
    char *Readline(FILE *inFile) ;


    int main()
    {



    rec_node *start = new rec_node; // new memory at beginning of list


    Build_unordered_linked_list (start);



    write_word_to_file(start);



    return 0;
    }

    void Build_unordered_linked_list (rec_node* start)
    {

    FILE *bFile; /* stream = BadData.dat file pointer */

    dirty_data rec;
    rec_node* p;



    bFile = fopen ("BadData.txt" , "r");

    if(!bFile)
    printf("could not open input file\n");



    else // file opened successfully
    {

    Read_one_word(bFile, rec); // priming read

    if ( feof(bFile))
    printf("this file is empty\n");


    else // data is in a rec
    {


    p = start ; // the helper points to the beginning of the list


    printf("%d ", p);
    printf("%d" , start);


    while (bFile != NULL)
    {


    p -> a_rec = rec ; // move the data into the node




    Read_one_word(bFile, rec);

    if ( !feof(bFile))
    {

    p -> next = new rec_node;

    p = p -> next; // linked to next music node

    } // end of if

    } // end of while

    p ->next = NULL; // end of list

    } // end of else ...data is in rec


    } // end of else

    } // end of function


    void Read_one_word(FILE* inFile, dirty_data& a_rec)
    {
    char c;
    char StoreFlg ;
    int i,j,k ;
    i = 0 ;

    StoreFlg = OFF ;


    while( (c = getc(inFile)) != EOF) {
    //if ( isalpha(c) || isdigit(c) )

    if ( !isspace(c) )
    {
    StoreFlg = ON ;
    a_rec.word[i++] = c ;
    if ( i >= 30 );

    }
    else if ( StoreFlg )
    {
    a_rec.word[i++] = '\0' ;
    }
    }


    printf(a_rec.word);

    }






    void write_word_to_file(rec_node* p)
    {

    FILE * gFile;

    gFile = fopen ("GoodData.txt" , "w");



    // test output file
    if(!gFile)
    printf("Error Opening File..\n");

    else
    {
    // print out all the records, including the ones that were updated
    for(; p != NULL; p = p->next)
    {

    fprintf(gFile, p->a_rec.line);

    } // end of for

    } // end of else

    } // end of function
     
    spidey12345, Oct 23, 2006
    #1
    1. Advertising

  2. "spidey12345" <> writes:

    > can anybody help me with this


    I'll try. I am rarely the first in the scene like this.

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


    OK. Looks like C.
    >
    > #define MAX_CHAR = 80 // 80 characters per line
    > #define MAX_WORD = 30


    Odd. Not illegal to define MAX_CHAR to expand to = 80 but it looks odd.

    > #define ON 1
    > #define OFF 0
    >
    > // ====================================== user defined type
    > ======================================


    // comments bad on usenet for this reason.

    > struct dirty_data {
    > char word[30];
    > char line[80];
    > };


    If you did not have the "=" in the macros above, you could use them here.

    > struct rec_node // node with the dirty_data type and a pointer
    > {
    > dirty_data a_rec;
    >
    > rec_node *next; // can point to a word node
    > };
    >
    > // ======================================= Function Prototypes
    > ===================================
    >
    > void Read_one_word(FILE* infile, dirty_data& a_rec);

    ---------------------------------------------^

    C++ alert. You are in the wrong group. Ask in comp.lang.C++ and you
    might get help (do they do code crits?).

    --
    Ben.
     
    Ben Bacarisse, Oct 23, 2006
    #2
    1. Advertising

  3. spidey12345

    Simon Biber Guest

    spidey12345 wrote:
    > can anybody help me with this
    >
    > i have a baddata.txt file that has certain data like this:
    >
    > " blah blah ere werew ss a s ef
    > df ww erew asf"
    >
    > and i want to store each word as a struct node with link list
    > which will be like in my head:
    >
    > blah->blah->ere->werew->ss->a->s->ef->df->ww->erew->asf
    >
    > then prints out to a new file
    >
    > "blah blah ere werew ss a s ef df ww erew asf"
    >
    > and yes i have to use link list


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

    #define MAX_WORD 80

    struct w {
    char word[MAX_WORD + 1];
    struct w *next;
    };

    struct list {
    struct w *head;
    struct w *tail;
    };

    struct list *new_list(void)
    {
    struct list *new = malloc(sizeof *new);
    if(!new)
    {
    fprintf(stderr, "Out of memory\n");
    return NULL;
    }
    new->head = NULL;
    new->tail = NULL;
    return new;
    }

    void push_back(struct list *list, const char *word)
    {
    struct w *new = malloc(sizeof *new);
    if(!new)
    {
    fprintf(stderr, "Out of memory\n");
    exit(EXIT_FAILURE);
    }
    strcpy(new->word, word);
    new->next = NULL;
    if(list->tail)
    list->tail->next = new;
    else
    list->head = new;
    list->tail = new;
    }

    void print_list(struct list *list)
    {
    struct w *p;
    for(p = list->head; p; p = p->next)
    {
    if(p != list->head) putchar(' ');
    fputs(p->word, stdout);
    }
    putchar('\n');
    }

    void free_list(struct list *list)
    {
    struct w *p, *q;
    for(p = list->head; p; p = q)
    {
    q = p->next;
    free(p);
    }
    free(list);
    }

    int main(void)
    {
    int ch;
    char buf[MAX_WORD + 1];
    struct list *list = new_list();
    FILE *fp = fopen("baddata.txt", "r");
    if(!fp || !list)
    {
    fprintf(stderr, "Error\n");
    exit(EXIT_FAILURE);
    }
    do
    {
    size_t i;
    for(i = 0; (ch = getc(fp)) != EOF && !isspace(ch)
    && i < MAX_WORD; i++)
    {
    buf = ch;
    }
    if(i == MAX_WORD)
    {
    fprintf(stderr, "Warning: long word will be split\n");
    ungetc(ch, fp); /* push the last, excess char back onto stream */
    }
    if(i != 0) /* if a word was read */
    {
    buf = 0; /* zero-terminate the buffer */
    push_back(list, buf);
    }
    if(isspace(ch)) /* if we must skip some white space */
    {
    while((ch = getc(fp)) != EOF && isspace(ch)) /* empty */ ;
    ungetc(ch, fp); /* push the last, non-whitespace
    char back onto stream */
    }
    } while(ch != EOF);
    fclose(fp);
    print_list(list);
    free_list(list);
    return 0;
    }
     
    Simon Biber, Oct 23, 2006
    #3
  4. spidey12345

    spidey12345 Guest

    Simmon, just wondering, i dont think it complies in gcc copmlier

    becuase of double varible declartion, any idea?
     
    spidey12345, Oct 23, 2006
    #4
  5. "spidey12345" <> writes:
    > Simmon, just wondering, i dont think it complies in gcc copmlier
    >
    > becuase of double varible declartion, any idea?


    Please provide context when posting a followup. Don't assume everyone
    can see the article to which you're replying.

    What do you mean you don't *think* it compiles in gcc? Does it
    compile, or doesn't it?

    I just tried compiling it with gcc; there were no warnings or error
    messages. What errors did you get?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 23, 2006
    #5
  6. spidey12345

    Simon Biber Guest

    spidey12345 wrote:
    > Simmon, just wondering, i dont think it complies in gcc copmlier


    It does for me. I used this command:

    gcc -ansi -pedantic -Wall -W -O2 baddata.c

    Make sure you name your C file with .c

    If you use the .cpp extension, it will compile as C++, and my code is
    not compatible with C++.


    > becuase of double varible declartion, any idea?


    What double variable declaration?

    --
    Simon.
     
    Simon Biber, Oct 24, 2006
    #6
  7. spidey12345

    spidey12345 Guest

    yeah, sorry, i got it to work..

    but whats wrong if the user had something like blah , either

    with commer in the middle, also if i want to start a new paragraph, how
    can it reformat it so just one \n, so that i can implment another
    paragraph.



    i tried to put inside printlist statement

    if (strcmp(char*)p->next, ","== 0)
    {

    }
    else

    {

    put(" ");
    }

    that will give me segment falt, i know that if p->next is either null
    or endline, will screw it up, but i have no idea acutally how to
    implment it
     
    spidey12345, Oct 28, 2006
    #7
  8. spidey12345

    Bill Pursell Guest

    spidey12345 wrote:
    > yeah, sorry, i got it to work..


    Please quote context.

    >
    > if (strcmp(char*)p->next, ","== 0)
    > {
    >
    > }
    > else
    >
    > {
    >
    > put(" ");
    > }
    >
    > that will give me segment falt, i know that if p->next is either null
    > or endline, will screw it up, but i have no idea acutally how to
    > implment it


    No, it doesn't get far enough to generate a segfault, because it
    doesn't even come close to compiling. Also, your bracing
    style is ... less than optimal.

    In summary: post context, post code which compiles cleanly, and
    use a reasonable bracing style, or people will not pay any attention
    to your posts.

    --
    Bill Pursell
     
    Bill Pursell, Oct 28, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,667
  2. jova

    array to linklist

    jova, Feb 8, 2004, in forum: Java
    Replies:
    5
    Views:
    4,782
    Harry Long
    Dec 16, 2010
  3. tone
    Replies:
    4
    Views:
    471
    Hywel Jenkins
    Nov 19, 2003
  4. yudi1226

    re:linklist

    yudi1226, Feb 13, 2004, in forum: C Programming
    Replies:
    1
    Views:
    406
    Martin Ambuhl
    Feb 13, 2004
  5. Tjerk Wolterink
    Replies:
    2
    Views:
    1,519
    Dimitre Novatchev
    Aug 24, 2006
Loading...

Share This Page