Streaming in a file with spaces/blanks

This is a discussion on Streaming in a file with spaces/blanks within the C++ Programming forums, part of the General Programming Boards category; hello all, I'm trying to input a file into an array, everything works, except for inputing spaces...I tried looking in ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    Streaming in a file with spaces/blanks

    hello all, I'm trying to input a file into an array, everything works, except for inputing spaces...I tried looking in the FAQ and around the web...but can't find much...here is what I have:
    Code:
    void readInFileAndGetChars(string fileName, char text[], int& sum)
    {
    	//declaring variables
    	int i;
    
    	ifstream inStream;
    
    	inStream.open(fileName.c_str()); //open user chosen file
    
    	if ( inStream.fail() ) { //check if file opened right
    		cout << "Input file opening for " << fileName << " failed.  Exiting...\n\n";
    		exit(-1);
    	}
    
    	for ( i=0; i<MAX_SIZE; i++ ){ //input file into the array
    		inStream >> text[i];
    	}
    	
    	cout << endl << endl << fileName << " consists of " << MAX_SIZE << " characters. "
    	     << endl;
    }
    text[] is a dynamic array of size MAX_SIZE or 500000,

    any hints?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    getline();

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Or you could open for binary and use std::istream::read( ).

    [edit]
    Yay! 500th post![/b]
    Last edited by XSquared; 04-13-2003 at 05:42 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    but getline() will only read in the first line, correct? and if I have to read in the first half a million characters of very large files?

    and I'm not sure what std::read() does, any further explanations?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    OK, I got it now...this is what I used:
    Code:
    for( i=0; i<MAX_SIZE; i++ ){
              inStream.get( text[i]);
    }
    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Read will most likely be faster than using get, because it will grab a large chunk of the file instead of byte-by-byte.

    Code:
    istream &read( char *buffer, streamsize num );
    Code:
    inStream.read( text, MAX_SIZE );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 01:13 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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