Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    2. News

    1. Red Dead Redemption 2

      1. News
      2. Red Dead Online
    1. GTA Online

      1. After Hours
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption

    2. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

ClareXoBearrx3R9

C++ getline()

Recommended Posts

ClareXoBearrx3R9

Has anyone here had luck or know if it's impossible to use

 

 

getline

such as:

 

 

 

 

std::getline(myfile, line)

For coding with the C++ ScriptHook? The last time I used it, it always crashed the game. I verified correct usage and tested on a separate standard console project with no problems but GTA didn't seem to like it.

 

Thanks :)

Share this post


Link to post
Share on other sites
cpp

I don't know if it's impossible to use getline() but Aru uses a different method to read (config) files in the speedometer example. You should take a look at it, especially Settings.h and Settings.cpp.

Edited by cpp

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

I see now. It certainly uses a way I never have seen before, so I'll try that it all else fails.

 

Thanks for the info! :)

Share this post


Link to post
Share on other sites
Skorpro

 

std::getline(myfile, line)
For coding with the C++ ScriptHook? The last time I used it, it always crashed the game. I verified correct usage and tested on a separate standard console project with no problems but GTA didn't seem to like it.

Think this is better than getline... but you decide ;)

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

 

 

std::getline(myfile, line)
For coding with the C++ ScriptHook? The last time I used it, it always crashed the game. I verified correct usage and tested on a separate standard console project with no problems but GTA didn't seem to like it.

Think this is better than getline... but you decide ;)

 

 

Beautiful - thanks so much!!! :D

 

I'll try those instead. Thanks again! :)

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

Just to update this thread one last time...

getline() does indeed work, so perhaps the previous time I had a problem somewhere else.

Unfortunately, I am performing file I/O to read in a bunch of data, not stuff that can be organized into an .INI file. So, my final go-to was getline().

Anyhow, since everyone is so friendly with helping each each other out, here's my written example of how to use getline() with the C++ ScriptHook SDK. Note however that while() loops should be avoided and there is a way to get around it but still use getline(). But I'll leave you, the programmer, to figure it out. ;)

 

Using getline():

std::string fileName = "myGTAfile.txt";std::string in;std::ifstream* myFilep = new std::ifstream(fileName.c_str(), std::ios_base::in);if (!myFilep->is_open()) return 1;while(getline(*myFilep ,in)){	// ...	// ...}return 0;

Enjoy. ;)

Edited by ClareXoBearrx3

Share this post


Link to post
Share on other sites
Skorpro

You made it :) Don't know that you want to read a lot of data from a file... Here is my way to do this (include getline() ;)).

 

C ya

Edited by Skorpro

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

Thanks again, Skorpro :)

 

I actually needed to get a bunch of text data that the user can use, which is the reason I needed to use getline(). Thanks again! :)

Share this post


Link to post
Share on other sites
3dplanet

Just to update this thread one last time...

 

getline() does indeed work, so perhaps the previous time I had a problem somewhere else.

 

Unfortunately, I am performing file I/O to read in a bunch of data, not stuff that can be organized into an .INI file. So, my final go-to was getline().

 

Anyhow, since everyone is so friendly with helping each each other out, here's my written example of how to use getline() with the C++ ScriptHook SDK. Note however that while() loops should be avoided and there is a way to get around it but still use getline(). But I'll leave you, the programmer, to figure it out. ;)

 

Using getline():

std::string fileName = "myGTAfile.txt";std::string in;std::ifstream* myFilep = new std::ifstream(fileName.c_str(), std::ios_base::in);if (!myFilep->is_open()) return 1;while(getline(*myFilep ,in)){	// ...	// ...}return 0;

Enjoy. ;)

I'm trying to read data from file using getline() too. I want to store data in an array of string, but scripthook is craching.

I've tried your code and scripthook keep craching.

How to you manage data extracted with your getline() code?

Edited by 3dplanet

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

I ended up using a really crappy algorithm that I just made up on the spot since I had some issues as well. I'll post it as soon as I get out of work and get home. :)

Share this post


Link to post
Share on other sites
ClareXoBearrx3R9

Here's my code, as promised, in all its glory. lol.

 

It's pretty generic, just a warning. It reads the file "my_file.txt" into the data vector (data_vector) which is of type string.

 

The file itself must be in the following format:

line1,line2,line3,line4,and so on...,<END_OF_FILE>

where each line ends with a comma. I did this back then because I wanted a unique identifier in case the line didn't have a line return ('\n') for whatever reason. If you don't care for that, you can remove that requirement of course. Anyhow, it basically reads line by line until it reaches the line that contains the end_data string. Of course, if you need to read more than 900,000 lines, adjust that value accordingly. :p

 

Here it is, adjust to your liking. Hope it helps. :)

vector<std::string> file_data;int read_data_file(){    int i;    bool atEOF;    std::string file_name = "my_file.txt", in_data, in_data_blank = "";    const char data_end[] = "<END_OF_FILE>";    std::ifstream* filep = new std::ifstream(file_name.c_str(), std::ios_base::in);    file_data.resize(0); // Make sure it's empty at first.    if (!filep->is_open()) return 1; // Failed to open file.    for (i = 0; i < 900000; i++){ // Yeah... whatever.        getline(*filep, in_data);        // Check if at end of file.        if (strcmp(in_data.c_str(), data_end) != 0) atEOF = false;                else{                        atEOF = true;            filep_filep->close();            delete[] filep;            // Success; finished reading file.            return 0;        }        // Insert data into the data vector.        if (!atEOF){            in_data = in_data.substr(0, in_data.size() - 1); // Remove comma            file_data.push_back(in_data);            in_data = ""; // Reset the contents because... YOLO.        }    }    return 0;}
Edited by ClareXoBearrx3

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

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.