0

Hey, Thx for the tutorial, but i was wondering how to search for text, not records.

Ex, i want to find all the records that have the word The in them how to do that?

Open file, read a line, if this line contains "The" substring - that's a record you want, get to the next line, if again... until end of file. Simple, right?

0

i got a problem with the write to file function, what i want to do is write whatever is in the variable data into the file.

void write ()
{
fstream fin;
fin.open("files/titles.txt",ios::out|ios::app);
char* data;
cout<<"Enter the data you want to insert inside the file\n";
cin>>data;

fin.write(data,int);

fin.close();

}
0

i got a problem with the write to file function, what i want to do is write whatever is in the variable data into the file.

void write ()
{
fstream fin;
fin.open("files/titles.txt",ios::out|ios::app);
char* data;
cout<<"Enter the data you want to insert inside the file\n";
cin>>data;

fin.write(data,int);

fin.close();

}

There are a few problems with your code.The char *data, is just a pointer.You need to allocate some memory to it before you call cin or it will cause the program to crash.

Better yet just allocate the memory statically (normally) like char data[100].
the write function should be called as file_handle.write(data,size of data);

void write ()
{
fstream fin;
fin.open("files/titles.txt",ios::out|ios::app);
char data[100];
cout<<"Enter the data you want to insert inside the file\n";
cin>>data;

fin.write(data,100); //better instead of 100 you can use use strlen(data)

fin.close();

}
0

ok tnks for the help in the above section but i got another piece of a code here that i am wondering why it is not reading the whole line.

ofstream file;

	file.open("gamepc.txt");		//open a file
	cout<<"please insert the name of the game: "<<endl;
	cin>>name;
	file<<"name of the game: "<<name<<endl;   //write to it
	cout<<"please insert the game publisher: "<<endl;
	cin>>publisher;
	file<<"publisher: "<<publisher<<endl;
	cout<<"please insert the game developer: "<<endl;
	cin>>developer;
	file<<"developer: "<<developer<<endl;
	cout<<"please insert the game genre: "<<endl;
	cin>>genre;
	file<<"genre: "<<genre<<endl;
	cout<<"please insert the game release date: "<<endl;
	cin>>date;
file.close();

now my problem is that if the line that i put for the game variable is lets say world of warcraft so it doesn't go to the game varialbe but only the word "world" goes into it, and the words of and warcraft go into publisher and developer, respectivly. so my question is how do i make the whole sentence like "world of warcraft" go into one variable

0

ok tnks for the help in the above section but i got another piece of a code here that i am wondering why it is not reading the whole line.

ofstream file;

	file.open("gamepc.txt");		//open a file
	cout<<"please insert the name of the game: "<<endl;
	cin>>name;
	file<<"name of the game: "<<name<<endl;   //write to it
	cout<<"please insert the game publisher: "<<endl;
	cin>>publisher;
	file<<"publisher: "<<publisher<<endl;
	cout<<"please insert the game developer: "<<endl;
	cin>>developer;
	file<<"developer: "<<developer<<endl;
	cout<<"please insert the game genre: "<<endl;
	cin>>genre;
	file<<"genre: "<<genre<<endl;
	cout<<"please insert the game release date: "<<endl;
	cin>>date;
file.close();

now my problem is that if the line that i put for the game variable is lets say world of warcraft so it doesn't go to the game varialbe but only the word "world" goes into it, and the words of and warcraft go into publisher and developer, respectivly. so my question is how do i make the whole sentence like "world of warcraft" go into one variable

Reading a whole line is quite simple.Just use:

file.getline(char_buffer,size);

and also:

file.getline(char_buffer,size,terminating_char);

By char buffer I mean any char array ;) and by teminating character I mean any character and the befault one is '\n' which stands for new line.

Helps?

0

k lets say i got
struct movies{
int year;
char title[256];
char director[256];
char genere[256]
char actors[256];
};
movies entery;

how do i search inside the structure for a specifit title, year actors ect? lets say i type like 20 records inside a file using one strcutre. then i want to search the name lord of the rings as the title in all of those records and then if it finds it it ouputs all the titles with this name and then allows the user to choose one and edit it. how to do it?

0

Hey firenet
You had mentioned a way to delete a record from the file (using sequential access). I tried to do it with random access (i.e seekg and seekp). The program can't delete the last record. What do I do? :(

0

Hey firenet
You had mentioned a way to delete a record from the file (using sequential access). I tried to do it with random access (i.e seekg and seekp). The program can't delete the last record. What do I do? :(

