Thread: Reading and cleaning text file to string

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    110

    Reading and cleaning text file to string

    I have a function in a program that reads a text file into a string. My problem is that later processes needs the string to be clean. In the sense that there's no white spaces, break rows etc. If someone could help me with this it would make my day. :-)

    The function:
    Code:
    string file2String(const string &fileName)
    {
        ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
        if(!ifs)
        {
            cerr << "Error : File could not be opened." << endl;
            exit(1);
        }
    
        ifstream::pos_type fileSize = ifs.tellg();
        ifs.seekg(0, ios::beg);
        vector<char> bytes(fileSize);
        ifs.read(&bytes[0], fileSize);
    
        return string(&bytes[0], fileSize);
    }
    Cheers!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by überfuzz
    My problem is that later processes needs the string to be clean. In the sense that there's no white spaces, break rows etc.
    That's easy: just return an empty string. Clean as can be!

    Anyway, there are two general approaches I can think of:
    1. White list the "clean" characters. Remove everything else from the string.
    2. Black list the "unclean" characters. Remove them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Is it possible to do that in the function I have at the moment..?

    EDIT
    This post was somewhat vague... I do this to clean strings from <br> used in a program, of which a part is posted in the first post.
    Code:
        string str;
        str = file2String("file.fa");
        str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
    However, this reqiures jet an other traversation of the string. If it's possible I like to do the cleaning in the same process as the reading.
    Last edited by überfuzz; 03-21-2012 at 04:07 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a file and comparing the text to a string
    By souslatable in forum C# Programming
    Replies: 1
    Last Post: 06-09-2011, 08:25 AM
  2. Reading a text file into a string - fread?
    By frankchester in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 12:49 PM
  3. reading a string from a text file into a 2d array
    By duelord in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2009, 07:29 AM
  4. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  5. reading string and integers from a text file
    By Gator in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 02:17 PM