Echoing a File

This is a discussion on Echoing a File within the C++ Programming forums, part of the General Programming Boards category; So I wanted to echo a file into standard output through the use of stream iterators, but I encountered an ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Echoing a File

    So I wanted to echo a file into standard output through the use of stream iterators, but I encountered an unexcepted output when I used strings compared to characters. Here's my code with chars (works properly):

    Code:
    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <iterator>
    #include <string>
    #include <stdexcept>
    #include <iomanip>
    
    int main(int argc, char **argv)
    {
    	if (argc != 2) {
    		std::cerr << "Incorrent arguments." << std::endl;
    		throw std::runtime_error("");
    	}
    
    	std::ifstream inFile(argv[1]);
    	inFile >> std::noskipws;
    	if (!inFile) {
    		std::cerr << "Error opening file." << std::endl;
    		throw std::runtime_error("");
    	}
    
    	std::istream_iterator<char> iterIn(inFile);
    	std::istream_iterator<char> iterEOF;
    
    	std::ostream_iterator<char> iterOut(std::cout);
    
    	std::copy(iterIn, iterEOF, iterOut);
    	return 0;
    }
    The only difference between this one and my "string" version is this:

    Code:
    	std::istream_iterator<std::string> iterIn(inFile);
    	std::istream_iterator<std::string> iterEOF;
    
    	std::ostream_iterator<std::string> iterOut(std::cout);
    The output I get from the string version is that I only get the "first word" and the program quits. If I omit the noskipws then it works properly but with no spaces so the output isn't exactly as I wanted it. Why doesn't the string one work like the char version is my primary question? Any pointers here would be good.

    P.S Learning C++ makes C feel so much easier - too much to learn!
    The cost of software maintenance increases with the square of the programmer's creativity.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The noskipws manipulator only affects whether the input stream skips leading whitespace when looking for the next element to read. When reading in anything other than characters, it still considers whitespace to be a delimiter between elements.

    That means if your file contents are "Hello there world" the input will read in "Hello" and stop when it hits the space. Then it will attempt to read in the next string. If noskipws is on, it will not skip over the space still in the input stream. Instead it will stop because it reached whitespace and it will not have read in anything, causing it to reach a fail state. If noskipws had been off, it would have skipped the leading whitespace and read in "there" before stopping due to the space between "there" and "world".

    So when you copy using those iterators and you are reading in strings, it reads in one string at a time and copies it to the output stream until it fails. If noskipws is on it will fail after the first word.

    This doesn't happen with chars because unlike strings, ints, doubles, etc., chars are not whitespace delimited. Instead, even spaces are considered valid input and are read in as an element instead of skipped. So stick with you char version if you want to echo the file exactly.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Thanks that cleared it up totally
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21