Reading lines from a File

This is a discussion on Reading lines from a File within the C++ Programming forums, part of the General Programming Boards category; This is an insanely simple problem I'm sure. I started a c++ class with no c++ history and only java. ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    16

    Reading lines from a File

    This is an insanely simple problem I'm sure. I started a c++ class with no c++ history and only java. The class started out in recursion and I've been struggling through it. I have one day to finish a program and e-mail it in and no time to talk to the teacher so I am in a dire situation. I have coded a program called Set which will uses a linked list to override the operators such as + - * += ==, etc. I get a file with varying lines that will appear as, {1,3,5,7} + {1,2,9} or {4,5,9} + {3,5} - {9}. I'm having trouble finding a way to read through multiple lines and reading the various characters into the Set class. I coded a small function to read one set section but I can't even compile it to check if it would work. Regardless, I am a complete beginner so don't assume I know anything.

    Code:
    void  Set::readFromFile(ifstream myFile)
    {
          int value;
          char seperator;
          while (! myFile.peek()=="}")
          {
              myFile >> value >> seperator;
              insert(myList.getLength()+1,value);
          }
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    And the question is ?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I can't even compile it to check if it would work.
    You mean you are going to turn in code without compiling it or testing it?

    The biggest thing I see wrong with that code is that you never read in the opening brace.

    Also you are only reading in a single set, but that might be what you want since this is a set class.

    Finally, you should be passing ifstreams by reference.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    while (! myFile.peek()=="}")
    Doesn't peek() return a single character? http://www.cppreference.com/cppio/peek.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    yes it does, and also you might want to replace the
    double quotes in:

    Code:
    while (! myFile.peek()=="}")
    with single quotes

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    That's what I was insinuating. It's like a teacher saying, "Doesn't 'cat' have three letters?". But thanks for being explicit.

    You can compile code online with one of several online compilers, though you can't test the executable. I usually use dinkumware.com/exam.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Desolation: If you read properly, I state "I'm having trouble finding a way to read through multiple lines and reading the various characters into the Set class." And the question, Can you help me find a way to read through multiple lines and reading the various characters into the Set class?, is inferred.

    Daved, I never intended to turn it in, I posted it to ask if anyone could see what was wrong with it. I only understand passing by reference and your comment on opening brace faintly. As far as only reading in a single set, this is just a small method to help with a larger run through of a program. I'd imagine that I would need to read a set, read an operator, read another set, test if its the end and if not, read another operator, etc.

    Dwks/Muzzleloader: Thank you, I did intend it to be a character.

    Code:
    void  Set::readFromFile(ifstream myFile)
    {
          int value;
          char seperator;
          while (! myFile.peek()=='}')
          {
              myFile >> value >> seperator;
              insert(myList.getLength()+1,value);
          }
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Where are you reading in the opening brace of the Set? The Set looks like this: { 1, 2, 3 }. There is an opening brace, then numbers separated by commas, then a closing brace. Either you are reading in the opening brace somewhere else, or you have to do it in this function.

    Another issue that I just saw is that you read value then separator, but the last value is not followed by a separator, it is followed by a closing brace. That will cause errors.

    Of course, I don't see much use in worrying about all the minor mistakes if you cannot compile and test the fixes.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Okay I changed some of the small problems.


    The errors I get when compiling are:
    "In file included from Set.cpp "
    "variable or field `readFromFile' declared void"
    "expected `;' before '(' token "
    "no `void Set::readFromFile(std::ifstream)' member function declared in class `Set' "
    "no match for 'operator>>' in 'myFile >> value' "

    Here is my code for Set.cpp and Set.h not including the methods which I've tested.

    Code:
    #include "Set.h"
    #include <iostream>
    
    using namespace std;
    
    //Set methods
    
    void  Set::readFromFile(ifstream myFile)
    {
          int value;
          char seperator;
          if(! myFile.peek()=='}') 
               myFile >> value;
          while (! myFile.peek()=='}')
          {
              myFile >> seperator >> value;
              insert(myList.getLength()+1,value);
          }
    }
    Code:
    #ifndef SET_H
    #define SET_H
    #include "List.h"
    
    class Set {
          public:
    
    //methods
    
    void readFromFile(ifstream myFile);
                 
              	private:
    
                List myList;
    };        
       
    #endif

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    In Set.h, "ifstream" is not defined. You should #include <iosfwd>, and use std::ifstream instead of ifstream. In fact, you should declare readFromFile() as:
    Code:
    void readFromFile(std::ifstream& myFile);
    After all, reading from an input stream modifies it, so it makes sense to take such a stream by reference. Using an istream instead of an ifstream could be more flexible.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Thank you laserlight. That helped me fix the including errors I had. One of my main questions is about the lines "myFile >> seperator;" I've used lines such as these in my other programs but in this one it complains "no match for 'operator>>' in 'myFile >> seperator' ". Is there something I'm doing wrong? And I've tried to code what I need in the end. I'm sure there are countless errors but maybe it would get the overall idea of what I'm trying to do.

    Code:
    void  Set::readFromFile(std::ifstream& myFile)
    {
          int value;
          char seperator;
          myFile >> seperator; //remove {
          if(! myFile.peek()=='}') //take in the first value
               myFile >> value;
          //take in the remaining values in the form ",value" repeating 
          while (! myFile.peek()=='}') 
          {
              myFile >> seperator >> value;
              insert(myList.getLength()+1,value);
          }
          myFile >> seperator; //remove }
    }
    
    Set Set::readOperator(std::ifstream& myFile)
    {
         Set b;
         Set equal;
         int value;
         char seperator;
         char operator;
         myFile << operator; //take in the operator following }
         b.readFromFile(myFile); //fill b with the set following the operator
         if(operator == '+')
                     equal=this+b;
         if(operator == '-')
                     equal=this-b;
         if(operator == '*')
                     equal=this*b;
         if(myFile.peek()=='+'||myFile.peek()=='-'||myFile.peek()=='*')
         {
                     equal.readOperator(myFile);
         }
         else
         {
             return equal;
         }
    }
    My driver uses it like this:

    Code:
    #include "Set.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ifstream myfile ("SetDoc.txt");
        
        if (myfile.is_open())
          {
            while (! myfile.eof() )
            {
              Set a;
              Set result;
              a.readFromFile(myFile);
              result=a.readOperator(myFile);
              result.displaySet();
            }
           myfile.close();
          }
    
        
        cin.get();
    }

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    Set Set::readOperator(std::ifstream& myFile)
    {
        //[...]
         myFile << operator; //take in the operator following }
    For input streams, you use >>; for output streams, you use <<.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Awesome, I managed to get it reading. My question is a different one now. My program will compile and prints out the first Set addition perfectly the way I wanted it to. Unfortunately, after the first printed line, the program errors out. I know what the problem is but not why. I overload the + operator to add two sets together and return the union. Then it returns the set and the program which is iterating through the file and displaying the results takes the set that was returned and either performs another operation on it or just displays it and moves down the line. The problem is that when I return my set in the overloaded operator it doesn't make it back into the method that called it. What is returned is empty so when I try to display it, it bugs out. Any reason for this?

    My driver is:
    Code:
    while (! myfile.eof() )
            {
              Set a;
              Set result;
              
              a.readFromFile(myfile);
              
              
              
              
              result=a.readOperator(myfile);
    
              result.displaySet();
            }
    and it calls readOperator and the error happens inside here:
    Code:
    if(anOperator == '+')
         {
                     c=*this+b; //The overloaded + operator returns a Set that is filled how I want
                     // but it is never making it into c
         }
         else if(anOperator == '-')
         {
                     c=*this-b;
         }
         else if(anOperator == '*')
                     c=*this*b;
         if(myFile.peek()!='\n')
         {             
                     c.readOperator(myFile);
         }
         else
         {
             c.displaySet(); // And inside the displaySet, it calls getValue to an index that doesn't            
             // exist
             return c;
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Reading file into edit - losing new lines
    By eam in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2003, 12:07 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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