473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Oct 23 '06 #1
7 2065
"spidey12345" <sw********@hotmail.comwrites:
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?).

--
Ben.
Oct 23 '06 #2
spidey12345 wrote:
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[i] = 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[i] = 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;
}
Oct 23 '06 #3
Simmon, just wondering, i dont think it complies in gcc copmlier

becuase of double varible declartion, any idea?

Oct 23 '06 #4
"spidey12345" <sw********@hotmail.comwrites:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 23 '06 #5
spidey12345 wrote:
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?

--
Simon.
Oct 24 '06 #6
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

Oct 28 '06 #7
spidey12345 wrote:
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.

--
Bill Pursell

Oct 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
2
by: spidey12345 | last post by:
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...
4
memoman
by: memoman | last post by:
Can any body help me in that program ??? mail me if anybody could reach any -helpfull- thing Write a C++ program that namely insert, delete, and search in a fixed record length file (containing...
1
by: beakersoft | last post by:
Hi, This is my first attempt at trying to code using Ajax/Javascript so bear with me if i've missed something obvious. I'm trying to build a list of links in a div element based on info in an...
1
by: smoothkriminal1 | last post by:
Write a Code that will pick timetable of 40 students from file using linklist n than find a slot where all the students dont have any class. file can be of any format Student can maximum take 6...
2
by: pnd1234 | last post by:
Hi, i am doing a project which represent message attachments in a xml view.I currently implemented the system in a tree view.Like if a message contains word file, zip file,image file , swf file...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.