having trouble with reading in file word by word with link list

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

  1. spidey12345

    spidey12345 Guest

    what i need this program to do is to read paragraphs like

    "st blah blh test ere
    se sit blha eere w"

    and then it will reformat to

    "st blah blh test ere se sit blha eere w" ....


    i am having trouble try to read it word by word, since the link list
    just go to the end, and never went to the second pointer to be able to
    read the word blah



    #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

    Daniel T. Guest

    "spidey12345" <> wrote:

    > what i need this program to do is to read paragraphs like
    >
    > "st blah blh test ere
    > se sit blha eere w"
    >
    > and then it will reformat to
    >
    > "st blah blh test ere se sit blha eere w" ....
    >
    >
    > i am having trouble try to read it word by word, since the link list
    > just go to the end, and never went to the second pointer to be able to
    > read the word blah


    If the program must be written in C, maybe you can ask in a newsgroup
    devoted to that language.

    If you are allowed to write it in C++, then investigate the fstream,
    list, and string classes. You could probably write it in 10 lines of
    code.

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Oct 23, 2006
    #2
    1. Advertising

  3. spidey12345

    ashani Guest

    Your code to too long...
    If you are using C++,
    try something like:

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <string>

    int main()
    {
    std::ifstream file("test.txt");
    std::copy(std::istream_iterator<std::string>(file),
    std::istream_iterator<std::string>(),
    std::eek:stream_iterator<std::string>(std::cout, " "));
    return 0;
    }
    ashani, Oct 23, 2006
    #3
    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. Priya
    Replies:
    0
    Views:
    329
    Priya
    Nov 25, 2003
  2. Jozef
    Replies:
    0
    Views:
    716
    Jozef
    Nov 15, 2005
  3. Replies:
    7
    Views:
    1,035
    red floyd
    Jul 17, 2005
  4. Jozef
    Replies:
    0
    Views:
    164
    Jozef
    Nov 15, 2005
  5. Christopher Rasch

    Having trouble reading from stderr

    Christopher Rasch, Feb 26, 2008, in forum: Ruby
    Replies:
    4
    Views:
    120
Loading...

Share This Page