Thread: searching problem

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    84

    searching problem

    ok i want to read a file into a buffer a line at a time, but atm the moment it reads the whole file and i am not sure how to sort this out

    Code:
    // Cfunctions.cpp: implementation of the functions class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "functions.h"
    #include <iostream>
    #include <windows.h>
    #include <fstream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    Cfunctions::Cfunctions()
    {
    
    }
    
    Cfunctions::~Cfunctions()
    {
    
    }
    
    Cfunctions::Printall()
    {
    	system("cls");
    	
    	ifstream file;
    
    	file.open("flight.txt",ios::nocreate);
    
    	if(!file)
    		{
    
    		cout<<"UNABLE TO OPEN FILE!!"; 
    
    		goto end; 
    
    		} 
    
    	while(file) 
    
         { 
    
         file.get(ch); 
    
    		if (ch=='~')
    		{
    			ch=NULL;
    		}
    		if (ch=='%')
    		{
    			ch=NULL;
    		}
    		if (ch=='#')
    		{
    			ch=NULL;
    		}
    
         cout << ch << flush;
    	
         } 
    
    	getch(); 
    
    	end: 
    
    	file.close(); 
    
    	return 0;
    
    }
    
    int Cfunctions::Flightdetails()
    {
    	int length1;
    	char * buffer;
    
    
    	cout << "Please enter flight number" << endl;
    	cin >> flightno;
    
    	system("cls");
    	
    	ifstream is;
    
    	is.open("flight.txt",ios::nocreate);
    
    	is.seekg (0, ios::end);
    	length1 = is.tellg();
    	is.seekg (0, ios::beg);
    
    	 buffer = new char [length1];
    	
    	 is.read (buffer,length1);
    
    	 is.close();
    
    	cout.write (buffer,length1);
    
    
                   return 0;
    
    }
    can anyone help?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i haven't tested it, but give this a whirl. ( i'm not 100% sure it will work though). i enboldened the answer to your specific question.

    Code:
    int Cfunctions::Printall()
    {
    	system("cls"); //try to find an alternative in the faq
    
    	ifstream file;
    	file.open("flight.txt",ios::nocreate);
    
    	if(!file)
    		return -1;
    	
    	while(file && !file.eof()) 
    	{ 
                    file.get(ch); 
    
    		switch(ch)
    		{
    			case '~': 
    			case '%': 
    			case '#':
    				ch = 0;
    				break; //really kind of unecessary
    		}
    	        cout << ch << flush;
    	}			 
    
    	file.close(); 
    	getch(); 
    
    	return 0;
    }
    
    int Cfunctions::Flightdetails()
    {
    	
    	cout << "Please enter flight number" << endl;
    	cin >> flightno;
    	system("cls");
    
    	ifstream is("flight.txt",ios::nocreate);
    	if(!is)
    		return -1;
    
    	
    	char buffer[500];
    
    	while(is && !is.eof())
    	{
    		is.getline(buffer,500);
    		cout << buffer << endl;
    	}	
    	is.close();
    
            return 0;
    
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    how can i get it to only read in 45 characters at a time, or 1 line of the file anyway

    and it should search through the file until it finds the right line, and then print the line out

    any ideas?

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You use getline() to read in only 45 characters. You can also use it to read up to a delimiter like '\n'.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    84
    if i change the buffer its just staggers the output, and still displays the entire file

    how can i get it to stop at a '#' symbol which is my end of line?

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Use getline(), and set the delimiter equal to '#'.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    84
    ok the buffer now works, but i need it to read 1 line of characters into the buffer at a time

    i need to search for a flight number, and output only the line that it appears on

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    That's great. I'm glad you have a thorough understanding of your problem. That's an important part of programming. So is writing code. Try the program yourself and if you get stuck, then we can help, but we're not going to write it for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM