Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

getting data from a txt file

I have a very basic game that I am working on and want to use a read only text file to save basic dat (character name, stats etc) I can make the file as I want but cannot figure out how to get said data back out.
                                      -Splint
0
Splint
Asked:
Splint
  • 2
  • 2
1 Solution
 
ExceterCommented:
>> use a read only text file to save basic dat

That is a contradiction in terms. :-)

You can retrieve values from a text file as follows,

char name[25];
int level, points;

ifstream in;
in.open( "data.txt", ios::in );

while( in )
{
    in >> name >> level >> points;
}

in.close();

cout << name << " died on level " << level << " with " << points << "!" << endl;

-- Sample input file --

Sam 23 2345
Greg 12 1234

-- end of file --

You should recieve the following output,

Sam died on level 23 with 2345 points!
Greg died on level 12 with 1234 points!

Exceter
0
 
boludazoCommented:
Yes, or use the old C stuff:

#include <stdio.h>

main()
{
FILE *inputfile;
int i = 200;
char read[200];
char filename[] = "c:\\myfile.txt";

if ( (inputfile = fopen(filename, "r") ) != NULL)
{

 while (fgets(read,i,inputfile))
        {
           
        cout >> read; //This while statement is for printing every line of the file.      
           
        }
}
}
0
 
boludazoCommented:
Yes, or use the old C stuff:

#include <stdio.h>

main()
{
FILE *inputfile;
int i = 200;
char read[200];
char filename[] = "c:\\myfile.txt";

if ( (inputfile = fopen(filename, "r") ) != NULL)
{

 while (fgets(read,i,inputfile))
        {
           
        cout >> read; //This while statement is for printing every line of the file.      
           
        }
}
}
0
 
ExceterCommented:
>> Yes, or use the old C stuff:

This is the C++ topic area not the C topic area. :-)
0
 
SplintAuthor Commented:
Alright Thanks a lot, I'll look into it

ps: I say read only because I make the file and then delete it when new data is added. read only will prevend at least the most passive of tampering
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now