473,386 Members | 1,962 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,386 software developers and data experts.

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

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
2 2521
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
8
by: Radu | last post by:
Hi, I have the following problem: I open a recordset based on excel/csv files, but then I need to filter (in code) in order to extract only data pertaining to a specific person. This code is...
7
by: spidey12345 | last post by:
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...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
4
by: jla1357 | last post by:
In my program I need to read in a file, search for 100 unique words, and count how many times a found word has been found all by using an array. This is what I have so far, but when the file is found...
2
by: Zethex | last post by:
At the moment i'm doing a piece of work for school and I'm stuck at the moment. I have a list of words, for example: Sentence = I have another list which I need to use to replace certain...
1
by: Alexnb | last post by:
Okay, what I want to do with this code is to got to thesaurus.reference.com and then search for a word and get the syns for it. Now, I can get the syns, but they are still in html form and some are...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.