Thread: stupid question!

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

    Question stupid question!

    I know this is stupid- but i have been working with assembly language for a while and now I am back to C++. I am reading in a file using redirection. Isnt there a way I can read the file once- and then read the file again? I cant even remember how to do it!!! or something to that effect! Help! Thanks

    Jenna

  2. #2

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Think it depends on how you're reading the file. Are you using fstreams or the C way of doing it?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    I am just using linux redirection....so when after i compile it i use ./a.out < postfix (where I have an equation in there). I am thinking something with the peek function! I feel like I am getting closer- but I just cant remember!

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ammochck21 View Post
    I am just using linux redirection....so when after i compile it i use ./a.out < postfix (where I have an equation in there). I am thinking something with the peek function! I feel like I am getting closer- but I just cant remember!
    You can't seek on stdin. So the answer is, you can't do what you're suggesting. If you need to go back and re-process data you read from stdin, you have to store all that data somewhere, because you can't go back and read it again.

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

    Question

    why wont this work??

    Code:
    cin >> sym1;
     while (!cin.eof())
      {
        if (cin.peek() == '+' || cin.peek() == '-' || cin.peek() == '*' || cin.peek() == '/')
        {
         count++;
        }
       cin >> sym1;
      }

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ammochck21 View Post
    why wont this work??

    Code:
    cin >> sym1;
     while (!cin.eof())
      {
        if (cin.peek() == '+' || cin.peek() == '-' || cin.peek() == '*' || cin.peek() == '/')
        {
         count++;
        }
       cin >> sym1;
      }
    It would work, but it's a really weird way to do that. Why not store the character in a VARIABLE and then check it against all the possibilities?

    EDIT: Anyway, you said you wanted to "read the file again." That's NOT what you just posted as your example. What do you ACTUALLY want to do?

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    well i have that in my code- and it wont work! my count is 0! Plus heres my thing- I am trying to scan through it once- and count all of the operands. And then i will read through it again using a variable and comparing. I know its weird- but i am trying to find the simplest way to do this!

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ammochck21 View Post
    well i have that in my code- and it wont work! my count is 0! Plus heres my thing- I am trying to scan through it once- and count all of the operands. And then i will read through it again using a variable and comparing. I know its weird- but i am trying to find the simplest way to do this!
    There's just no way to "read through it again." Standard input is like a water faucet. You can't put the water back in the faucet once it comes out.

    If you need to re-process the entire input again, you need to store the entire input in a buffer somewhere. std::string and std::stringstream might come in handy.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    How does the file look? Why don't you output the return of peek() to the console and see what you actually get? I'm willing to bet that it reads whitespace.

    Oh, and eof() is a bad loop condition. See the FAQ.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 12:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 10:00 PM