Thread: Manipulating C strings

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    Manipulating C strings

    I am new to C++. My programming experience thus far has been in Java. I've been given an assignment to parse some things out of a file using C++ file streams but I am not allowed to use the C++ strings to manipulate what comes from the file. I have to use cstrings instead. I am trying to read the file one line at a time but the specifications say that I have to be able delimit on /r, /n, and /r/n. I can't figure out a safe way to do that with cstring. Every way that I come up with either is unsafe because of potential buffer overflows or makes it so that I potential lose data that I already got from the file. Can anyone propose a good way to approach this problem? Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    By "cstrings" do you mean standard C strings, using char arrays?

    To delimit on either of '\r', '\n' or '\r' & '\n', you'd have to use a lookahead if you see a '\r' character, since the only way to know if the NEXT character is '\n' is to read that character.

    One way to achieve that would be to have your own little wrapper struct for a stream, and have two more fields, one with the lookahead character, the other with the "lookahead is valid" boolean value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    get should be able to munge \r\n, or any subset thereof into \n, as in

    file.get(buffer, sizeof buffer, '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. manipulating strings
    By student2005 in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2004, 03:21 AM
  5. manipulating strings
    By rain_e in forum C Programming
    Replies: 14
    Last Post: 03-05-2002, 03:15 PM