This program is "C:\test\program.exe":

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int choice;
    
    cout<<" Which name to embrace?\n\n";
    cout<<" - 1. Fight Club\n";
    cout<<" - 2. Shawshank Redemption\n";
    cout<<" - 3. Italian Job\n";
    
    cin>> choice;
}

There's also a .txt file there named file.txt, "C:\test\file.txt\". Inside this .txt file is the following:

[Let us embrace those names!]
    
Fight Club =
Shawshank Redemption =
Italian Job =

Now. After "cin>> choice;" in the program, I want there to be code that uses fstream. The program is run and let's say
the user types in "1", referring to "Fight Club". The program will now go inside the .txt file, and put the number "1"
next to "Fight Club =". So, it would be: "Fight Club = 1".

If the program is exited and started again, it would ask the same question: "Which name to embrace," and if
the user again types "1", the program will go inside the .txt file, look for Fight Club and change the line from:
"Fight Club = 1" to "Fight Club = 2". So, each time the program is run and the user types in which name
to embrace, the program will raise the number that stands by the name. How is this done?

P.s I'm using CodeBlocks as a compiler and Windows XP Pro.

This is an example of a program which will always add the word 'something' at the end of the file:

#include <iostream>
#include <fstream>

using namespace std;

int main() 
{
	fstream filestr;
		
	filestr.open ("test.txt", fstream::app); 
        // 'app' stands for append
	filestr << "something\n";
	filestr.close();
		
       return 0;
}

You can also use this code with numbers ...

Hope this helps !

Oh, sorry I didn't read your question thoroughly ...

Thanks for the reply mate, but that's not exactly what I had in mind. If I use this program:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream myfile;

    myfile.open ("C:\\test.txt", fstream::app); // 'app' stands for append
    myfile << "something\n";
    myfile.close();
}

What I get is a program which constantly adds "something" into the file (as you referred). But, I was looking for a way I think it's called using "seek" to go to the line I want it to go, and once there to output information. Any help there? =)

You could also use structures which you read from the file, everytime the program runs, and write the back to the file when the program has finished ...

But you can also use seekg() ...

You could also use structures which you read from the file, everytime the program runs, and write the back to the file when the program has finished ...

But you can also use seekg() ...

You see the problem with using seekg is that when for example a name f.x. Fight Club goes to "Fight Club = 10", the amount of characters change in the line (they are 14 in "Fight Club = 1" and 15 in "Fight Club = 10"), so the seekg would be bugged when the amount adds an extra character :(

There must be a simple way to do this ?

There must be a simple way to do this ?

Yeah, sure, use structures !

Ok thanks, I'll look at a tutorial on structures, and ask if there are any other quesstions =)

Here's an example of what I mean:

#include <iostream>
#include <fstream>

using namespace std;

int main() 
{
	struct filestruct
	{
		int val_one, val_two, val_three;
	} test;
	
	test.val_one = 12;
	test.val_two = 13;
	test.val_three = 14;
	
	ofstream filestr;
	
	filestr.open ("test.txt");
	
	filestr << test.val_one << endl << test.val_two << endl << test.val_three;
	
	filestr.close();
	
    return 0;
}

This example is only showing you how to write the data to a file ...

You can use a similar method to read it back from the file (using an ifstream object instead of an ofstream object) ...

Is there a command such as seek that counts lines but not characters (So, it would tell the pointer to go to line number 6 f.x.) Is there also a command to select all data in a specific line?

To read a whole line from a file: std::getline(<filename>, <string>);

To count the number of lines in a file you can use the following function:

int countLines(ifstream & file, string filename)
{
	file.close();
	file.open(filename.c_str());
	string line;
	int c = 0;
	
	while(getline(file, line))
		c++; // :-)
	
	file.close();	
	return c;
}

(But you could also write such a function yourself)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.