Like Tree1Likes
  • 1 Post By rags_to_riches

How to insert a "word" at a different line than the one being referenced to.

This is a discussion on How to insert a "word" at a different line than the one being referenced to. within the C++ Programming forums, part of the General Programming Boards category; Hi, I have an input file which contains 3 sentences: Jane likes chicken. Alex likes chicken too. both like chicken. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    How to insert a "word" at a different line than the one being referenced to.

    Hi,
    I have an input file which contains 3 sentences:
    Jane likes chicken.
    Alex likes chicken too.
    both like chicken.

    At the beginning of the 3rd sentence I want to add a word "They" while the reference line is the 1st line i.e "Jane likes chicken".
    The code I have tried to develop can do so if reference is made to the exact line where I want to add the word but this is not what I want. Below is the program that I have developed but as I said before, it adds the word when reference is made to the exact line of insertion.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    string line;
    int main()
    {
        /*opens the input file*/
        ifstream infile("../testing_in.txt");
        if (!infile.is_open())
        {
            cout<<"problem!!!!"<<endl;
            return 1;
        }
        /*this part opens the temperate output file*/
        ofstream outfile ("testing_out.txt");
        /*this reads the lines in the input file and inserts
         a word "They" in one of the lines specified*/
        while(getline(infile,line))
        {
            if(line.substr(0,line.find(" "))== "both")
            {
                line.insert(0,"They ");
                outfile<<line<<endl;
            }
        }
    }
    Is there a way whereby I can do something like this:
    (1) Find a line that begins with "Jane".
    (2) Then two (2) line after that line with Jane (which brings me to the line "both like chicken" in this particular case, insert the word "They".

    Thanks for your help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    Finite-state machine - Wikipedia, the free encyclopedia
    So the first state would be "initial"
    Then you go to "foundJane" state, having found Jane at the start of the line.
    Then count 2 lines, see if it begins with "both"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User svanski's Avatar
    Join Date
    Oct 2011
    Posts
    8
    I modified and made some changes to your code. It almost does exactly what you need. I do not have time right now to finish it. However, if I can finish it later I will post that code too.

    I do not know for sure but, if you declare type string "string s" without #including <string>. On some compilers you might get an error, because strings are not primitive types. Strings are sequence of chars. Again I am not 100% sure about this. I know that this is how C works; not sure about c++.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    #include <cstring>
    
    
    using namespace std;
    
    
    int main(){ 
        /*opens the input file*/
        ifstream infile("./text.txt"); /* "./" is for current directory this will work when program.cpp and text.txt are in same folder/directory  */
        if (!infile.is_open()){
            cout<<"problem!!!!"<<endl;
            return 1;
        /*this part opens the temperate output file*/
        fstream myFile;  
        myFile.open("text.txt", fstream::in | fstream::out| fstream::app); /* you need append so that every time you write to a file you do not overwrite... This way pointer is at the end of the file so when you write to it it will add new stuff.  */
    
    
        std::string string_input;   /* initializing string_input which will hold whatever you write on to command line  */
        getline(cin, string_input); /* read from command line and write it into string_input  */
        
        
        myFile<<string_input<<endl;  /* channel, command line input that is held by "string_input" into myFile which is "text.txt"   */
    
    
        myFile.close();  /* close the file   */
    }

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    I modified and made some changes to your code. It almost does exactly what you need. I do not have time right now to finish it. However, if I can finish it later I will post that code too.
    Welcome to the forum, dachichk. You might want to keep in mind that we're not in the habit of handing people full-blown solutions to their homework problems. It's counterproductive to their education.

  5. #5
    Registered User svanski's Avatar
    Join Date
    Oct 2011
    Posts
    8
    Quote Originally Posted by rags_to_riches View Post
    Welcome to the forum, dachichk. You might want to keep in mind that we're not in the habit of handing people full-blown solutions to their homework problems. It's counterproductive to their education.
    Thanks, rags_to_riches. This is my second post where I provided "solution". On my first post people warned/advised me not to do so. I will keep in mind for my future posts.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by rags_to_riches View Post
    Welcome to the forum, dachichk. You might want to keep in mind that we're not in the habit of handing people full-blown solutions to their homework problems. It's counterproductive to their education.
    Well, this post was quite short and to the point....
    I'd say these kinds of answers..i.e short and to the point code (not pages full of code) help those who are willing to learn.
    Avoiding good answers just in case the OP is looking for pasta code seems counter productive.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    I had little problem with what was provided, it was more this:

    It almost does exactly what you need. I do not have time right now to finish it. However, if I can finish it later I will post that code too.
    This is an "if I get a chance, I will complete your homework for you" statement. Do we really want that, instead of hints and pointers?

    EDIT: Oh, and it was cross-posted.
    manasij7479 likes this.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Quote Originally Posted by manasij7479 View Post
    Well, this post was quite short and to the point....
    I'd say these kinds of answers..i.e short and to the point code (not pages full of code) help those who are willing to learn.
    Avoiding good answers just in case the OP is looking for pasta code seems counter productive.
    Sometimes examples can be helpful. In such cases, one might make a similar example, but one that does not solve the original problem.
    In that way, the one who is being helped may get a deeper understanding needed to finish the original problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,454
    what's interesting is that the code he posted has nothing to do with the answers the OP seeks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help this algorithm. "Longest word"
    By mike2008 in forum Tech Board
    Replies: 4
    Last Post: 10-02-2010, 09:48 AM
  2. Replies: 10
    Last Post: 08-16-2007, 01:02 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  5. How to insert "tab" into a stream of output??
    By JCK in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2002, 09:23 PM

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