0

Question Description: I read a file using fstream, when I extract data using istream::operator>>.I found dead loop, getline() seems to do not work.what's more,the code cant find all the keyword. I can’t find causes of the problem. inputfile:

  • @contact 0 0
  • glob_id nbr_id force b
  • 1001009 2001309 1.936503748e-013 5.980546471e+000
  • 1001011 2001311 7.548735960e-018 9.524005670e-001
  • 1001014 2001314 6.246652435e-029 9.048956149e-001
  • @cross_flat 0 0
  • glob_id nbr_id force b
  • 1001009 2001309 1.936503748e-013 5.980546471e+000
  • 1001011 2001311 7.548735960e-018 9.524005670e-001

  • @contact 0 2

  • glob_id nbr_id force b

  • 1001009 2001309 1.936503748e-013 5.980546471e+000

  • @cross_flat 0 0
  • glob_id nbr_id force b
  • 1001009 2001309 1.936503748e-013 5.980546471e+000

  • 1001011 2001311 7.548735960e-018 9.524005670e-001

code:

     #include <iostream>
     #include <cstdlib>
     #include<cstdio>
     #include<fstream>
     #include <map>
     #include <string>
     using namespace std;
        int main()
        {
        fstream file;
        char fileName[128];
        char keyWord[128]={"@contact"};
        char tempWord[256];

        //sprintf()
        int step=0,substep=0;
        unsigned long globelId=0;
        unsigned long nbr_id=0;
        double bForce;
        double Force;
        bool bNextLineEmty=false;
        file.open("demo.text",fstream::in);
        if(!file.is_open())
        {
            cout<<"Error Opeing File!" <<endl;

        }

        while (!file.eof())
        {
            file.getline(tempWord,256);
            char  dumpChar[256];
            int pos=0;
            if(strncmp(keyWord,tempWord,7)==0)
            {

// judge  next line is blank or not  
                while(!bNextLineEmty)
                {

                    file.getline(dumpChar,256);
                    pos=file.tellg();
                    file.getline(dumpChar,256);
// if next line is blank,then bNextLineEmty is Ture,
                    if (strcmp(dumpChar,"")==0)
                    {
                        bNextLineEmty=true;
                    }
                    file.seekg(pos,ios::beg);
                    pos=file.tellg();
//read data 
                    file>>globelId>>nbr_id>>Force>>bForce;          
                }
            }

        }
        return 0;
    }
JackChen
  • 27
  • 3

0 Answers0