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
"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