Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Need help on reading from files

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Thu Jun 05, 2008 5:09 am    Post subject: Need help on reading from files Reply with quote

So far i got this code:

Code:
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib>

int main()
{
   ifstream indata;
   int num;
  indata.open("C:\\Documents and Settings\\KPO\\Dokumenter\\Visual Studio 2008\\Projects\\readfromfile\\Debug\\example.evt"); // opens the file
   if(!indata) {
      cerr << "Error: file could not be opened" << endl;
     std::cin >> num;
      exit(1);
   }
   char word[99];

   indata.getline(word, 10, '\n');
   std::cout << word << std::endl;

   indata.close();
    std::cin >> num;
   return 0;
}


and what i wish to do, is to repeat this part:

Code:
   indata.getline(word, 10, '\n');
   std::cout << word << std::endl;


Untill there is no more lines in the file, how would i do this?
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Jun 05, 2008 5:18 am    Post subject: Reply with quote

Code:
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdlib>

int main()
{
   ifstream indata;
   int num;
  indata.open("C:\\Documents and Settings\\KPO\\Dokumenter\\Visual Studio 2008\\Projects\\readfromfile\\Debug\\example.evt"); // opens the file
   if(!indata) {
      cerr << "Error: file could not be opened" << endl;
     std::cin >> num;
      exit(1);
   }
   char word[99];


while(!indata::eof()){
   indata.getline(word, 10, '\n');
   std::cout << word << std::endl;
}



   indata.close();
    std::cin >> num;
   return 0;
}

_________________
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Thu Jun 05, 2008 5:58 am    Post subject: Reply with quote

Wow, ty so much! Razz

but you just made one error... its indata.eof and not indata::eof Razz

edit: Now it doesent write anything at all O.o, actually it just freezes O.o...
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Jun 05, 2008 10:59 am    Post subject: Reply with quote

http://www.cplusplus.com/doc/tutorial/files.html

Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  return 0;
}


So check to see if myfile is open and if it is and it isn't the end of the file get a line.

_________________
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Thu Jun 05, 2008 4:01 pm    Post subject: Reply with quote

Thanks, the link you gave me was good Razz, ill try it out tomorrow! Razz
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8588
Location: 127.0.0.1

PostPosted: Thu Jun 05, 2008 5:41 pm    Post subject: Reply with quote

Chopped up version using TinyXML's method with fopen:

Code:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int main( int argc, TCHAR* argcv[] )
{
   // Open File For Reading
   FILE* fFile = 0;
   errno_t errNum = fopen_s( &fFile, _T("test.txt"), _T("rb") );
   if( (errNum != 0) || (fFile == 0) )
      return 0;

   // Get File Size
   long lLength = 0;
   fseek( fFile, 0, SEEK_END );
   lLength = ftell( fFile );
   fseek( fFile, 0, SEEK_SET );

   // Allocate Buffer If File Is Not Empty
   if( lLength <= 0 )
   {
      // Empty
      fclose( fFile );
      return 0;
   }

   char* buf = new char[ lLength + 1 ];
   
   // Read File Contents
   if( fread( buf, lLength, 1, fFile ) != 1 )
   {
      // Failed..
      delete[] buf;
      return 0;
   }

   // Set Null Char To Terminate String
   buf[ lLength ] = '\0';

   // Output Buffer
   printf( buf );

   // Delete Buffer And Cleanup   
   delete[] buf;
   fclose( fFile );

   getchar(); // Pause Without iostream
   return 0;
}

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites