5 Replies - 309 Views - Last Post: 02 March 2010 - 12:58 PM Rate Topic: -----

#1 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

updating textfile and reading from it

Posted 02 March 2010 - 08:15 AM

Hey guys this is my first post on this forum. Im glad to be here :)

Anyway i got two problems. I got a text file wich i want to change a special value in. It looks like this:

Quote

amx_godmode 1
amx_health 100


Now i want to change the value that comes after amx_godmode from '1' to '0'. I've came up with this code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool changeValue(string container[], string title, string value);

bool changeValue(string container[], string title, string value)
{
	bool result = false;
	for (int i = 0; i<4;i++) {
		if(container[i] == title) {
			container[i+1] = value;
			result = true;
		}
	}

	if (result == false) {
		cout << "\nYou didn't enter a valid title";
		return false;
	}

	else {
	ofstream fout("test.txt");

	for (int i = 0; i<4;i++) {
		fout << container[i] << " ";
		if (i != 0 && double(i)/2 != (int)i/2)
			fout << "\n";
	}
	fout.close();
	return true;
	}
}


void main()
{
	string st1[4];
	string tit;
	string val;

	ifstream fin("test.txt");
	char ch1[20];

	for (int i = 0; i<4;i++) {
		fin >> ch1;
		st1[i] = ch1;
	}


	cout << "These are the current values\n";
	for (int i = 0; i<4;i++) {
		cout << "*" << st1[i].c_str() << endl;
	}

	cout <<"\nPlease enter what you want to change <title> <value>\n";
	cin >> tit >> val;

	while (changeValue(st1, tit, val) == false)	{
		cout <<"\nPlease re-enter what you want to change <title> <value>\n";
		cin >> tit >> val;
	}


	cout << "\n\nThe values after update\n";
	for (int i = 0; i<4;i++) {
		cout << st1[i].c_str() << endl;
	}
	
	cout << "\n";
	//cout << "\n" << ch1 << "\n";
	system("PAUSE");
}



It does exactly what i want it to do, but i don't know if this is the best way of doing this. From the perspective of memory efficiency etcetera. I don't know how good it really is to save everything as strings, i might want to use the data for calculations and such in future. Anyway please leave your opinion about my solution and tell me if you know something more clever.

Hey again,
simpler :)

Is This A Good Question/Topic? 1
  • +

Replies To: updating textfile and reading from it

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: updating textfile and reading from it

Posted 02 March 2010 - 08:27 AM

Welcome to Dreamincode!

Personally, with a program of the size you posted, I would not worry too much about memory efficiency and optimisation. It is good though that you show concerns in this way.
Was This Post Helpful? 1
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: updating textfile and reading from it

Posted 02 March 2010 - 10:42 AM

An improvement would be to use "int main()" instead of "void main()", and remove the "changeValue" function prototype. Function prototypes are used when you want to implement a function below your main(), and you plan to use that function in main().
Was This Post Helpful? 1
  • +
  • -

#4 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: updating textfile and reading from it

Posted 02 March 2010 - 10:47 AM

View Postsarmanu, on 02 March 2010 - 09:42 AM, said:

An improvement would be to use "int main()" instead of "void main()", and remove the "changeValue" function prototype. Function prototypes are used when you want to implement a function below your main(), and you plan to use that function in main().


So the prototype isn't necessary if i put the function above main()? Thank for the tip! :)

EDIT: Why use int main()?

This post has been edited by simpler: 02 March 2010 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: updating textfile and reading from it

Posted 02 March 2010 - 10:53 AM

Yes, I can give you a very easy example:
#include <iostream> // for std::cout

// Uncomment this if you want the program to compile!
// int ret_val(int);

int main()
{
   int a = ret_val(31); // ERROR! the compiler can't find the function ret_val, as it is declared below the main() function
   std::cout << a << std::endl;
 
   return 0;
}

// function implementation. 
int ret_val(int a)
{
   return a;
}


See the comments.
Another thing that you need to know about function prototypes, is that you don't have to specify the name of the arguments! (not an error if you do though). Their type must be specified though. So, if I have a function taking arguments a float and an integer, I would prototype it like this:
<function_return_type> <func_name>(float, int); // notice, I only put the type of the arguments!


Be careful not to mismatch the type of the arguments from func. prototype, with the type of the arguments from func. implementation!

Why us int main()?
Read here: http://www.gidnetwork.com/b-66.html

This post has been edited by sarmanu: 02 March 2010 - 10:54 AM

Was This Post Helpful? 1
  • +
  • -

#6 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: updating textfile and reading from it

Posted 02 March 2010 - 12:58 PM

Thanks, that explained things clearly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1