Hey post the code, I will be able to better pinpoint the problem :)

0

this is a revised version of the example program ..
it runs properly ...but still got a few bugs to be fixed.

code:

#include <iostream.h>
#include <fstream.h>

struct  contact
{
	char name[10];
	int age;
};

struct address
{
	char city[10];
	char country[10];
};

class DtbRec
{
	private:
	contact cnt;
	address adr;
	
	public:
	void getdata();
	void dispdata();
};

/*
   I will give you a bit of work, make the funtions getdata() and dispdata()
   It's easy and not worth for me to bother with now :-}
*/
   
void DtbRec::getdata() //get user input
{
cout <<"Enter info(Name,age,city,country)"<<endl;
cin>>cnt.name;
cin>>cnt.age;
cin>>adr.city;
cin>>adr.country;

}

void DtbRec::dispdata()  //display to screen
{
cout<<cnt.name<<endl;
cout<<cnt.age<<endl;
cout<<adr.city<<endl;
cout<<adr.country<<endl;
}

//This program is not tested, have fun fixing errors, if any
//I am a taos programmer so dont expect anything major
//Typing mistakes are not errors
//This was done off the cuff and not even compiled once

int main()
{
	DtbRec xRec;		//temp rec
	fstream fl_h;	//file handle
	char ch;
	int num;
	
	fl_h.open("database.txt",ios::in|ios::out|ios::binary|ios::trunc);
	
	do
	{
		cout<<"\n\nFstream Dtb\n"
		      <<"\n1.Add records"
		      <<"\n2.View records"
		      <<"\n3.Modify records"
		      <<"\n4.Exit"
		      
		      <<"\n\tEnter Choice:";
		      
		cin>>ch;
		
		if(ch == '1')	//we are dealing with chars not ints
		{
			//Adding a rec
			
			fl_h.seekp(0,ios::end);	//will disscuss this later,this sets the file write pointer to
							//the end of a file.
			xRec.getdata();		//Get some data from the user
			
			fl_h.write((char*)&xRec,sizeof(DtbRec));
		    
            fl_h.seekg(0,ios::end);
			num=fl_h.tellg();
            int rec;
            rec=num/(sizeof(DtbRec));
            cout<<"No of recs="<<rec;
			
		}
		
        else if(ch == '2')
		{
			//View recs
			
			fl_h.seekg(0,ios::beg);	//will disscuss this later,this sets the file read pointer to the 
							//begining of a file.
		    int n = 0;
            fl_h.seekg(0,ios::beg);
			while(!fl_h.eof())		//will disscuss this later,it check if the file's end has been reached
			{
				n++;
				fl_h.read((char*)&xRec,sizeof(DtbRec));
				
				cout<<"\nRecord No["<<n<<"]\n";
				xRec.dispdata();	//Show the user all the data present
			}
		}
		
        else if(ch == '3')
		{    
			//Modify me colors ;-)
			
			cout<<"Enter the record no(starts at 0):";
			cin>>num;
			
			fl_h.seekg(num*sizeof(DtbRec),ios::beg);	//move the read pointer to where the rec is
			fl_h.read((char*)&xRec,sizeof(DtbRec));	//read it
			xRec.dispdata();					//Show the info
			xRec.getdata();					//Let the user change the info
			
			fl_h.seekp(num*sizeof(DtbRec),ios::beg);	//move the write ponter this time
			fl_h.write((char*)&xRec,sizeof(DtbRec));	//overwrite with new info
			
			//yahoo,modification done.I have seen too many people who just
			//cant get modification .It's so simiple I just cant get why they just cant
			//get it ;-)
		}
	    
      }while(ch != '4');
	
	fl_h.close();		//close the file
	cout<<"\nEnd of Program";
return 0;
}
0

Hi. I'm writing a pong game which loads and saves a playlist and a configuration file. Both files load properly, but the playlist wont save properly. I think the problem may have to do with the fact that it uses either Win32, openGL, or fmod, but im not sure which. When I write a console application, it runs fine. I use the visual c++ 6.0 standard edition compiler.

this works:

void SaveConfig()
{
	ofstream fout("tpong.cfg");											fout << volume <<endl;		//volume is an int		
	fout <<stream<<endl;    //stream is a bool
	fout <<maxPoints<<endl;  //maxPoints is an int
}

this doesn't work:

void SavePlaylist()
{
	ofstream fout("tpong.pl"); 
	fout<<numSongs<<endl; //numSongs is an int
	fout<<playlist.c_str()<<endl; //playlist is a string
}

