String/Line manipulation

This is a discussion on String/Line manipulation within the C++ Programming forums, part of the General Programming Boards category; Hi... I'm a newbie to C++ and am stuck trying to develop a school project. I need to develop a ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1

    Post String/Line manipulation

    Hi...

    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
    Unregistered
    Guest
    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;
    eg

    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.

    Donal

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, 07: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

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