6 Replies - 6367 Views - Last Post: 30 March 2011 - 02:50 PM Rate Topic: -----

#1 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

C++ Error when reading in text file with blank line at the end

Posted 28 March 2011 - 08:53 PM

Hi, I'm trying to read in a file and filling a string array for each line from the text file. My program compiles and works fine when I delete the blank line at the end of the file I am importing. However the process of importing this file will be automated so I cannot open it and remove the blank line at the end.

The error occurs regardless of which input file I use, I don't know how to get it to ignore the blank line.

Error: "This application has requested the Runtime to terminate in an unusual way. Please contact the application's support team for more information."


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <sstream>
using namespace std;

int main () 
{

fstream importFile("MZS_UMS_origin.txt",ios::in | ios::out); // file input
    string line;
    string import[10000];
    
    ofstream myfile;
    myfile.open("created.txt"); 
    int j = 0; 
    while(!importFile.eof())
    {    
        getline(importFile,line); 
        if ((line.at(0) == '2') && (line.at(1) == '0') && (line.at(2) == '4'))
        {
            myfile << line << endl; 
            import[j] = line;
            j++;
        }                 
    }            
    myfile.close();
    importFile.close();    

    system("pause");
    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ Error when reading in text file with blank line at the end

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++ Error when reading in text file with blank line at the end

Posted 28 March 2011 - 10:15 PM

Quote

My program compiles and works fine when I delete the blank line at the end of the file I am importing.


while(!importFile.eof()) is the cause.
Read this.


And the error message is probably due to string import[10000];. You are trying to allocate too much memory. So...Why do you need to store the file in the array anyway?

This post has been edited by Munawwar: 28 March 2011 - 10:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: C++ Error when reading in text file with blank line at the end

Posted 29 March 2011 - 06:09 PM

I've read the post you've linked to and I'm not sure how this would work with the "getline" that I'm using.

I'm using an array of strings which are then each turned to vectors because I need to do a lot of calculations between the vectors and this is the only was I could think of doing it. (ie. I basically have a delimited text file which is a table and I need to do calculations between the rows and columns and output into a differently formatted delimited text file)
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1398
  • View blog
  • Posts: 4,873
  • Joined: 19-February 09

Re: C++ Error when reading in text file with blank line at the end

Posted 29 March 2011 - 06:34 PM

View Postaiislinn, on 30 March 2011 - 03:09 AM, said:

I've read the post you've linked to and I'm not sure how this would work with the "getline" that I'm using.


A quick way is to use the extract whitespace manipulator function (ws) after your getline().

getline(importFile,line);
importFile >> ws; 



Your while doesn't check for error conditions (which can occur now and then) you could use good().
while(importFile.good())


Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ Error when reading in text file with blank line at the end

Posted 29 March 2011 - 06:38 PM

while(getline(importFile,line))


getline() returns the input stream whose internal state flags indicate success or error. This is the preferred way to read a file.

This post has been edited by n8wxs: 29 March 2011 - 06:43 PM

Was This Post Helpful? 1
  • +
  • -

#6 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++ Error when reading in text file with blank line at the end

Posted 29 March 2011 - 10:42 PM

Exactly.
Here's what I'd do:
int i1,i2;
string word1,word2;

stringstream ss;
while(getline(importFile,line)) {
   ss.str(line);
   ss >> word1 >> i1 >> word2 >> i2;
   
   //Store data in vector or hmm...vector of vectors
   ...
}


If you have more then one whitespace character between columns, then do as #define said (ss >> word1 >> ws >> i1 >> ws >> word2;).
Was This Post Helpful? 0
  • +
  • -

#7 aiislinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 08

Re: C++ Error when reading in text file with blank line at the end

Posted 30 March 2011 - 02:50 PM

Thanks, the while(getline(file,string)) works perfectly!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1