both are called on after the other. Changing the order of calling them doesn't have any effect. After the program runs, and these functions are called, "tpong.cfg" is changed while "tpong.pl" isnt.

Any help would be much appreciated.

Edited by Nick Evan: n/a

0

you should probably update your tutorial to use current c++ standards -- The information is very very old and many of it obsolete. This makes it pretty difficult for new c++ programmers to weed through the chaff to get to the good stuff.

-1

you should probably update your tutorial to use current c++ standards -- The information is very very old and many of it obsolete. This makes it pretty difficult for new c++ programmers to weed through the chaff to get to the good stuff.

I agree and it's true. It is based on pre standard C++ file streams. When I wrote it, only the cutting edge people really used STL and coded according to standard c++ methods. Geesh, I did not even know what a template was at that time.

Only recently that the support for STL and standard C++ has become mainstream and found it's way into schools and other educational institutes.


Amazing is it not, education always gives you the old stuff and when you step out into the real world, you find so many new things and much more advanced concepts in practical use. Never be stuck with what you get in school. The net is the best place to get to know what goes on in the real world.

And me, well I was inactive here for a long time. I've travelled a lot further, learnt so much that I never even knew existed ..... and got a new monster of a comp ;)


Write an update?
Good idea, I will see what I can do :)

http://xlock.fusionxhost.com

0

Hi. I'm writing a pong game which loads and saves a playlist and a configuration file. Both files load properly, but the playlist wont save properly. I think the problem may have to do with the fact that it uses either Win32, openGL, or fmod, but im not sure which. When I write a console application, it runs fine. I use the visual c++ 6.0 standard edition compiler.

this works:

void SaveConfig()
{
ofstream fout("tpong.cfg"); fout << volume <<endl; //volume is an int
fout <<stream<<endl; //stream is a bool
fout <<maxPoints<<endl; //maxPoints is an int
}

this doesn't work:

void SavePlaylist()
{
ofstream fout("tpong.pl");
fout<<numSongs<<endl; //numSongs is an int
fout<<playlist.c_str()<<endl; //playlist is a string
}

both are called on after the other. Changing the order of calling them doesn't have any effect. After the program runs, and these functions are called, "tpong.cfg" is changed while "tpong.pl" isnt.

Any help would be much appreciated.

Have you tried deleting both the files and then running the program?

Or does your program open 'tpong.pl' elsewhere and does not close it?

Try checking with fout.good() to see if the file has been opened properly.

0

Ok I realize I'm bumping a four year old thread, but the reason is to clarify for anyone who get here by googleing for "fstream tutorial". The tutorial above is somewhat obsolete now -- such as using #include <fstream.h> should now be #include <fstream> without the .h extension.

The OP -- FireNet -- also posted a link to updated code -- that link no longer exists.

0

Can someone make a newer tutorial? None of the stuff in FireNet's tutorial will work with my compiler, I know it was probably pretty up to date when he wrote it, but Visual Studio 2008 doesn't accept the code :-(

0

im using the ios::nocreate in a fstream with vc++ 2008 express but its say its not a member of std::basic_ios...

0

im using the ios::nocreate in a fstream with vc++ 2008 express but its say its not a member of std::basic_ios...

That flag is not available anymore, so simply don't try to use it. (The same goes for ios::noreplace )

0

Hello!! I know this thread is old, but I have a problem regarding the program.. When I output the available records it displays as-> Name: _ty665 ryw@@#Eric blabla bla a bunch of gibberish

Same goes for Age, it displays the Name and age, etc but it also displays a bunch of gibberish in front :/ Suggestions bro?

0

include <iostream>

using namespace std;

int main()

int a, b, c, d, e;
int count=0;
cout<<"Enter 4 digit number"<<endl;
cin>>a;
while (a<1000||a>9999)

    cout<<"Enter 4 digit number"<<endl;
    cin>>a;

b=a/1000;
c=(a%1000)/100;
d=(a%100)/10;
e=a%10;

if(b==e && c==d)

    cout<<"It is a palindrome"<<endl;
    cout<<"Hexa: "<<hex<<a<<endl;
    cout<<"Octa: "<<oct<<a<<endl;
    for( ; a!=1; )

        int l=a%2;
        a=a/2;
        cout<<l;
        count++;

    cout<<a;
    count++;

    for(int i=count; i<32; i++)
        cout<<"0";


else

    cout<<"NOt a palindrome";

cout<<endl;

return 0;
0

It was wonderfull!Ofcourse I had problem in File but this discusion gave me new understanding!Thank you very much

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.