Reading an int in a text file

Started by
4 comments, last by X5-Programmer 18 years, 11 months ago
I was wondering how to read an integer from a text file. I know how to read a character from a text file, but was told that to read a integer it is different. I need to do this because I am making an rpg, and am trying to read a text file that says how much gold the person has. Can anyone help?
Advertisement
Reading in an integer is pretty much the same as reading in a character. For example, if you are using C++:

#include <ifstream>using namespace std;int main( int argc, char* argv[] ){   int gold;      ifstream IF;   IF.open( "yourfile.txt" );   if( !IF.is_open() )      return -1;   IF >> gold;   IF.close();   return 0;}


That will work if your file is like this:

// yourfile.txt
12345


Now the problem comes when there is other stuff, what does your file look like? There are many ways to accompish it as well, I'm sure you'll get lots of reccomendations [wink]
You are not giving many clues to how you you are doing it with chars but if you use an ifstream (input_) you can do the following.

int value;
input_ >> value;
For reading numbers from streams in general (this is especially a problem with cin, because it's normally used as interactive input for a console game, and it's line-buffered), I normally refer people here.

There's lots of other useful stuff there too. Just please don't use their ugly code with char*'s; use std::string instead :)
Similarly to the link Zahlman posted, I've found recently that it's much easier to read the text-file/stream into a std::string [since that is near fool-proof] and then convert the string [or part of the string] into a number. Just fewer things to go wrong that way...

[edit: Actually, that's not direct advice. It's kind of advice thrown out for folks more skilled than I to comment upon, since I've little idea if this is good advice, or merely better than what I would've given in the past]
Hi, Redtshirt.

Im at school for the moment but I wrote an example on how you can read your character data from an file.
I put the code here so you can play around with it.. Im not sure if its the best way to do like this^^
But I'll give it an try.

#include<iostream.h>#include<conio.h>#include<windows.h>#include<stdio.h>//Character Structurestruct pCharacter{	char m_Name[32];	int  m_Str;	int  m_Dex;	int  m_Vit;	int  m_Health;	int  m_Mana;	int  m_Gold;};//Load CharacterpCharacter LoadCharacter( char *pName ){	FILE*	   m_File;	int        m_Num = 0;	char       m_Buf[128];	pCharacter m_Character;	int Str=0,Dex=0,Vit=0,Health=0, Mana=0, Gold=0;    m_File = fopen( pName, "r" );	if ( m_File )	{		fgets( m_Buf, sizeof( m_Buf ), m_File );		sscanf( m_Buf, "[ %s ]", m_Character.m_Name );		short  m_Type=0;    		while ( !feof(m_File) )		{			fgets( m_Buf, sizeof( m_Buf ), m_File );			if ( sscanf( m_Buf, "Str: %i, Dex: %i, Vit: %i, Health: %i, Mana: %i, Gold: %i", 						 &Str, &Dex, &Vit, &Health, &Mana, &Gold ) )			{				m_Character.m_Str    = Str;				m_Character.m_Dex    = Dex;				m_Character.m_Vit    = Vit;				m_Character.m_Health = Health;				m_Character.m_Mana   = Mana;				m_Character.m_Gold   = Gold;					++m_Num;			}		}		fclose( m_File );	}	else	{		printf("Failed to open file");	}	return m_Character;}void main(){	pCharacter pChar;	pChar = LoadCharacter( "Roy.txt" );	printf( "Name   : %s\n", pChar.m_Name   );	printf( "Str    : %i\n", pChar.m_Str    );	printf( "Dex    : %i\n", pChar.m_Dex    );	printf( "Vit    : %i\n", pChar.m_Vit    );	printf( "Health : %i\n", pChar.m_Health );	printf( "Mana   : %i\n", pChar.m_Mana   );	printf( "Gold   : %i\n", pChar.m_Gold   );	getch();}


And basically this is the data I read from the file.

[ Roy ]
Str: 20, Dex: 25, Vit: 30, Health: 100, Mana: 100, Gold: 500
-[ thx ]-

This topic is closed to new replies.

Advertisement