Thread: fstream help

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    fstream help

    Not sure if i should have added this to my enum post but oh well. If i have a file and read the in a line such as:

    CPTR212 Programming 1

    then it searches the string for "Programming" if finds it and than how can i get to go back to the beging of that specific line. The file i am using has a bunch of different lines all at diff lengths.

    sample file:

    CPTR211 Web Design
    CPTR212 Programming 1
    CPTR213 Database Management
    MATH101 College Algebra
    MUS207 Keynotes
    ART101 Basic Drawing

    I am tring to write a function to let the user either enter the course ID or the actual course name. CPTR211 for example would be the course ID and Web Design would be the actual Course Name. Here is some code i have. I cant get it to work correctly. I know this right always enters the same if statment but i cant figure out a way to switch it around. I need to store the ID and course name. If id is entered than it stores it and get the course name automatically. Vice versa, if the course name is entered it get stored than it get the id for that class automatically. Please Help. This is kinda urgent but not too much.

    Code:
    string students::getCourse()
    {
    	char Classname[256],
    		ch;
    	string ClassName,
    		Class,
    		str;
    	bool loop = true;
    	ifstream inStream;
    	int pos,
    		i = 0;
    
    	cout << "Course: ";
    	cin.getline(Classname, 256, '\n');
    	ClassName = Classname;
    	str = ClassName.substr(0, 4);
    
    	inStream.open("classID.kss");
    	if (inStream.is_open())
    	{
    		while (loop)
    		{
    			inStream >> Class;
    	
    			pos = ClassName.find(Class.data(), 0);
    			if (pos != string::npos)
    			{
    					if (Class.substr(0, 4) == str)
    					{
    						CourseID = Class;
    						inStream.get(ch);
    						getline(inStream, Class);
    						CourseName = Class;
    						cout << endl << "Yes\nCourse ID: " << CourseID << endl << "Course Name: " << CourseName << endl;
    						break;
    					}
    					else
    					{
    						CourseName = Class;			
    						getline(inStream, Class);
    						CourseName = CourseName + Class;
    						inStream >> Class;
    						CourseID = Class;
    						cout << endl << "Course ID: " << CourseID << endl << "Course Name: " << CourseName << endl;
    						break;
    					}
    				break;
    			}
    
    			if (inStream.eof())
    			{
    				break;
    			}
    
    		}//End Infinite For Loop
    	}
    
    	return CourseName;
    }//End getClassName Function

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you know if you've read a course number, and how do you know it's a class name? If you have a clear way of telling which is which, it should be easy to tell what you're reading. Then, based on what the first read is, you fill the appropriate second variable.

    In your example file, I only see you doing it one way, so how are you going to test the second method? Shouldn't you have at least one entry there which the name comes before the number?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM