Thread: String/Line manipulation

  1. #1
    Registered User
    Join Date
    Nov 2001

    Post String/Line manipulation


    I'm a newbie to C++ and am stuck trying to develop a school project. I need to develop a small C++ program that will accept data files(.txt in this case). Contained in the files are a collection of strings(sentences, words, letters or whatever). If the file contains eight or more lines, the program will display to the screen the first four lines of the file, followed by the last four lines. If the data file is eight line or less, then the entire file will be displayed to screen. Like I said, I'm new to C++ and have tried all the forms of the function getline, as well as trying to assign, remove, ignore, and replace strings in order to display only the eight lines(the first four, and the last four). Any guidance and/or coding examples of how one would be able to add and/or remove strings line by line would be greatly appreciated. In fact, ANY help at all would be appreciated...

    Thank You

  2. #2
    Hey man,
    Hows it goin?
    Off the top of my head, you could get the number of lines in the text file by using the index in the for loop while reading each line;

    for(int i = 1; Ifile_stream.getline(filestring,sizeof(filestring) , '\n'); i++)
    { //no need for anything in here, its the index i you are after.
    //You could store each line that is returned in a string array, but it might get messy if the number of lines in the file is greater than the size if the array.

    You may be using the standard File class, if you are just make sure the getline() function is the condition in the for loop.

    The integer i will now have the number of lines in the file. I'm not sure if there is a function for counting the number of lines.

    If you haven't stored all the lines of the file in the for loop,
    close the file and then reopen and depending on the size, display the lines.
    let me know how you get on.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about Bit Manipulation.
    By parastar in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 08:46 AM
  2. memory problem with string manipulation
    By Ichmael™ in forum C++ Programming
    Replies: 9
    Last Post: 07-10-2005, 03:09 AM
  3. string manipulation
    By knj316 in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2005, 11:36 PM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. File manipulation
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 08:07 AM