S
spidey12345
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
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