buffer searching problem

This is a discussion on buffer searching problem within the C++ Programming forums, part of the General Programming Boards category; ok i have tried to get the following piece of code to go from 1 line of char's in a ...

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

    buffer searching problem

    ok i have tried to get the following piece of code to go from 1 line of char's in a buffer to the next, check to see if the first 6 lines match and if so print out the line

    however i am don't know why it dosen't print out correctly

    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>
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    Cfunctions::Cfunctions()
    {
    
    }
    
    Cfunctions::~Cfunctions()
    {
    
    }
    
    Cfunctions::Printall()
    {
    	system("cls"); 
    
    	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;		
                                     }
    	        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[48];
    	char buffer2[48];
    	
    	do 
    	{
    		is.getline(buffer,48, '#');
    		if (buffer[0] == flightno[0])
    		{
    			if (buffer[1] == flightno[1])
    			{
    				if (buffer[2] == flightno[2])
    				{
    					if (buffer[3] == flightno[3])
    					{
    						if (buffer[4] == flightno[4])
    						{
    							if (buffer[5] == flightno[5])
    							{
    								cout << buffer << endl;
    							}
    						}
    					}
    				}
    			}
    		}
    		else 
    		{
    			if (n == 0)
    			{
    				is.seekg(45, ios::beg);
    				n++;
    			}
    			else
    			{
    				is.seekg(45, ios::cur);
    			}
    		}
    		
    	}while(is && !is.eof());	
    
    	is.close();
    
            return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by chris285
    ok i have tried to get the following piece of code to go from 1 line of char's in a buffer to the next, check to see if the first 6 lines match and if so print out the line

    however i am don't know why it dosen't print out correctly

    Code:
    		if (buffer[0] == flightno[0])
    		{
    			if (buffer[1] == flightno[1])
    			{
    				if (buffer[2] == flightno[2])
    				{
    					if (buffer[3] == flightno[3])
    					{
    						if (buffer[4] == flightno[4])
    						{
    							if (buffer[5] == flightno[5])
    							{
    								cout << buffer << endl;
    							}
    						}
    					}
    				}
    			}
    		}
    There's definitely a better way to go about comparing strings. Speaking of which, why are you working with C-style strings instead of using the string class?

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    84
    its the only way i know how to use strings

    problem i have is it dosen't move forward and therfore seek properly, and outputting what it should

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Another searching problem
    By 182 in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2006, 07:05 PM
  3. Searching problem
    By 182 in forum C++ Programming
    Replies: 23
    Last Post: 02-18-2006, 11:38 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 10:12 PM

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