Thread: AI

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I just need help with seekg().
    So go look it up! !

    Or Say what the particular problem you are having with it is.. you know.. explain the problem you are having, in a more informative way - If you just want to know the basic usage of seekg() then that is exactly what documentation - like c++ reference is for! Then you can come back and say things like -

    "what is wrong with my arg to seekg? "

    "why do i get this warning from the compiler? the code compiles but the function call shown seems to produce the warning"

    " why is this not compiling, i get the error shown but according to the docs this should be valid, am i missing something? "

    I get a crash with the following error when i run the program, in the debugger it is on the seekg call , see code below, where am i going wrong?"

    things like that will get you better replies
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by rogster001 View Post
    So go look it up! !

    Or Say what the particular problem you are having with it is.. you know.. explain the problem you are having, in a more informative way - If you just want to know the basic usage of seekg() then that is exactly what documentation - like c++ reference is for! Then you can come back and say things like -

    "what is wrong with my arg to seekg? "

    "why do i get this warning from the compiler? the code compiles but the function call shown seems to produce the warning"

    " why is this not compiling, i get the error shown but according to the docs this should be valid, am i missing something? "

    I get a crash with the following error when i run the program, in the debugger it is on the seekg call , see code below, where am i going wrong?"

    things like that will get you better replies
    post#3 line 89 - 103. Why does the program not output the string of user with seekg()?
    Last edited by kiknwing; 04-19-2013 at 03:10 PM.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by kiknwing View Post
    post#3 line 89 - 103. Why will does the program not output the string of user with seekg()?
    As I said, you are using saving a memory address to the file. I already explained why that is a wrong approach.

    However, there is nothing wrong with saving strings to files and then retrieving them, e.g.
    Code:
    #include <fstream>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    class Member
    {
       private:
       std::fstream file;
       std::string user;
    
       public:
       void setUser();
       std::string getUser() const;
       bool saveUser();
       bool loadUser();
    };
    
    void Member::setUser()
    {
       std::cout << "Enter user name: ";
       std::cin >> user;
       if (!std::cin)
       {
          throw std::runtime_error("Set user failed!!\n");
       }
    }
    
    std::string Member::getUser() const
    {
       return user;
    }
    
    bool Member::saveUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::app | std::fstream::out);
    
       if ( !file.is_open()) {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file << user << std::endl;
    
       if (file) {
          result = true;
       }
    
       file.close();
       return result;
    }
    
    bool Member::loadUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::in);
    
       if ( !file.is_open())
       {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file >> user;
    
       if (file)
       {
          result = true;
       }
       file.close();
       return result;
    }
    
    int main()
    {
       Member member;
       member.setUser();
       if ( !member.saveUser())
       {
          std::cerr << "Failed to save the user (broken file) !!\n";
          return 0;
       }
    
       if ( !member.loadUser())
       {
          std::cerr << "Failed to load the user (record.txt: no content) !!\n";
          return 0;
       }
    
       std::cout << "User retrieved from file is " << member.getUser() << std::endl;
       return 0;
    }
    You can see that this at least retrieves something from a file.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    As I said, you are using saving a memory address to the file. I already explained why that is a wrong approach.

    However, there is nothing wrong with saving strings to files and then retrieving them, e.g.
    Code:
    #include <fstream>
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    class Member
    {
       private:
       std::fstream file;
       std::string user;
    
       public:
       void setUser();
       std::string getUser() const;
       bool saveUser();
       bool loadUser();
    };
    
    void Member::setUser()
    {
       std::cout << "Enter user name: ";
       std::cin >> user;
       if (!std::cin)
       {
          throw std::runtime_error("Set user failed!!\n");
       }
    }
    
    std::string Member::getUser() const
    {
       return user;
    }
    
    bool Member::saveUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::app | std::fstream::out);
    
       if ( !file.is_open()) {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file << user << std::endl;
    
       if (file) {
          result = true;
       }
    
       file.close();
       return result;
    }
    
    bool Member::loadUser()
    {
       bool result = false;
       file.open("record.txt", std::fstream::in);
    
       if ( !file.is_open())
       {
          throw std::runtime_error("Couldn't open file\n");
       }
    
       file >> user;
    
       if (file)
       {
          result = true;
       }
       file.close();
       return result;
    }
    
    int main()
    {
       Member member;
       member.setUser();
       if ( !member.saveUser())
       {
          std::cerr << "Failed to save the user (broken file) !!\n";
          return 0;
       }
    
       if ( !member.loadUser())
       {
          std::cerr << "Failed to load the user (record.txt: no content) !!\n";
          return 0;
       }
    
       std::cout << "User retrieved from file is " << member.getUser() << std::endl;
       return 0;
    }
    You can see that this at least retrieves something from a file.
    Question: Is there any way I can use seekg() to retrieve those strings or addresses? I can't include the code you wrote because it conflicts with the Index class I made. Can you also tell me how I can include your class with mine? Thanks, kiknwing.

Popular pages Recent additions subscribe to a feed