Filestream help!

This is a discussion on Filestream help! within the C++ Programming forums, part of the General Programming Boards category; Ok so I need to read from a file that the user will enter in. They then enter a search ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Filestream help!

    Ok so I need to read from a file that the user will enter in. They then enter a search string to look for in the file. All I am trying to do here is just take it step by step and make sure everything works. I can get it to read the file name and print that out, I can get it to read the string name and print that out- but when I try to print out the "words" in the file- it will only print out the last word in my file. EX: file is named filename
    contents of file are Hi my name is Jenna
    search string is my
    will print: filename my Jenna

    How can i get it to print filename my Hi my name is Jenna ????

    here is my code:

    Code:
      int main()
    {
      string filename;
      string search;
      string words;;
      char ch;
    
      ifstream name;
    
      cout << "Please enter the name of the input file" << endl;
      cin >> filename;
      name.open(filename.c_str());
      cout << "Please enter your search string" << endl;
      cin >> search;
      name >> words;
    
      while(!name.eof())
        {
          name.get(ch);
          name >> words;
        }
          cout << filename << " " << search << " " << words << endl;
    
    }
    I know if has something to do with name.get(ch) in thw while loop because that will only get one word but why is it getting the last word as opposed to the first word and also how can i make it get the whole content of the file?

    Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    In the while loop
    Code:
       string tmp;
      while(!name.eof())
        {
          name.get(ch);
          name >> words;
        }
    Everytime you take a character it moves the file pointer next. and also name >> words, replaces the contents of words

    Code:
       string tmp;
      while(!name.eof())
        {
          name >> tmp;
          words += " ";
          words += tmp;
        }
    Last edited by Benzakhar; 11-14-2006 at 12:43 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I know if has something to do with name.get(ch)
    Actually, the name.get(ch) does nothing important. It reads in the whitespace between words, but that whitespace is ignored anyway when you read in the next word. So the name.get(ch) can be there or not, it doesn't matter. I'd remove it since it is unnecessary.

    The problem is that if you want to print out each word in the file, you have to put the output inside the loop. Right now, you are reading each word in a loop, and when the loop ends (after the last word is read) it outputs the search word and the last word read.

    Benzakhar's edited version would work, but I would keep it the way you have it because it lends itself better to the final solution (i.e. it uses each word read in individually within the loop as you will have to do when searching for the search string).
    Last edited by Daved; 11-14-2006 at 12:55 PM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    I realized my final solution was poor and I edited last minute before I saw your post .

    Wow what fast posting

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    re: filestream help

    Ok well when I put the output inside the loop it does this:
    Code:
     
    Please enter the name of the input file
    file
    
    Please enter your search string
    my
    
    file my name
    file my jenna
    file my jenna
    it still is only printing the last word in my file.

    I want it to say
    Code:
    file my Hi my name is jenna

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You should be able to figure out why that happens. Mold your output to do what you want.

    If you want the file to be printed first, but not in the loop, then you should output it first, but not in the loop. Use that logic to do the rest. You don't have to end each output with endl. If you don't add that or a new line, then it will still all appear on the same line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. going crazy... wont write to filestream in loop!?
    By ac251404 in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2006, 05:03 PM
  3. Getting type
    By AngKar in forum C# Programming
    Replies: 3
    Last Post: 05-26-2006, 10:06 AM
  4. Visual C++ - FileStream
    By MB1 in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2005, 10:19 AM
  5. Filestream Out + move cursor back one place
    By h3lm3t in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 07:08 PM

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