Thread: data from a file

  1. #1
    Authorized Technician
    Join Date
    Jul 2004
    Posts
    11

    data from a file

    Hello, I'm a semi-experienced programmer... just not to C++. I've used QBasic (please don't laugh at me)... and I'm ready to use a real language. Using files isn't hard... but what I want to do and what I know how to do are two different things. So...

    Code:
    #include <iostream>    //cout stream, so you can see
    #include <stdlib.h>    //rand() function
    #include <ctime>       //time() function -for srand()
    #include <fstream.h>   //files
    #include "windows.h"   //accepting keys without pressing enter (much nicer)
    bool exitprog = false;
    int main(int argc, char *argv[])
    {
      cls();
     cout << "John's battle game... very beta\nFilename:";
     int str, def, spd, hp, gold;
     char buffer[32], bufferb[32], bufferc[32];
     cin >> buffer;
     ifstream filein(buffer, ios::binary);
     if (filein.eof)
     {
      filein.close();
      ifstream fileout(buffer, ios::binary);
      cout << "\nThis file is not yet initialized. Character name?\n";
      cin>>bufferb;
      for (int x=0; x<31; x++)
      {                                       //I'm so terribly sorry...
       fileout << bufferb[x];                 //my braces are a nightmare to read.
      }                                       
      fileout << 7 << 7 << 7 << 25 << 10;
      fileout.close();
      filein.open(buffer, ios::binary);
     }
    for (int y=0; y<31; y++)
    {
     filein >> bufferc[x];
    }
    filein >> str >> def >> spd >>hp >> gold;
    
    //It's time: The do...while. This goes until it's time to end.
    do
    {
     exitprog = true;  //lazy functstubbing developers!!!
    }while(!exitprog)  
     
     //file save code...
     //Debug information. If any needed.
     
     system("pause");  // <- remove this (developer only)
    
     return 0; //ah, the end. My program is done.
    }
    exitprog is a global boolean used by a function I haven't made yet and main. This is a RPG, and I've worked out the [psuedo]random numbers and taking keys (that's why windows.h is in there, though I hate importability). My problem is that I don't know how to take a variable out of a file. I hate to do it, but I'll put a string header in and make the file text format... but that would really be a pity, it's a game, and that would make it MAJOR hackable.
    It's not finished but I think it's stubbed up pretty well and should run... all my errors are contained in the code posted. I am using functions posted as tutorials, but I'm not marketing, this will only go around my house.
    and vets- don't let me go around thinking I know what's going on... I figure I got this under control and if I don't, straighten me out before I embarass myself. Thanks.
    Behind me lies another fallen soldier...

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    posting your errors would be helpful, but I'll point out some things that seem shady to me
    Code:
    cin >> buffer;
    since these are char arrays, you may want to try out cin.getline() instead.
    Code:
    if (filein.eof)
    I think it might be filein.eof()
    Code:
    ifstream fileout(buffer, ios::binary);
    ...
    fileout << 7 << 7 << 7 << 25 << 10;
    an ifstream objects name fileout seems fishy, too. ofstream is what you want.
    Code:
    for (int y=0; y<31; y++)
    {
     filein >> bufferc[x];
    }
    filein >> str >> def >> spd >>hp >> gold;
    You send data to the file....then immediatley get it back from the file? First off, that's more work than what's needed. You've already got the name they wanted in bufferb, and the stats can be set to your hardcoded values. Also, your file pointers may be all over the place.

    edit:
    Code:
    system("pause");
    tsk, tsk.

  3. #3
    Authorized Technician
    Join Date
    Jul 2004
    Posts
    11
    ouch, some of that looks pretty bad, I'll fix it and get back... But the point of writing and then loading is this:

    if I open the file and it has reached EOF (blank file), then I write the default statistics close, and reopen it.

    I load (starting from the begining) the variables. This way, if the file started blank or it was a legit file, I'm reading information (default or modified).

    Now that I think about it, I could make this into a class and maybe save myself some trouble. But for now let me just make it work. Thanks for pointing that stuff out though, compiler errors just aren't in the right language.
    Behind me lies another fallen soldier...

  4. #4
    Authorized Technician
    Join Date
    Jul 2004
    Posts
    11
    Fixed the code. Errors dropped from 8 to 1. The line:

    Code:
    if filein.eof()
    compiler says,
    Code:
    Parse error before '.'
    Behind me lies another fallen soldier...

  5. #5
    Authorized Technician
    Join Date
    Jul 2004
    Posts
    11
    Got it to compile.

    Code:
      //Compiler: Dev-C++ bloodshed (I love that name) v4.01 
    
    //preprocessor
    #include <iostream>    
    #include <stdlib.h>    
    #include <ctime>       
    #include <fstream.h>   
    #include "windows.h"   
    
    //function prototypes   
       int GetRand(int min, int max);
       bool keyHit(void);
       bool getChar(TCHAR &ch);
       TCHAR getChar(void);
       void cls(void);
    
    //global(s)
       bool exitprog = false;
       
    
    int main(int argc, char *argv[])
    {
      cls();
     cout << "John's battle game... very beta\nFilename:";
     int str=0, def=0, spd=0, hp=0, gold=0;
     char buffer[32], bufferb[32], bufferc[32];
     cin >> buffer;
     ifstream filein(buffer, ios::binary);
     if(!filein.eof())
     {
        filein.close();
        ofstream fileout(buffer, ios::binary);
        cout << "\nThis file is not yet initialized. Character name?\n";
        cin>>bufferb;
        for (int x=0; x<31; x++)
        {                                       //I'm so terribly sorry...
           fileout << bufferb[x];              //my braces are a nightmare to read.
        }                                       
        fileout << 7 << 7 << 7 << 25 << 10;
        fileout.close();
        filein.open(buffer, ios::binary);
     }
    for (int y=0; y<31; y++)
    {
     filein >> bufferc[y];
    }
    filein >> str >> def >> spd >>hp >> gold;
    
    do
    {
     exitprog = true;  
    }while(!exitprog);  
     
     //file save code...
     cout << str << def << spd << hp << gold; 
     
     system("pause");  
    
     return 0; 
    }
    The program returns 0 0 0 0 0 (before it was returning insane values). file.eof() keeps returning false (so if(!file.eof()) keeps executing, even with real files). Fake file names are created, but are 0 bytes in size.
    Behind me lies another fallen soldier...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM