reading from a text file queation

This is a discussion on reading from a text file queation within the C++ Programming forums, part of the General Programming Boards category; i want to read stuff in a text file one line at a time by line because i have to ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    reading from a text file queation

    i want to read stuff in a text file one line at a time by line because i have to store the first thing in each line in a list the first time though, close the file reopen it again to get back to the top of file, search for that first item in the first line that was placed in the list and add the rest of the info in that like so it is associated with it then go to the next line and so on. at the end of each line there is a # to indicate the end.

    the only thing i am having trouble with is with the loop where you put the first item in the line and skip the rest of the things in the line until u hit the # symbol.

    here is an example txt file of what i mean

    Code:
    Atlanta       Houston  650   Washington   600 #
    Austin     Dallas   200  Houston   300 #
    Buffalo     New_York  450   Newark  500   #
    Chicago     Denver  550   New_York  950   #
    Dallas     Austin    200  Chicago    1500 #
    Denver     Atlanta    800   Chicago   550 #
    Houston     Atlanta    650 #
    Newark         #
    New_York     Chicago   950  Buffalo   450  #
    Washington    Atlanta  600  Dallas   700  #

    here is the function i am working on i have highlited in red where my trouble spot it. its the while statement that i dont get to how i can recognize the # so it knows when to stop inFiling nothing and go to the next line. the way i have it now i get an error because of the != but dosent string have a != operator? i made one myself and it still said the same thing

    Code:
    void Graph::GetGraph()
    {
    	Edge edge;
    	Vertex V;
    	string vName;
    
    	cout << "Enter the the file location" << endl;
    	cin >> inFileName;
    	inFile.open(inFileName.c_str());
    	if (!inFile.is_open())  //test for file
    	{
    		cerr << "Cannot open file: " << inFileName << endl;
    		getche();
    		return;
    	}
    	while(!inFile.eof()) //loop through untill end of file
    	{ //cout << "hi" << endl;
    		inFile >> V.name;
    			
    		g.push_front(V);
    		VertNum++;
    
    		// Loop until # is inFiled - the next inFile will either be another V or eof
    		while(V.name != '#')
    		{
    			inFile;
    		}
     
    	}// end obtain info 
    	//cout << "hi" << endl;
    	inFile.close();
    	inFile.clear();
    
    
    	// Reopen file - load edges
    	
    	while(!inFile.eof())
    	{
    		inFile >> vName;
    
    		// Find matching Vertex V
    
    	Vertex *walker;
    	Vertex *ptr;
    
    	if(!first)
    	{
    		cout << "There is no Graph" << endl;
    		return;
    	}
    	ptr = NULL;
    	walker = first;
    	while(!walker && (vName > walker->name))
    	{
    		ptr = walker;
    		walker = walker->pNextVert;
    	}
    	if(vName == walker->name)
    		V.name = walker->name;
    	else
    		return;
    /****************************************************/
    
    		// Find the ptr to Vert
    		// Loop thru edge-list
    		while(!'#')
    		{
    			inFile >> edge.name;
    			inFile >> edge.weight;
    
    			V.edgelist.push_front(edge);
    		}
    	}
    
    	inFile.close();//close infile
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    string has a != that works with other strings or string literals. A string literal use double quotes, not single quotes, since single quotes indicate a single character. Use "#" instead of '#'.

    However, I might just use getline to get the rest of the line. If you specify the '#' as the delimiter, then it will read up to the # and then stop.

    BTW, this:
    Code:
    	while(!inFile.eof())
    	{
    		inFile >> vName;
    should really be this:
    Code:
    	while(inFile >> vName)
    	{
    Using eof() to control the loop is a bad idea. You won't be able to handle errors well and you might read and process the last line twice.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    ok when i do that i get an infinite look right there, y?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    What exactly did you do? You don't do anything inside the loop marked in red, so of course it will loop infinitely. I assume you added code inside, but you have to post it for us to see what is wrong.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    i changed the while loop to look like this

    Code:
    while(inFile >> V.name != "#")
    {
    	cout << "hi3" << endl;
    }
    and i get an infinate on hi3

    when there is a argument in brackets it still does it right??

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You are mixing two different things. The return value of inFile >> V.name is evaluated as a true or false boolean that is false if the read failed (normally due to end-of-file). You are trying to compare that to "#". What you want to do is loop while infile >> V.name is true and V.name != "#".

    >> when there is a argument in brackets it still does it right??
    I don't know what you mean here.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    Quote Originally Posted by Daved View Post
    You are mixing two different things. The return value of inFile >> V.name is evaluated as a true or false boolean that is false if the read failed (normally due to end-of-file). You are trying to compare that to "#". What you want to do is loop while infile >> V.name is true and V.name != "#".

    >> when there is a argument in brackets it still does it right??
    I don't know what you mean here.
    so it should look like this

    Code:
    while(inFile >>  V.name && V.name != "#")
    now that i think about it, it dont make sense the way i originally had it. man i hate dealing with files

    and u answered the question about the brackets/parentheses so dont worry about it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 11:51 AM

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