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

C++ FileIO problem.

1
Hi, Im having difficulty populating a linked list with data from a file.

Expand|Select|Wrap|Line Numbers
  1. cout << "Please enter filename: ";
  2.         string filename; 
  3.         getline(cin,filename);
  4.  
  5.         ifstream myfile;
  6.         myfile.open (filename.c_str());
  7.  
  8.         if (myfile.is_open())
  9.         {
  10.             node *temp;
  11.  
  12.             temp = new node;
  13.             while (!myfile.eof())
  14.             {
  15.                 getline(myfile,temp->name);
  16.                 getline(myfile,temp->info);
  17.  
  18.                 if (start_ptr == NULL)
  19.                 { 
  20.                     start_ptr = temp;
  21.                     start_ptr->nxt = NULL;
  22.                 }
  23.  
  24.                 cout << start_ptr->name;
  25.                 cout << start_ptr->info;
  26.             }
  27.                 myfile.close();
  28.         }
  29.         else
  30.             cout << "Unable to open file";
My input file is

a
a

b
b

c
c


Im just trying to get it to correctly create the start_ptr to being with.
start_ptr has a value of NULL to begin with and as far as I can see this code should only print "aaaaaa" but it prints "aabbcc".

If I put in the rest of my code to create the ordered list the whole thing goes into an infinite loop. It works fine without the file input.

What am I doing wrong?
Apr 1 '06 #1
1 2469
Banfa
9,065 Expert Mod 8TB
You have 2 problems
  1. The line
    temp = new node;

    is where you create your entries in the linked list. However this appears outside the loop that reads them. This means that although you read 3 items from the file for the list you have only ever created 1 item. Each item you read from the file after the first item overwrites the previous item.
  2. You only handle the case of start_ptr == NULL, i.e. adding the first entry to the list, you do not handle the case of start_ptr != NULL, adding subsequent entries to the list.

The reason you see aabbcc when you expected aaaaaa is that you only have 1 object that you are overwriting the contents of and printing out.
Apr 3 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
4
by: Layin'_Low | last post by:
i'm havin a problem with this simple program. i need the output of this loop to be written to a file but i dont know how to do it!! this is what i have right now: gclas = raw_input("What is the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
1
by: Moe | last post by:
I have a winForm control that contains an openFileDialog. I am using this control in a webForm by enbedding it using an <object> tag. I need to access some information about the file on the client....
0
by: Steven Smith | last post by:
Hi guy's I've hit a bit of a sticking point, I'm sure this is simple enough but I just can't get my head around it and I'm looking for a bit of direction. When dealing with FileIO up until now I...
8
by: Vai2000 | last post by:
Hi All, How do I access a shared Drive which requires username and password to access? I need to write a file in a folder within it TIA
3
by: Marquis | last post by:
I am writing a ActiveX dll using Visual C# However , i am facing fileio and environment permission problem. Is there any way to solve this problem? just as signing the dll or other method to...
0
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
Hi All, I have the following: FileIO.FileSystem.CopyDirectory(strFromString, strLocString, FileIO.UIOption.AllDialogs) Q: How do I access the UI that is presented and re-positon the x,y...
1
by: =?Utf-8?B?bWFnZWxsYW4=?= | last post by:
hi all, I have a button (stopcopy) that I want the user to cancel out of a long file copying operation. I'm not clear what I should "adding" in the sub to make this happen besides & return....
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.