Thread: Reading/Writing txt files

  1. #1
    Registered User
    Join Date
    Feb 2011

    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:

    class filereader
    filereader(string filename);
    filereader::filereader(string filename)
       vector<string> v;
       ifstream File(filename.c_str());
    The class is not what I'm using, it's just to complement the function. What I need help with is the function filereader(string filename).

    I'm sort of confused. I would appreciate any help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > cin.getline(filename.c_str());
    > v.push_back(filename);
    c_str() is a CONST, so you shouldn't be trying to modify it with getline.

    Something like
       vector<string> v;
       ifstream File(filename.c_str());
       string line;
       while ( File.getline( line ) ) {
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Is there an overloaded function in streams that read into a std::string? I don't think so, IIRC.
    You may have to use std::getline(yourfile, yourstring);
    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. Reading/Writing txt files
    By bigboybz in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2011, 06:57 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
    Replies: 2
    Last Post: 03-10-2002, 09:53 PM