Thread: Question on lesson 10

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Question on lesson 10

    Hello,

    Recently, I read the tutorial on C++ File I/O and tried out the source code example given in the tutorial. I used VC++ 6.0.

    The output for the source code is supposed to be ‘ This text will now be inside of example.txt’. However, the output on my pc is only ‘This’.

    It seems that spaces prevent the entire string from being displayed because when I used ‘Thistextwillnowbeinsideofexample.txt’, the whole string is displayed on screen.



    I think the code related to my question is this:



    B_file>>str;

    Cout<<str;



    May I know what went wrong or is the code wrong?



    Please reply….thank you!

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Use fgets, or whatever the crazy C++ equivalent is. B_file>>str; only reads the first word into the string.

    Code:
    char str[512];
    fgets(str, 512, B_File);

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <string>
    ...
    std::string str;
    getline(file,str,'\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on getline and the string funtions
    By larry_2k4 in forum C Programming
    Replies: 5
    Last Post: 04-29-2009, 09:58 PM
  2. question about declaration precedence
    By waltr in forum C Programming
    Replies: 4
    Last Post: 06-16-2006, 12:32 PM
  3. Quick H/W question...
    By djz3r0 in forum C++ Programming
    Replies: 15
    Last Post: 09-12-2004, 08:36 PM
  4. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 09:11 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM