Thread: input from a file, problem with spaces

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    input from a file, problem with spaces

    Code:
    char text[50];
    
    ifstream a_file("test.txt");
    a_file >> text;
    a_file.close();
    
    cout << text << endl;

    test.txt
    Code:
    here is a sentence

    outputs to the screen

    "here"

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The operator>> stops at the first whitespace... use getline. There are plenty of threads about how to use getline, including one only a few notches below this one with the same question.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    i tried this

    Code:
    ifstream a_file("test.txt");
    a_file.getline(text, 50);
    a_file.close();
    it compiles but does not print anything when running.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    try this
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
    	char text[64];
    	ifstream a_file("test.txt");
    	a_file.getline(text,64);
    	cout << text << endl;
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 11:34 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. a file problem !!
    By girlzone in forum C++ Programming
    Replies: 17
    Last Post: 04-15-2003, 07:55 PM