Thread: Readng in and Writing to a file at same time?

  1. #1
    yo_da84
    Guest

    Question Readng in and Writing to a file at same time?

    Hi
    im just wondering if this is possible:

    I have made a CD Database at, which when the menu is loaded you can add/search for CD etc. But whilst entering the CD details, i wont to be able to search the db and to see if the name/artist already exists, if it does, tell the user that the name is already taken.

    Thanks guys
    Daz

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Surely, when you are taking in the data for a new CD, you are buffering this in memory, therefore not writing to the file, hence reading from the file should be completely normal?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    yo_da84
    Guest

    take a look?

    i want it to make sure that the info i just typed in, isnt already in the file yeah?

    sorry not spaced out. (Mod: well read the announcements then to find out how!)
    Code:
    void addToDb()
    {
    	char cdnum[3], title[30], artist[25], tracks[3], rel[10];
    	
    	ofstream outfile;
    	outfile.open("cddb.dat", ios::app);
    	
    	if(!outfile)
    	{
    		cout<<"\n!!Error opening File!!\n";
    	}
    	else
    	{
    		cout<<"\nEnter CD: ";
    		cin.getline(cdnum, 3);
    		cout<<"\nEnter Album Title: ";
    		cin.getline(title, 30);
    		cout<<"\nEnter Name of Artist: ";
    		cin.getline(artist, 25);
    		cout<<"\nEnter Tracks: ";
    		cin.getline(tracks, 3);
    		cout<<"\nEnter Album Release Date: ";
    		cin.getline(rel, 10);
    
    		outfile<<cdnum<<"/"<<title<<"/"<<artist<<"/"<<tracks<<"/"<<rel<<"/"<<endl;
    	
    	}
    	outfile.close();
    }

  4. #4
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    Okay, I think I know what you mean. While the user is entering input, after a certain field is obtained, then you want to search for that field in the Database while they continue to enter other information - But your problem is that if your application begins to search for information in that Database then the user will not be able to continue to enter information while the database is being accessed.

    If that is your problem then one solution would be to create another thread which searched for the instance in the DB, and if that instance was found then it would call a function which alerted the user.
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

Popular pages Recent additions subscribe to a feed