1 Replies - 1829 Views - Last Post: 26 February 2009 - 10:45 AM Rate Topic: -----

#1 digitalhitman00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

Writing linked list back to a file produces extra carriage return at e

Posted 26 February 2009 - 09:18 AM

Hi all,
I have a program that gathers information from a file then updates the file once the program is finished. The program has uses a linked list that reads from a file, you can alter changes to the linked list during the program, and at the end of the program the linked list is wrote back to the file again. I understand why its adding an extra carriage return at the end because the way my method writeBackItemFile() is setup, but how do I remove any empty spaces while writing to the file so I do not end up with any extra null info? Basically I can run the program once and all my data appears fine. Then I re run the program and i see my data, plus additional null data that was added to it I am assuming from the writing proccess. So basically if I display only 3 items the first time, I see 4 items (1 of the items uses null data) the next time, 5 items when I rerun it again, and so on. What might be a better way to do this where the null items are not added to the end?

Here is the file it uses:


input.txt
127 Candy 3 50 0.5
128 Chips 21 50 0.8
129 Gum 15 50 .25




Here is the code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdlib>
#include <list>

using std::ifstream;
using namespace std;


struct node
			{
			int num;		//Number of Product
			string name;	//Name of Product
			int qty;		//Qty of Item
			int maxQty;	 //Max Qty of Item
			float price;	//Price of Item
			node *nxt;		// Pointer to next node
			};
node *start_ptr;

void add()
  {
ifstream infile;
infile.open("input.txt");

while (!infile.eof())
{
	  node *temp, *temp2;   // Temporary pointers
	 // Reserve space for new node and fill it with data
	 temp = new node;


	 // Variables collected from the File applied
	 infile>>temp->num;
	 infile>>temp->name;
	 infile>>temp->qty;
	 infile>>temp->maxQty;
	 infile>>temp->price;
	 temp->nxt = NULL;

	 // Set up link to  node
	 if (start_ptr == NULL)
		 start_ptr = temp;
	 else
	   { temp2 = start_ptr;
		 
		 while (temp2->nxt != NULL)
		   {  temp2 = temp2->nxt;
			  // Move to next link in chain
		   }
		 temp2->nxt = temp;
	   
	 }
  }
	 infile.close();

}

void display()
{

node *temp;
temp = start_ptr;
do
  {  if (temp == NULL)
	   cout << "End of list" << endl;
	 else
	   {  // Display details for what temp points to
		  cout << "Number : " << temp->num << endl;
		  cout << "Name : " << temp->name << endl;
		  cout << "Qty : " << temp->qty << endl;
		  cout << "Max Qty : " << temp->maxQty << endl;
		  cout << "Price : " << temp->price << endl;
		  cout <<"*******************\n";
		  cout << endl;	   // Blank line

		  // Move to next node (if present)
		  temp = temp->nxt;
	   }
  }
while (temp != NULL);
}

void writeBackItemFile()
{
ofstream outfile;
outfile.open("input.txt");

node *temp;
temp = start_ptr;

while(temp != NULL)
{

		  outfile<< temp->num <<" ";
		  outfile<< temp->name<<" ";
		  outfile<< temp->qty<<" ";
		  outfile<< temp->maxQty<<" ";
		  outfile<< temp->price<<"\n";
	  
				  
		 // Move to next node (if present)
		  temp = temp->nxt;
}

outfile.close();
}


int main()
{
add();
display();
writeBackItemFile();
cin.get();
cin.get();
}





Thanks for the Help!

This post has been edited by digitalhitman00: 26 February 2009 - 09:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing linked list back to a file produces extra carriage return at e

#2 digitalhitman00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-February 09

Re: Writing linked list back to a file produces extra carriage return at e

Posted 26 February 2009 - 10:45 AM

I figured it out. All I had to do is move the placement of the '\n' to a if statement if temp == NULL. The code is shown below.


void writeBackItemFile()
{
ofstream outfile;
outfile.open("input.txt");

node *temp;
temp = start_ptr;

while(temp != NULL)
{

		  outfile<< temp->num <<" ";
		  outfile<< temp->name<<" ";
		  outfile<< temp->qty<<" ";
		  outfile<< temp->maxQty<<" ";
		  outfile<< temp->price;

		  
					 temp = temp->nxt;
		  if (temp != NULL)
			outfile<<"\n";

   
			  
		 // Move to next node (if present)
		 
}

outfile.close();
}




THANKS!
Steve
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1