I/O question

This is a discussion on I/O question within the C++ Programming forums, part of the General Programming Boards category; I have a problem: I'm trying to read from a text file that has a lot of tabs, carriage returns, ...

  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,662
    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,799
    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, 04:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 11:48 PM
  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, 02:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM

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