?
Solved

getting data from a txt file

Posted on 2003-02-20
5
Medium Priority
?
175 Views
Last Modified: 2010-04-02
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
Comment
Question by:Splint
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 7988474
>> 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
 

Expert Comment

by:boludazo
ID: 7988862
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
 

Expert Comment

by:boludazo
ID: 7988863
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
 
LVL 8

Expert Comment

by:Exceter
ID: 7990340
>> Yes, or use the old C stuff:

This is the C++ topic area not the C topic area. :-)
0
 

Author Comment

by:Splint
ID: 8010283
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question