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

S

spidey12345

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
 
D

Daniel T.

spidey12345 said:
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.
 
A

ashani

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top