By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Oct 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"spidey12345" <sw********@hotmail.comwrote:
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.
Oct 23 '06 #2

P: n/a
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::ostream_iterator<std::string>(std::cout, " "));
return 0;
}

Oct 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.