954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

well thanlks to N3wbi3C0d3r i am glad to knoiw that thete are others like me on this web. keep it up. :) ;) :lol: :)

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 
if you output abc123 to a text file you will see abc123 but in a binary file you may see only a bunch of black blocks if you use notepad

are you confusing this with acutally writing hex ab c1 and 23 in binary? that will give strange chars. how would you do this in the code? as said before this quote was proved to be wrong so how can you actually write 0xab 0xc1 0x23 across 3 bytes???
is it just a cast?

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 
am doing it without any funds and using a free web tutorial worth crap

i no how u feel!!! i am self taught, learned by copying code from the help file in QBasic and lookin @ how it worked! having said that i am at a better standard in c++ vb and asm lang than other people i know doing A-levels in the specified subject so hold out as there are some good tuts out there. I have managed to create a few games and even worked as a software developer designing drivers and GUIs in vb just using tutorials and practising a LOT. good job too as part of my engineering masters degree is computer programming and they are using the same IDE as me (DevC++) :) btw i forgot to say last time thank you to 'FireNet' for the fstream tutorial. Ive been looking for a good one for a while. is there any advantage into using the Win32 API (FILE) file handling? the win32 help is a bit patchy on the subject.....

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

HeHe, im still running into some toruble learning, casue the tutorial doesnt assign home work, and im bad at creating homework on stuff i dun now, so i have to ask one of my 3 freinds who are Programming Wizards, and occasionaly here. I might soon just give up trying to do it by the internet and use a book ;p Altho i dun have the money, ill save up!

N3wbi3C0d3r
Junior Poster in Training
67 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

this one was more intersting. keep it up. looking forward for the next part

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

i no how u feel!!! i am self taught, learned by copying code from the help file in QBasic and lookin @ how it worked! having said that i am at a better standard in c++ vb and asm lang than other people i know doing A-levels in the specified subject so hold out as there are some good tuts out there. I have managed to create a few games and even worked as a software developer designing drivers and GUIs in vb just using tutorials and practising a LOT. good job too as part of my engineering masters degree is computer programming and they are using the same IDE as me (DevC++) :)

btw i forgot to say last time thank you to 'FireNet' for the fstream tutorial. Ive been looking for a good one for a while. is there any advantage into using the Win32 API (FILE) file handling? the win32 help is a bit patchy on the subject.....

You are welcome.

Advantages of Win32 API (FILE) file handling, are you kidding,it sucks.... well almost.One of the major problem with it is that it is platform dependent and uses C style functions.

Try this link, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_file.2d.handling_tasks.asp

Using standard library functions are always better than using API stuff,unless you are doning something really specific, since std library code is cross-platform and will work on any OS which has the compiler.

this one was more intersting. keep it up. looking forward for the next part

Huh well there is no next part as of such, ;) ,it's just that darn message lying at the top.I can remove it since,we cant edit or posts after half an hour.

But Just wait till I get my site up properly...

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

What about file compression techniques like RLE? i know a bit but if you can do a tut and some code on that im sure we'd all appreciate it... :)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

FireNet, this really is a good tutorial.

Here is my problem. I want to read in a file, the file might have x amount of lines, but on each line I would like to read in each substring, which is separated by spaces, and manipulate each.

For example. I would like to read a line which has "A2 3 G4 H5 L4" where A2 is the name of the node, so that needs to be stored in my structure as char type in variable NodeName, plus the number after the initial NodeName, 3 in this case, is the amount of nodes following it, so my program should know how many more nodes to check for. Also, a node can be named A2b or G4h or something, it stops reading the node name when it comes on a space. What is the easiest to go about doing this ?

marcamillion
Newbie Poster
1 post since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Have a line at the beginning specifying the number of lines. another alternative is to get the total filesize and divide it by the size of a line... You can then get lines by reading characters and stopping at "\n" chars. There also is a standard c function getline but i dont know how to use it :)

As for the sub strings if you have the line in a character string:

char *line;
char x;
int i = 0;


do
{
    x = line[i];
    if(x == ' ')
    {
        // you have hit a space, the characters before are a substring
    }
    i++; // scan across the line
} while (x != "\n"); // dont overrun the line


im sure it would be easier with a lot more thought, @ the moment its all i can think of. If you add another variable to track where you found the last space you can 'map out' where there are words in a string and also copy them across into another array of sub strings... :)

post what code you have so far including the structs and the fstream code and one of us should be able to help

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

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?

johnnyrico
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 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?

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

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();

}
johnnyrico
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 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();

}
FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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

johnnyrico
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 
gfunkllg
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 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?

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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?

johnnyrico
Newbie Poster
4 posts since Jan 2005
Reputation Points: 10
Solved Threads: 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? :(

iamboredguy
Newbie Poster
23 posts since Aug 2004
Reputation Points: 12
Solved Threads: 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 :)

FireNet
Posting Whiz in Training
258 posts since May 2004
Reputation Points: 108
Solved Threads: 7
 

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;
}
rghai6
Newbie Poster
14 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You