Sign in to follow this  
Ajain

C++...getting info back out of files...

Recommended Posts

The part of my brain that isn't liquified from day-old chinese food, staring at computer screens for hours and gaming in a dark room, and watching TV is about to tear itself apart because I can't get this! Right...first I'm making a small text based game...I mean small...you make a charachter in one file and then you acess it w/ another one in which you fight w/ either punch, kick, defend, or run...right...enough about that... my question is how to read each of the stats. because this isn't hardcore I decided to put the stats and whatnot in one file...a .txt file...in order to test taking things outa these files I made another file...i put in an "a"...then I'm gonna try to get it onto the screen... here is my code:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

using namespace std;
char letter[2];

int main()
{
  ifstream fin("char1.txt");
  fin >> letter;
  cout << "\n\n\n\t\t\t" << letter << "\n\n\n\t\t";
  system("PAUSE");
  return 0;
}
where am I going wrong? thanks for ur time... -Ajain

Share this post


Link to post
Share on other sites
Okay I'm not that great at this right now, but First I believe you are declaring your ifstream part wrong. I was taught that you need to use ios::in or ios::out depending on what you wish to do. I know I havn't been that much help but let me get back on some other stuff, I don't remember using fin either :)

There we go, found my ICA that has this in it :)

Okay I would use



#include <iostream>
#include <fstream>

using namespace std;

int main()
{
// Was taught to try not declare anything outside of main if its possible
char letter[2];

fstream DataFile("Char1.txt", ios::in); // opens file for read in
if (DataFile.fail()) { // Checks to see if it was able to open file
cout << "File could not be opened or is missing";
cout << "\nPlease make sure file is in proper path\n";
return(0);
}

while( !DataFile.eof( ) )
{
for(int i=0; i<2; i++) { // reads the letters into the array
DataFile >> letter[i];
}
}

cout << "\n\n\n\t\t\t" << letter << "\n\n\n\t\t";
system("PAUSE");
return 0;
}



Share this post


Link to post
Share on other sites
Quote:
I was taught that you need to use ios::in or ios::out depending on what you wish to do.

that is only necessary if you're using an fstream object. ifstream is, by definition, an input stream.

if you only put in one "a" in the text file, then only 1 char is needed to retrieve it, not a char array of [2]. so you'd have :

char letter; // not char letter[2];
int main()
{
/* init file ... */
fin >> letter;
/* output to screen ... */
return 0;
}

Share this post


Link to post
Share on other sites
none of that is working...

and about accessing it, I have multiple variables on multiple lines (yes, even a name) so how do I handle that? (is that the for (...) thing?)...

and about being selective of data once it comes out...

:/

Thanks for ur help but...is there a tutorial (already saw the gamedev one) out there that goes into detail and treats the reader like an inatimate object (in the way that it talks so slowly at a turtle's rate that it could cause flies to drop from the air)?

Thanks for ur time, though...

-Ajain

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this