Thread: Reading/Writing txt files

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    16

    Reading/Writing txt files

    I'm writing a program that accepts a file and inputs each line from a txt file into a vector. Is it correct to use:

    Code:
    class filereader
    {
    public:
    filereader(string filename);
    private:
    
    }
    filereader::filereader(string filename)
    {
       vector<string> v;
       ifstream File(filename.c_str());
       cin.getline(filename.c_str());
       v.push_back(filename);
       while(cin.good())
       {
          cin.getline(filename.c_str());
          v.push_back(filename);
       }
    }
    I'm sort of confused. I would appreciate any help!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, that won't work very well. The first strangeness is that you're pushing the filename onto the vector v, for some reason. Was v supposed to contain only data from the file?

    The bigger issue, however, is that you're calling .c_str() as a destination for getline(). c_str() is supposed to return a constant char* version of an std::string. You shouldn't be assigning anything to the pointer returned by c_str(), As it so happens, there's a version of getline() especially for std::strings; you should be using
    Code:
    getline(cin, theStringInWhichToPutTheNewLine);
    getline - C++ Reference

    Also note that the vector v is declared as a local variable in the constructor, and it will get destroyed once the constructor is finished. This means that you have no way to access the information afterwords.

    Actually the biggest issue of all -- which I missed when I first saw your code -- is that you're reading from cin, the standard input, rather than File, the actual input file!

    You don't have to go to nearly that much effort. Consider this:
    Code:
    // This opens a file stream "file" which reads the data in the file specified by filename.
    // Now file is an istream just like cin, and can be used anywhere cin can be used.
    // Note that the .c_str() is required here because the designers of C++ had an oversight;
    // if it seems silly that's because it is. Most other functions work with strings directly.
    ifstream file(filename.c_str());
    
    string line;  // this variable holds the next line to read from the file
    while(getline(file, line)) {
        v.push_back(line);
    }
    So, basically: getline reads a line from a stream into a variable, and if you want to get a string use the other version of getline that I provided. When you open a file using an ifstream you create a variable which is essentially usable anywhere you can use cin. If you'd like to say "cin >> age" you can instead say "file >> age". And so on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    16
    Sorry, I'm such a C++ noob. I had no idea a string would be that big of a deal. I'll post my full code like I should have from the start. I've changed some things to fit what you said, but for the most part it is the same.

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class UserPw
    {
    public:
        UserPw();
        UserPw(string user, string password);
        string getUser();
        string getPassword();
    private:
        string user;
        string password;
    };
    
    UserPw::UserPw()
    {
        string user="";
        string password="";
    }
    
    UserPw::UserPw(string user, string password)
    {
        this->user = user;
        this->password = password;
    }
    
    string UserPw::getUser()
    {
        return user;
    }
    
    string UserPw::getPassword()
    {
        return password;
    }
       
    class PasswordFile
    {
    public:
        PasswordFile();
        PasswordFile(string filename);
        vector<UserPw> getFile();
        void addpw(UserPw newentry);
    private:
        string filename;
        vector<UserPw> entry;
    };
    
    PasswordFile::PasswordFile()
    {
        vector<UserPw> entry;
        string filename = "";
    }
    
    PasswordFile::PasswordFile(string filename)
    {
        this->filename = filename;
        UserPw x;
        ifstream infile;
        infile.open(filename.c_str());
        getline(cin, x);
        entry.push_back(x);
        while(infile.good())
        {
            getline(cin, x);
            entry.push_back(x);
        }
    }
    
    vector<UserPw> PasswordFile::getFile()
    {
    	return entry;
    }
    
    void PasswordFile::addpw(UserPw newentry)
    {
        ofstream File("password.txt");
        entry.push_back(newentry);
        for(int i=0;i<entry;i++)
        {
        File>> entry[i];
        }
    }
    
    int main()
    {
    PasswordFile passfile("password.txt");
    passfile.addpw(UserPw("dbotting","123qwe"));
    passfile.addpw(UserPw("egomez","qwerty"));
    passfile.addpw(UserPw("tongyu","liberty"));
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Deja vù? I am pretty sure there is another thread on this very board with the very same title.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Communication with txt files (continuing)
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 08-13-2007, 08:49 PM
  4. Editing TXT files in C++
    By Nexus in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2005, 05:34 AM
  5. printing txt files
    By baphomet in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-10-2002, 09:53 PM