Thread: file cearching tutorial

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

    file searching tutorial

    anyone know a good tutorial for searching a file, its a text fle and i need to search for aspects of the file. its a flight database and i need to search for flight numbers, routes and flight times mainly
    Last edited by chris285; 04-19-2005 at 07:29 AM.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Read in the file to different buffers and then do the search.

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    You have to look for common elements of the things you are looking for, like do the flight numbers have a consistent prefix.

    Or if you file is delimited and has fields you can read in a line and search through for the correct field and read it into a buffer.

    Or if your file has a fixed record length and field lengths you can read in a record at a time and look for the specific bytes that hold the info you need.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    On the Win32 platform, one solution is file-mapping.

    Kuphryn

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    84
    having a problem with reading the file into the buffer and it displaying it, this is what i have so far

    Code:
    int Cfunctions::Flightdetails()
    {
    	cout << "Please enter flight number" << endl;
    	cin >> flightno;
    
    	system("cls");
    	
    	ifstream file;
    
    	file.open("flight.txt",ios::nocreate);
    
    	if(!file)
    		{
    
    		cout<<"UNABLE TO OPEN FILE!!"; 
    
    		goto end; 
    
    		} 
    
    	while(file) 
    
    		n = 1;
    		file.get(ch);
    			
    			char outputting[57];
    
    			outputting[n] = ch;
    			n++;
    
    			cout << outputting << endl;
    
    			if(ch=='~')
    			{
    				ch=NULL;
    			}
    			if(ch=='#')
    			{
    				ch=NULL;
    			}
    
    			end:
    			
    			file.close();
    			
    		
    
    	return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try replacing NULL with '\0' or just 0. NULL is a specific address that is gauranteed to exist, not the null char, which has the integer value of zero, 0. Since zero can be confused with capital Oh, I feel it is best to be explicit and use the char description of the null char, which is '\0'.

    Also, put the checks for '~' and '#' before the lines you use to append ch to the buffer.
    You're only born perfect.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It also looks like you're missing braces around your while loop (it will probably infinite loop)

    And get rid of the goto statement and just put 'return 1'(or some error code) unless you plan on adding more code at the end of the function (in which case there may still be a better solution than goto). The file should also implicitly close when the ifstream goes out of scope at the end of the function.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM