Thread: I/O question

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    I/O question

    I have a problem: I'm trying to read from a text file that has a lot of tabs, carriage returns, and extra spaces. For the most part, I want to maintain the relative ordering of all the material that I view, but it seems like the program skips over a lot of these symbols. Is there any way to read them verbatim? Please let me know. Thanks.

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Have you tried peek() or get() ? Like:

    Code:
    char Char1=File.peek();
    char Char2=File.get();
    I think both are supposed to work. They get the next character from a stream. Difference between them is that get() removes the symbol it reads from the stream, while peek() does not. I don't know which one of them is best for your program.

    So what you can try to do, is to get a character from the file and then see if it is '\t', '\n' or '\0' or something else you want to pay special attention to.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but it seems like the program skips over a lot of these symbols
    What symbols?

    The ">>" operator is defined to skip whitespace.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    After you open the file, call the unsetf member function on the file stream object with ios::skipws (skip white space) as an argument to turn off the automatic skipping of whitespace (tabs, spaces, etc...).

    Code:
    ifstream input("file.txt");
    
    input.unsetf(ios::skipws);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM