Problem with fstream

This is a discussion on Problem with fstream within the C++ Programming forums, part of the General Programming Boards category; First off, I'd like to say this is a hw problem. I have gutted out other functions that are fine ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    25

    Problem with fstream

    First off, I'd like to say this is a hw problem. I have gutted out other functions that are fine and left only the one im having a migraine on X_X. So, the problem is reading data in from a file which i cant seem to do.

    sample file entries from LAB1DATA.txt
    Code:
    joe smith 12
    joe smoe 66
    joe someone 19
    My gutted work:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <assert.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Node
    {
    	char firstname[30];
    	char lastname[30];
    	int age;
    };
    
    struct Listtype
    {
    	Node * people[10];
    	int size;
    };
    
    //Function Prototypes
    Listtype initialize(Listtype&);//gutted out
    int menu();//gutted out
    void deallocate(Listtype&);//gutted out
    void printList(Listtype);//gutted out
    void fillList(Listtype&);
    void doInsert(Listtype&);//gutted out
    void doDelete(Listtype&); //gutted out
    
    void main() //left here so you can get the gist of what the prog is suppose to do.
    {
    	Listtype List;
    	int choice;
    
    	initialize(List);
    	do
    	{
    		system("cls");
    		choice = menu();
    		switch(choice)
    		{
    			case	1:	fillList(List); break;
    			case	2:	printList(List); break;
    			case	3:	doInsert(List); break;
    			case	4:	doDelete(List); break;
    			case	5:	break;
    			default	:	cout<<"\n INVALID...1-5 ONLY!\n";
    						cin.ignore();
    						cin.ignore();
    		}
    	}while( choice != 5 );
    	deallocate(List);
    }
    
    void fillList(Listtype &List)
    {
    	int i = 0;
    	char fname[30];
    	char lname[30];
    	int tempAge;
    	system("cls");
    
    	ifstream infile("c:LAB1DATA.txt");
    	assert( !infile.fail() );
    
    	while( infile>>fname>>lname>>tempAge )
    	{
    		List.people[i] = new Node;
    		
    		strcpy(List.people[i]->lastname, lname);
    		strcpy(List.people[i]->firstname, fname);
    		List.people[i]->age = tempAge;
    		cout<<List.people[i]->lastname << List.people[i]->firstname << List.people[i]->age <<endl ;
    		i++;
    	}
    	List.size = i;
    }
    i originally wrote the while loop in fillList() as:
    Code:
    int i = 0;
    while( !infile.eof() )
    	{
    		List.people[i] = new Node;
    		infile<<List.people[i]->lastname << List.people[i]->firstname << List.people[i]->age;
    		i++;
    	}
    this didnt work, so i refered to sample input loop from my instructors handout which is in the above code - which also does not work the way how im using it :/. I have no clue why it isnt reading the data from the file. Any suggestions? thnxs in advance

    minor note - im using Microsoft Visual C++ 6.0, as the school lab uses this compiler and my professor REQUIRES us to have our labs to work on school computers which is a pain, so ive confined myself to use Microsoft Visual C++ 6.0 and not dev C++ : /
    Last edited by I BLcK I; 09-30-2006 at 01:07 AM.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <assert.h>
    #include <stdlib.h>
    #include <string.h>
    Is that REALLY what your class is teaching you? Three of those are C headers, you should be using the C++ equivalents, and the other two haven't been standard C++ for almost ten years. void main() is likewise not C++.

    It seems to me your teacher should be teaching a somewhat newer standard. I mean it's not like the standard is brand new, it came out in 1998...
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    well the instructor made no mention of depricated headers but on the sample codes its listed as ".h" and this is a data structures class so i dont think he really cares so <_<

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so i dont think he really cares so <_<
    Well, if he doesn't care then there's no problem with doing it correctly, is there? Can you describe how your code doesn't work? Have you tried stepping through it in the debugger?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Well i agreee with you that i should be using standard code, but thats the least of my worries right now. I've helped out a friend with java and his instructor was a dinosaur who refused to accept code from ver 1.5 including the scanner and only accepted code from ver 1.4x : / I digress so id like to discribe the prblem and situation as clearly as i can, and no i havent tried it yet through the debugger but i will.

    The code i have written compiles error free. When i run the program, the menu appears as it should. When i enter in option 1 to read LAB1DATA.txt, screen blinks which leads me to believe it has gone through the function and returns to the menu. Next, I enter in option 2
    and all i get from the cmd window is "List size = 0" and "Hit return to continue...", so at this point I continue back to the menu and choose option 5 to quit the program.

    the printList() funtion:
    Code:
    void printList(Listtype List)
    {
    	system("cls");
    	for(int i = 0; i < List.size; i++)
    	{
    		cout<<i+1<<". "<<List.people[i]->lastname <<" "<< List.people[i]->firstname <<" "<< List.people[i]->age <<endl;
    	}
    	cout<< "List size = " << List.size << endl;
    
    	cin.ignore();
    	cout<< "Hit return to continue..."<<endl;
    	cin.ignore();
    }
    I know this funtion does work. I have only tried fillList() and printList(). LAB1DATA.txt is in the right directory and has valid data. What am I missing?!?! Ive gone through the book example and my instructors example handout looking over them enough times to make me wanna sink my teeth into the book and attempt to be like cujo! I followed the syntax(from the book and handout) and they doesnt work! Any suggestions? wOOf!
    Last edited by I BLcK I; 09-30-2006 at 01:36 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    ajksld;fiaweg;whg *Bark bark*, *Grrrrrrr*

    I found my error on this line
    Code:
    ifstream infile("c:LAB1DATA.txt");
    should have been:
    Code:
    ifstream infile("c:\\LAB1DATA.txt");
    *tears papers up with mouth some more*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Fstream Problem
    By Xterria in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2001, 11:03 PM

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