Reading files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jthep
    New Member
    • Oct 2007
    • 34

    Reading files

    Hi all,

    I made the following loop to read data from a text file:

    Code:
    inStream.getline(name, 25, '\n');
            while(!inStream.eof())
            {
    			inStream.getline(address, 80, '@');
    			inStream >> yearofbirth;
    			//discard newline
    			inStream.get();
    			inStream.getline(telno, 15, '\n');
    			//discard newline
    			instream.get();
                addRecord(name, address, yearofbirth, telno);
                records++;
    
    			inStream.getline(name, 25, '\n');
    		}
    the txt file itself contains data in the following format:

    name\n
    address@ (can be more than one line)
    yearofbirth\n
    telno\n

    name\n
    address@ (can be more than one line)
    yearofbirth\n
    telno\n

    name\n
    address@ (can be more than one line)
    yearofbirth\n
    telno\n

    etc....the '\n' and '@' at the end of each data is the deliminator between the four data. However, this code goes into an infinate loop for some reason. When it executes line 14, shouldn't the loop continue if there is a new data or get out of the loop since it should encounter the end of file? I tried using while (inStream.getli ne(name, 25, \n)) vs using .eof() but that resulted in only one set of data being read. Any help?

    Regards,
    Jthep
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You have several calls to inStream.getlin e() and inStream.get(). Should any of these reach eof, or encounter some other error, a failbit is set. The inStream.eof() first checks the failbit and if it is set just returns without doing anything.

    You should check each of your inStream calls for success.

    Comment

    • jthep
      New Member
      • Oct 2007
      • 34

      #3
      so I should try something like:

      Code:
      bool exit = false;
      
      while (!exit)
      {
          inStream.getline(name, 25, '\n');
          if(inStream.eof())
          {
              exit = true;
              break;
          }
          inStream.getline(address, 80, '@');
          if(inStream.eof())
          {
              exit = true;
              break;
          }
          inStream >> yearofbook;
          if(inStream.eof())
          {
              exit = true;
              break;
          }
          inStream.get();
            if(inStream.eof())
          {
              exit = true;
              break;
          }
          inStream.get(telno, 15, '\n');
          if(inStream.eof())
          {
              exit = true;
              break;
          }
          inStream.get();
          if(inStream.eof())
          {
              exit = true;
              break;
          }
      
          addRecord(name, address, yearofbirth, telno);
          records++;
      }
      What should I use, break, continue, exit?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        break and contuinue are forms of go-to programming. In the one case, you go-to the end of the loop and in the other case you go-to back to the start of the loop.

        I personally don't like that style but you cna use it.

        I prefer your exit approach where the bool is tested on each cycle of the loop.

        Comment

        Working...