Database

This is a discussion on Database within the C++ Programming forums, part of the General Programming Boards category; he read only recordnumber 1 from file test.txt please help me attachement below Code: #include <iostream> #include <string> #include <cassert> ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    Database

    he read only recordnumber 1 from file test.txt
    please help me
    attachement below
    Code:
    #include <iostream>    
    #include <string>      
    #include <cassert>     
    #include <fstream>   
    using namespace std;
    
    char c;
    string line;
    int Numb;
    int lineNumber;
    char Data [10];
    const int FileNameLen = 30, ArrayLen = 6;
    
    
    int main() 
    { 
        ofstream outfile; 
        char filename[FileNameLen]; 
        
    
        cout << "Enter file name\n";            
        cin >> filename;
        
    	ifstream inFile(filename);    
    	
    	if (filename != "") // if filename is difrent then nothing, file exist
    	{
    	  cout << "File is Open\n";
    	}                              
    	
        cout << "Enter a Recordnumber): ";	
    	cin >> Numb;            
         
    	
    	int lineNumber = Numb;     
     //ERROR: LineNumber is RecordNumber but he takes,
     // Only recordnumber 1 data and 2 doesn't work from file test.txt
    
    
    	if (Numb <= 0)            	{
         cout << "No Such Recordnumber,Program Will Shut Down\n)";
         return 0;
    	}
    	
    	else
     
    	{
    		inFile >> line;
    		if(inFile.eof())
           {
    			inFile.close();
    			cout << "End of File): ";
    			return 0;
    		}
    		inFile.seekg(lineNumber, ios::beg);
    		getline(inFile, line);          
    		
    	}
    	cout << "Line Number Information: " << line << "\n";	
    	//cout << "The read position is at: " << lineNumber << endl;
    	inFile.close();
    
        cout << "END OF THIS TEST PROGRAM WRITED BY CYBERBJORN AND MODJO\n";
    
        return 0;
    }
    Attached Files Attached Files

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >he read only recordnumber 1 from file test.txt
    Because that's what you told it to do. If you want to read more lines you'll want to place the code to read a single line in a loop.

    >#include <iostream>
    Wrong forum. This is the C forum, not the C++ forum. The two languages are different enough for that to be an issue.

    p.s. Code tags would make your code vastly easier to read.
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, for starters, you're on the wrong board. I'm sure a moderator will set you straight here soon enough. (In other words, don't post this again on the C++ board, it'll get moved.) But your real problem is going to be that you can't seek in text files accurately. You'll need to loop through reading one line at a time until you get as far as you need to.

    [edit]
    Curses, foiled again.
    [/edit]

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Moved and tagged
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    I would do something like


    Code:
    int i=0;
    while(!inFile.eof() && i<lineNumber )
    {
    getline(inFile, line); 
    i++;
    }
     
    if(linenumber<=i)
    cout<<"Record not found";
    else
    cout<<"the record is "<<line;

    but some one here can you give you a more elegant solution
    Last edited by vasanth; 04-11-2004 at 02:13 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while(!inFile.eof() && i<lineNumber )
    The eof member function wasn't designed to be used as a loop condition.

    >but some one here can you give you a more elegant solution
    You can only get so elegant with sequential search...
    Code:
    int i;
    ...
    for ( i = 0; i < lineNumber && getline ( inFile, line ); i++ ) {
      if ( /* Match */ )
        break;
    }
    if ( i < lineNumber )
      cout<<"Record found"<<endl;
    else
      cout<<"Record not found"<<endl;
    My best code is written with the delete key.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Prelude
    >while(!inFile.eof() && i<lineNumber )
    The eof member function wasn't designed to be used as a loop condition.
    [/code]
    Look slike i have been making the mistake for a long time... Can you please elobrate on why it is not recommended to use it in a loop condition... Just interested ...

    thanx in advance

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you please elobrate on why it is not recommended to use it in a loop condition
    Because the stream only goes into an eof state after a read is attempted and fails. The result is that you process the last piece of input twice before the loop terminates. It's a classic fencepost error.
    My best code is written with the delete key.

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Prelude
    >Can you please elobrate on why it is not recommended to use it in a loop condition
    Because the stream only goes into an eof state after a read is attempted and fails. The result is that you process the last piece of input twice before the loop terminates. It's a classic fencepost error.
    hmm that figures why i was having so much of problems before.....
    And thanks for poking fun at me in your Cprogramming Drinking Game " thread- A post using feof ( stream ) or stream.eof() as the loop condition."

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >And thanks for poking fun at me in your Cprogramming Drinking Game " thread
    That particular item was there because of the frequency with which I see people doing it. The point of the drinking game is to drink as much as possible without saying "take a drink if someone posts".
    My best code is written with the delete key.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    char filename[FileNameLen]; 
        
    cout << "Enter file name\n";            
    cin >> filename;
        
    ifstream inFile(filename);    
    	
    if (filename != "") // if filename is difrent then nothing, file exist
    {
        cout << "File is Open\n";
    }
    The if test above does not check to make sure the file is open. If the file doesn't exist then what... you're code keeps going even if there is no file? Also you have #include <string> so maybe you should read in the filename as a string object so you don't have to worry about a fixed size character array being overrun:
    Code:
    string filename;
    
    cout << "Enter file name\n";
    cin >> filename;
    
    ifstream inFile(filename.c_str());
    
    if( !inFile.is_open() )  // Use the is_open() member function to test if open succeeded
    {
        cout << "File not opened, exiting program.\n";
        return 1;            // Exit program if could not open a file
    }
    else cout << "File is Open\n";
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 11:34 AM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Developing database management software
    By jdm in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 04:06 PM
  5. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 09:17 PM

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