question

This is a discussion on question within the C++ Programming forums, part of the General Programming Boards category; I am trying to read in from an input file and everytime the word "is" appears in the file, replace ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    new question

    I am trying to read in from an input file and everytime the word "is" appears in the file, replace it with "are".

    will this work?

    Code:
    string word;
    for (int i=0, i<kiSize, i++)
    {
      word=ifIn.getline(array[], 15);
      if (word=="is")
      {
        word=="are";
      }
    }
    thanks
    Last edited by luckyboy23; 04-28-2003 at 10:40 AM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're reading it in, but you're not writing it back to the file. What you're gonna have to do is either write it to another file, or keep all of the words in a buffer and overwrite the original file.

    You should also use ifIn>>word; so it breaks at the spaces.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    word=ifIn.getline(array[], 15);

    You should get some errors from that line. getline() returns a reference to the istream object, and it's parameters are char* and long.

    istream& getline(char* pArray, streamsize n)

    And, what the heck is:

    array[]

    ?? Some undeclared, anonymous, dimensionless place holder? This is the way getline works:

    char text[200];
    ifIn.getline(text, 200);
    cout<<text<<endl;

    There is a version for string types too:

    string line_of_data;
    getline(ifIn, line_of_data);


    Even if your "word=ifIn.getline()" statement did work, word is going to equal a whole line of data, so it's not going to equal "is".
    Last edited by 7stud; 04-27-2003 at 11:17 PM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    i figured out the getline thanks for the help.

    but now i have to read in a word and insert the word "uh" after the 4th letter in that word.

    for example:

    read in "programmer" and change it to "prog-uh-rammer"

    how would i do that?
    Last edited by luckyboy23; 04-28-2003 at 11:08 AM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to leave programmer intact use a different buffer to hold proguhrammer. use a loop to read in first four char of word to change. Then add uh to new word. Then read last part of old word into new word. Works best if you use two different index variables, one for the old word and one for the new word, increasing the index variables appropriately.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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