linked list + file system

This is a discussion on linked list + file system within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <fstream> using namespace std; class LinkedList { public: LinkedList(); ~LinkedList(); void push_back(string word); void ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    linked list + file system

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class LinkedList
    {
    	public:
    		
    		LinkedList();
    		~LinkedList();
    		void push_back(string word);
    		void pop_front();
    		bool empty() const;
    		void save();
    
    	private:
    
    		struct Node
    		{
    			string item;
    			Node* next;	
    		};
    
    		int Size;
    		Node *head, *tail, *cur, *prev;
    
    };
    
    LinkedList::LinkedList():Size(0)
    {
    	head = tail = cur = prev = NULL;
    }
    
    LinkedList::~LinkedList()
    {
    	while(!empty())
    		pop_front();
    }
    
    void LinkedList::push_back(string word)
    {
    	if(empty())
    	{
    		head = new Node;
    		head->item = word;
    		head->next = NULL;
    		tail = head;
    	}
    	else
    	{
    		Node* slot = new Node;
    		slot->item = word;
    		slot->next = NULL;
    		tail->next = slot;
    		tail = slot;
    	}
    
    	Size++;
    }
    
    void LinkedList::pop_front()
    {
    	if(empty())
    	{
    		cout << "Error: Attempt to pop empty list" << endl;
    		system("pause");
    		exit(1);
    	}
    
    	Node* oldhead = head;
    	head = head->next;
    	delete oldhead;
    
    	Size--;
    }
    
    bool LinkedList::empty() const
    {
    	if(head == NULL)
    		return true;
    	else
    		return false;
    }
    
    void LinkedList::save()
    {
    	ofstream fout("index.txt");
    	if(fout.is_open())
    	{
    		cur=head;
    		while(cur != NULL)
    		{
    			fout << cur->item << endl;
    			cur = cur->next;
    		}
    	}
    }
    
    int main()
    {
    	LinkedList index;
    	ifstream fin_noindex;
    	ifstream fin_f1;
    	string s, temp;
    	int found;
    
    	fin_f1.open("file2.txt");
    
    	while(fin_f1.is_open())
    	{
    		fin_f1 >> s;
    
    		fin_noindex.open("noindex.txt");
    
    		found = 0;
    
    		while(fin_noindex.is_open())
    		{
    			fin_noindex >> temp;
    
    			if(s==temp)
    			{
    				found++;
    			}
    		}
    		fin_noindex.close();
    
    		if(found==0)
    		{
    			index.push_back(s);
    		}
    	}
    	fin_f1.close();
    	index.save();
    
    	return 0;
    }
    i want to write a program that read file2.txt and compare each words in file2.txt with noindex.txt. If there are any words which is in file2.txt but not in noindex.txt, i am suppose to put it in a linked list, then later save the linked list in index.txt.

    the problem now is: why the program went to an infinite loop ?

    actually i am not so sure wether my implementation or way is correct. Please correct and comment on my code. Thanks in advance.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    while (fin_noindex.is_open())
    change that to 
    while (fin_noindex) or 
    while (fin_noindex.good())
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    why cant use .is_good() ? how come when changed to .good(), no more infinite loop ?

    my program is not running like what i want. The problem now is, the program won't compare the words. Instead, it just save word by word into index.txt. Any idea how to fix it ?

    i tried strcmp(s, temp) but it returned error

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    after i changed it, the program saves NOTHING. Before i changed it, the program saves EVERYTHING. I am confused why and how it happened. Any solutions or ideas ?

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    this is the file2.txt.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    this is the noindex.txt

    each word in file2.txt need to be compare to the noindex.txt. If the word in file2.txt doesn't exists in noindex.txt, then that particular word will be inserts into the linked list and later will be saved into index.txt
    Attached Files Attached Files

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    This would do it (though not perfectly).
    Code:
    int main()
    {
    	LinkedList index;
    	ifstream fin_noindex;
    	ifstream fin_f1;
    	string s, temp;
    	int found;
    
    	fin_f1.open("file2.txt");
    
    	while(fin_f1)
    	{
    		fin_f1 >> s;
    
    		// there's a better way to always read from
                    // the beginning of file using
                    // fin_noindex.seekg(0)
                    fin_noindex.open("noindex.txt");
    
    		found = 0;
    
    		while(fin_noindex)
    		{
    			fin_noindex >> temp;
    
    			if(s==temp)
    			{
    				found++;
    			}
    		}
    	// Apparently you need to reset the stream status
                    // after it's set to eof in preceding loop.
                    // If you don't reset it, no further reading allowed                
                    fin_noindex.clear();  
    		fin_noindex.close();
    
    		if(found==0)
    		{
    			index.push_back(s);
    		}
    	}
    	fin_f1.close();
    	index.save();
    
    	return 0;
    }
    Note, in your program, you will read "now," ("now" with a comma), "This" not equal to "this", and so on..But at least it's working now.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The program looks fine on the outset, the only problem being that you are checking for the end of file too early - it needs to happen after you read in a string. Try something like:

    Code:
        while(fin_noindex >> temp && fin_noindex.good())
      {
       // ...
      }

    But the real bug lay buried within the stl library itself. Apparently, closing the file did not clear the internal file-status flags. When I added this:

    Code:
     fin_noindex.clear();
    ...after the call to

    Code:
     fin_noindex.close();
    ...it worked just fine.

    The output I got was:

    The
    Art
    Modelling
    Conceptually,
    models
    medium
    explain
    system
    operates
    chance
    misunderstanding
    concept
    minimal.
    This
    article,
    introduce
    techniques
    frameworks
    create
    efficient
    models.
    Models
    now,
    been,
    integral
    part
    human
    experience.
    We
    create
    models
    world
    based
    sensory
    inputs:
    visual,
    auditory,
    textile,
    olfactory,
    gustatory.
    These
    models
    inform
    perception
    permit
    understanding
    comparison
    past
    events.
    As
    infants
    develop
    sophisticated
    internal
    mental
    models
    motion,
    shape,
    distance,
    time,
    cause/effect
    effort
    relate
    confusing
    world
    us.
    Starting
    clean
    slate,
    build
    internal
    understanding
    experience.
    Our
    internal
    mental
    models
    world
    key
    communication
    complex
    concepts
    people1.
    Since
    people
    share
    exact
    set
    experiences,
    mental
    models
    same.
    For
    example,
    impossible
    people
    living
    primitive
    hunter-gatherer
    culture,
    contact
    modern
    world,
    understand
    principles
    refrigeration
    simply
    hearing
    description
    mechanics;
    concepts
    completely
    foreign
    experience.
    To
    succeed,
    shared
    communication
    medium
    (
    language,
    pictures,
    gestures,
    etc.),
    translate
    internal
    mental
    model
    related
    understanding
    world:
    examples
    food
    storage,
    effect
    cold
    temperatures
    spoilage,
    forth.
    In
    words,
    align
    explanation
    phenomenon
    shared
    set
    knowledge
    intended
    audience.2
    Without
    common
    information
    base,
    difficult
    person
    explain
    understanding
    phenomenon
    system
    another.
    Moreover,
    probability
    misunderstandings
    increases
    dramatically.
    Similarly,
    developing
    software
    systems,
    person
    development
    team
    understanding
    purpose
    system.
    This
    information
    gathered
    specialists
    (e.g.,
    requirements
    engineers)
    present
    information
    remainder
    team,
    series
    visual
    textual
    models.
    Because
    models
    built
    viewpoint
    specialist,
    important
    "modeler"
    understand
    biases
    assumptions,
    shared
    members
    development
    team.
    This
    clear
    communication
    complex
    concepts
    purpose
    creating
    models.
    This
    series
    articles
    focus
    principles
    practices
    model
    conception,
    creation,
    presentation.
    This
    article
    primarily
    concerned
    principles
    system
    analysis,
    observation,
    experimentation,
    techniques
    employed
    study
    selected
    system.
    The
    article
    focus
    practical
    applications
    modeling,
    emphasis
    software
    development
    models.
    The
    final
    article
    focus
    techniques
    considerations
    presentation
    information,
    UML
    visual
    examples.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    how to i exclude all the symbols such as comma, fullstop, etc ?
    i just need to save the word only excluding the all the puncuation marks.

    1 more thing, if i want to include the line number and the word position in that particular line, how am i suppose to do it ?

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    what does .clear() do ?

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by stalker
    how to i exclude all the symbols such as comma, fullstop, etc ?
    i just need to save the word only excluding the all the puncuation marks.
    Where you test for spaces, test for all other characters too. ispunct(x) is a standard C function to return TRUE if the int value x is punctuation (non-alphanumeric and printable)

    Or use isalnum(x) which returns TRUE only if the value is an digit or alpha

    1 more thing, if i want to include the line number and the word position in that particular line, how am i suppose to do it ?
    Store the values with the word in Node.

    what does .clear() do ?
    Erases extra stuff in the object.
    cin.ignore() will clear the input buffer of unread characters.
    [update]cin.ignore() will clear the input buffer of an unread character.
    [/update]
    Last edited by WaltP; 01-19-2004 at 10:27 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> i just need to save the word only excluding the all the puncuation marks.
    Never done it before so, I would probably check the last character of the string and discard it if I don't want to include it.
    Code:
    #include <cctype>
    int ispunct( int ch );
    
    The ispunct() function returns non-zero if its argument is a
    printing character but neither alphanumeric nor a space.
    Otherwise, zero is returned.
    >> 1 more thing, if i want to include the line number and the word position in that particular line, how am i suppose to do it ?
    For the line number I would probably have a counter that's incremented after every '\n' occurence.
    For the word position, I would have another counter that resets after every '\n' and increments one for each word.

    >> what does .clear() do ?
    In your case, you have to access the whole dictionary file repeatedly. When it reaches the end of file, one of the stream (fin_noindex) states, eofbit, is set to 1. When it happens, the stream is closed for further input until it's set to 0 using clear() method. And that's what you want to do beacuse you want to re-read the dictionary file over and over.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 01:58 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21