help with linklist with I/O file, and word node

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
 
B

Ben Bacarisse

spidey12345 said:
can anybody help me with this

I'll try. I am rarely the first in the scene like this.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

OK. Looks like C.
#define MAX_CHAR = 80 // 80 characters per line
#define MAX_WORD = 30

Odd. Not illegal to define MAX_CHAR to expand to = 80 but it looks odd.
#define ON 1
#define OFF 0

// ====================================== user defined type
======================================

// comments bad on usenet for this reason.
struct dirty_data {
char word[30];
char line[80];
};

If you did not have the "=" in the macros above, you could use them here.
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);
---------------------------------------------^

C++ alert. You are in the wrong group. Ask in comp.lang.C++ and you
might get help (do they do code crits?).
 
S

Simon Biber

spidey12345 said:
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_WORD 80

struct w {
char word[MAX_WORD + 1];
struct w *next;
};

struct list {
struct w *head;
struct w *tail;
};

struct list *new_list(void)
{
struct list *new = malloc(sizeof *new);
if(!new)
{
fprintf(stderr, "Out of memory\n");
return NULL;
}
new->head = NULL;
new->tail = NULL;
return new;
}

void push_back(struct list *list, const char *word)
{
struct w *new = malloc(sizeof *new);
if(!new)
{
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(new->word, word);
new->next = NULL;
if(list->tail)
list->tail->next = new;
else
list->head = new;
list->tail = new;
}

void print_list(struct list *list)
{
struct w *p;
for(p = list->head; p; p = p->next)
{
if(p != list->head) putchar(' ');
fputs(p->word, stdout);
}
putchar('\n');
}

void free_list(struct list *list)
{
struct w *p, *q;
for(p = list->head; p; p = q)
{
q = p->next;
free(p);
}
free(list);
}

int main(void)
{
int ch;
char buf[MAX_WORD + 1];
struct list *list = new_list();
FILE *fp = fopen("baddata.txt", "r");
if(!fp || !list)
{
fprintf(stderr, "Error\n");
exit(EXIT_FAILURE);
}
do
{
size_t i;
for(i = 0; (ch = getc(fp)) != EOF && !isspace(ch)
&& i < MAX_WORD; i++)
{
buf = ch;
}
if(i == MAX_WORD)
{
fprintf(stderr, "Warning: long word will be split\n");
ungetc(ch, fp); /* push the last, excess char back onto stream */
}
if(i != 0) /* if a word was read */
{
buf = 0; /* zero-terminate the buffer */
push_back(list, buf);
}
if(isspace(ch)) /* if we must skip some white space */
{
while((ch = getc(fp)) != EOF && isspace(ch)) /* empty */ ;
ungetc(ch, fp); /* push the last, non-whitespace
char back onto stream */
}
} while(ch != EOF);
fclose(fp);
print_list(list);
free_list(list);
return 0;
}
 
S

spidey12345

Simmon, just wondering, i dont think it complies in gcc copmlier

becuase of double varible declartion, any idea?
 
K

Keith Thompson

spidey12345 said:
Simmon, just wondering, i dont think it complies in gcc copmlier

becuase of double varible declartion, any idea?

Please provide context when posting a followup. Don't assume everyone
can see the article to which you're replying.

What do you mean you don't *think* it compiles in gcc? Does it
compile, or doesn't it?

I just tried compiling it with gcc; there were no warnings or error
messages. What errors did you get?
 
S

Simon Biber

spidey12345 said:
Simmon, just wondering, i dont think it complies in gcc copmlier

It does for me. I used this command:

gcc -ansi -pedantic -Wall -W -O2 baddata.c

Make sure you name your C file with .c

If you use the .cpp extension, it will compile as C++, and my code is
not compatible with C++.

becuase of double varible declartion, any idea?

What double variable declaration?
 
S

spidey12345

yeah, sorry, i got it to work..

but whats wrong if the user had something like blah , either

with commer in the middle, also if i want to start a new paragraph, how
can it reformat it so just one \n, so that i can implment another
paragraph.



i tried to put inside printlist statement

if (strcmp(char*)p->next, ","== 0)
{

}
else

{

put(" ");
}

that will give me segment falt, i know that if p->next is either null
or endline, will screw it up, but i have no idea acutally how to
implment it
 
B

Bill Pursell

spidey12345 said:
yeah, sorry, i got it to work..

Please quote context.
if (strcmp(char*)p->next, ","== 0)
{

}
else

{

put(" ");
}

that will give me segment falt, i know that if p->next is either null
or endline, will screw it up, but i have no idea acutally how to
implment it

No, it doesn't get far enough to generate a segfault, because it
doesn't even come close to compiling. Also, your bracing
style is ... less than optimal.

In summary: post context, post code which compiles cleanly, and
use a reasonable bracing style, or people will not pay any attention
to your posts.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top