save in file

Discussion about everything. New games, 3d math, development tips...
Post Reply
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

save in file

Post by VeneX »

I have had some troubles with saving in a file, my code is:

Code: Select all

char ch;
filename += ".save";
fstream savefile;
savefile.open(filename.c_str(), ios::out);
int i2 = text_save.length(); //the editor sais this couldn't be done :?
for (int i = -1; i < i2; i++);
{	ch = text_save[i]; //here should be a fault too
	savefile.put(ch);
}
conversion from 'size_t' to 'int', possible loss of data

I want to convert a string to an old c-string... But how I tried doesn't work and text_save.c_str(); doesn't work either when I use it, please can someone help me?

Thanx in advance
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

Post by warui »

Try:

Code: Select all

size_t i2 = text_save.length();
'the gnu c library' wrote: Data Type: size_t

This is an unsigned integer type used to represent the sizes of objects. The result of the sizeof operator is of this type, and functions such as malloc (see Unconstrained Allocation) and memcpy (see Copying and Concatenation) accept arguments of this type to specify object sizes.

Usage Note: size_t is the preferred way to declare any arguments or variables that hold the size of an object.

In the GNU system size_t is equivalent to either unsigned int or unsigned long int. These types have identical properties on the GNU system and, for most purposes, you can use them interchangeably. However, they are distinct as data types, which makes a difference in certain contexts.

For example, when you specify the type of a function argument in a function prototype, it makes a difference which one you use. If the system header files declare malloc with an argument of type size_t and you declare malloc with an argument of type unsigned int, you will get a compilation error if size_t happens to be unsigned long int on your system. To avoid any possibility of error, when a function argument or value is supposed to have type size_t, never declare its type in any other way.
And one more thing. Don't start you loop from -1, because this way it will try to read for a -1 place of string which will crash your program for 99%.
Tomasz Nowakowski
Openoko - www.openoko.pl
Guest

Post by Guest »

It works now, but a 'space' doesn't work with char, het writes only the first word, how to solve this, but still using char?
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

guest was me
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post by Endar »

The way I'm reading that is that when you get input from the user, only the first word that is typed is put into the string...... right??

Well, my help is based on that.

When you get input, different functions do it different ways. Eg. Std C function 'scanf' , when specified to input a string, it only takes the first word if any more are input, because it ignores everything past a whitespace (tab, space, newline).

There are different functions in Std C (only making example here, go look for some C++ ones, or you could use these) that are used for input, like scanf, but take everything including whitespaces, so, for the typed string "hello how are you today?", 'scanf' would only get "hello", but another function, like 'fgets' would get "hello how are you today?" but with a newline at the end.

So, just look around, there could be some in the irrlicht API, or probably just go looking in the C++ help for "input string functions" or something like that.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

Post by warui »

Post some more code, with text_save definition.
Tomasz Nowakowski
Openoko - www.openoko.pl
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

I want to put it in c string because I want to decode the input:

Code: Select all

// save.cpp : Defines the entry point for the console application.
#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string readfile(string filename)
{	char ch;
	filename += ".save";
	string text;
	fstream savefile;
	savefile.open(filename.c_str(), ios::in);
	if (!savefile)
		return "File does not exist";
	while (savefile.get(ch))
		text += (ch + 10);
	return text;
}

void writefile(string filename, string text_save)
{	char ch;
	filename += ".save";
	fstream savefile;
	savefile.open(filename.c_str(), ios::out);
	size_t i2 = text_save.length();
	for (unsigned int i = 0; i < i2; i++)
	{	ch = (text_save[i] - 10);
		savefile.put(ch);
	}
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{	string filename;
	string text_save;
	string text_opened;
	int x;
	cout << "1 to open file.\n2 to save file\n: ";
	cin >> x;
	if (x == 1)
	{	cout << "filename: ";
		cin >> filename;
		text_opened = readfile(filename);
		cout << text_opened;
	}
	if (x == 2)
	{	cout << "save game as: ";
		cin >> filename;
		cout << "\n\nWhat do you want to save?\n: ";
		cin >> text_save;
		writefile(filename, text_save);
	}
	return 0;
}
At this time it works, but the rest after a 'white' space won't be written in the file :(
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

BTW. cout and cin doesn't seem to work in a .NET app :( :(
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

Post by warui »

OK, the problem is with cin. Try to use filename with space like "name of a file" ;) and you'll see what's wrong. But i don't know how to help you other then using something diffrent to read from keyboard.
Tomasz Nowakowski
Openoko - www.openoko.pl
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

cin.get(buffer, 80); where buffer is a string (std::string or const char*)
The Robomaniac
Project Head / Lead Programmer
Centaur Force
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

the problem isnt cin but filename.c_str() when using space in the filename'

I don't mean the get, but the put().... het writes only one word in the file. Using the cin will result in a string (include words after white space) that string will be converted to a c string witch result in a stop after the first wite space
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
Post Reply