Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading blank lines

Reading blank lines

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++jsonhelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I'm writing a file parser. It's really pretty simple. Here's a sample file: B C 25 A D 36 D E 63 B A 48 C D 30 D B 52 E C 30 C P 30 P Q 52 Q X 51 X Z 70 Z X 20 X Q 20 * My question is this: Notice that blank line in the middle? How, in C++ using ifstream, can I detect when the parser reaches a blank line such as that one, but still be able to continue parsing? Thanks for any help!

    T E 2 Replies Last reply
    0
    • L Lost User

      I'm writing a file parser. It's really pretty simple. Here's a sample file: B C 25 A D 36 D E 63 B A 48 C D 30 D B 52 E C 30 C P 30 P Q 52 Q X 51 X Z 70 Z X 20 X Q 20 * My question is this: Notice that blank line in the middle? How, in C++ using ifstream, can I detect when the parser reaches a blank line such as that one, but still be able to continue parsing? Thanks for any help!

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      There is Function for detecting EOF() using that you can read your file upto last end of file!. Now for detecting and ignoring Blankline, read file line by line (yeah ifstream has function to do so)and then you can check for empty line easily by using strcmp().


      [Vote One Here, Complete my Survey....] Alok Gupta
      visit me at http://www.thisisalok.tk          "I Think Believe this Will Help"

      1 Reply Last reply
      0
      • L Lost User

        I'm writing a file parser. It's really pretty simple. Here's a sample file: B C 25 A D 36 D E 63 B A 48 C D 30 D B 52 E C 30 C P 30 P Q 52 Q X 51 X Z 70 Z X 20 X Q 20 * My question is this: Notice that blank line in the middle? How, in C++ using ifstream, can I detect when the parser reaches a blank line such as that one, but still be able to continue parsing? Thanks for any help!

        E Offline
        E Offline
        eli15021979
        wrote on last edited by
        #3

        Hi, This line is not a blank line. The legnth of that line is 1 and it's containing '\n'(0x0d). Regards, Eli

        L 1 Reply Last reply
        0
        • E eli15021979

          Hi, This line is not a blank line. The legnth of that line is 1 and it's containing '\n'(0x0d). Regards, Eli

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks. That helped some, but I'm still not sure how to do it. I'm fairly experienced with C++, I've just literally haven't ever used standard file input and output interface, so I'm really pretty stumped. Here's the code for the parser (note this isn't the only code I've written though, I have a lot more that's not the parser): CompanySetVector CompanySets = CompanySetVector(); ifstream InputFile ("company.in"); ofstream OutputFile("company.out"); string A, B; float cAB; int CurrSet = 0; CompanySets.push_back(new CompanySet); InputFile >> A; while (A != "*") { if (InputFile.peek() == '\n') { CompanySets.push_back(new CompanySet); CurrSet++; } InputFile >> B; CompanyDef* pA = CompanySets[CurrSet]->second[A]; CompanyDef* pB = CompanySets[CurrSet]->second[B]; if (pA == NULL) { CompanySets[CurrSet]->second[A] = new CompanyDef(A); pA = CompanySets[CurrSet]->second[A]; } if (pB == NULL) { CompanySets[CurrSet]->second[B] = new CompanyDef(B); pB = CompanySets[CurrSet]->second[B]; } InputFile >> cAB; ControllingInterest* ci = new ControllingInterest; ci->first = pB; ci->second = cAB; pA->ControllingInterests[B] = ci; InputFile >> A; } Most of that code is pretty project specific an irrelevant to you. Notice the line: if (InputFile.peek() == '\n') ... This is where I need to see if the line is blank. I thought using peek would work, but it doesnt :( Do you guys have any other suggestions? Again, thanks for taking time to read this!

          E N 2 Replies Last reply
          0
          • L Lost User

            Thanks. That helped some, but I'm still not sure how to do it. I'm fairly experienced with C++, I've just literally haven't ever used standard file input and output interface, so I'm really pretty stumped. Here's the code for the parser (note this isn't the only code I've written though, I have a lot more that's not the parser): CompanySetVector CompanySets = CompanySetVector(); ifstream InputFile ("company.in"); ofstream OutputFile("company.out"); string A, B; float cAB; int CurrSet = 0; CompanySets.push_back(new CompanySet); InputFile >> A; while (A != "*") { if (InputFile.peek() == '\n') { CompanySets.push_back(new CompanySet); CurrSet++; } InputFile >> B; CompanyDef* pA = CompanySets[CurrSet]->second[A]; CompanyDef* pB = CompanySets[CurrSet]->second[B]; if (pA == NULL) { CompanySets[CurrSet]->second[A] = new CompanyDef(A); pA = CompanySets[CurrSet]->second[A]; } if (pB == NULL) { CompanySets[CurrSet]->second[B] = new CompanyDef(B); pB = CompanySets[CurrSet]->second[B]; } InputFile >> cAB; ControllingInterest* ci = new ControllingInterest; ci->first = pB; ci->second = cAB; pA->ControllingInterests[B] = ci; InputFile >> A; } Most of that code is pretty project specific an irrelevant to you. Notice the line: if (InputFile.peek() == '\n') ... This is where I need to see if the line is blank. I thought using peek would work, but it doesnt :( Do you guys have any other suggestions? Again, thanks for taking time to read this!

            E Offline
            E Offline
            eli15021979
            wrote on last edited by
            #5

            Hi, Why don't you use FILE instead of ifstream and ofstream? It's work like this:

             FILE \*InputFile,\*OutputFile;
             char InputFileName\[\] = "company.in";
             char OutputFileName\[\] = "company.out";
             char \*LineFromFile = new char\[100\];
             CString string;
            
            
             InputFile = fopen(InputFileNAme,"r");
             OutputFile = fopen(OutputFileName,"w");
            
             if(InputFileName == NULL)
             {
                AfxMessageBox("Unable to open input file");
                return;
             } 
            
             if(OutputFileName == NULL)
             {
                AfxMessageBox("Unable to open output file");
                return;
             } 
            
            
             // read 1 line from input file until you reached EOF(note that 
             // max line length is 100bytes.
             // otherwise you must change the length of LineFromFile and the
             // total number of bytes to read.
             while(fgets(LineFromFile,100,InputFile)!= NULL)
             {
                string = LineFromFile;
                if(string.GetLength() == 1)
                {
                   if(string == '\\n')
                   {
                      // this is a blank line , in the middle of the file.
                      // don't do nothing and continue to read.
                      continue;
                   }           
                }
                else
                {
                   //do something
                }         
             }
            

            Good luck, Eli

            1 Reply Last reply
            0
            • L Lost User

              Thanks. That helped some, but I'm still not sure how to do it. I'm fairly experienced with C++, I've just literally haven't ever used standard file input and output interface, so I'm really pretty stumped. Here's the code for the parser (note this isn't the only code I've written though, I have a lot more that's not the parser): CompanySetVector CompanySets = CompanySetVector(); ifstream InputFile ("company.in"); ofstream OutputFile("company.out"); string A, B; float cAB; int CurrSet = 0; CompanySets.push_back(new CompanySet); InputFile >> A; while (A != "*") { if (InputFile.peek() == '\n') { CompanySets.push_back(new CompanySet); CurrSet++; } InputFile >> B; CompanyDef* pA = CompanySets[CurrSet]->second[A]; CompanyDef* pB = CompanySets[CurrSet]->second[B]; if (pA == NULL) { CompanySets[CurrSet]->second[A] = new CompanyDef(A); pA = CompanySets[CurrSet]->second[A]; } if (pB == NULL) { CompanySets[CurrSet]->second[B] = new CompanyDef(B); pB = CompanySets[CurrSet]->second[B]; } InputFile >> cAB; ControllingInterest* ci = new ControllingInterest; ci->first = pB; ci->second = cAB; pA->ControllingInterests[B] = ci; InputFile >> A; } Most of that code is pretty project specific an irrelevant to you. Notice the line: if (InputFile.peek() == '\n') ... This is where I need to see if the line is blank. I thought using peek would work, but it doesnt :( Do you guys have any other suggestions? Again, thanks for taking time to read this!

              N Offline
              N Offline
              ng kok chuan
              wrote on last edited by
              #6

              i think that using ifstream and the "<<" operators (forgot what it's called...) will get u what u want. do something like this: while (!file.IsEOF()) // read all lines, even if it's blank. { __file >> B; } if (!B.empty()) // forgot if this works or not. think it should. __// do the required operation here. anyway think my code is pretty unclear. here's the explanation. just keep readin the file, and before performing the push_back operation for the vectors, check that the string is not empty. this will prevent empty string from being written into the vectors. and as the IsEOF() function stops the while loop only on eof, the blank lines will be read. hope this is correct, as i'm coding off-line here.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups