Reading and cleaning text file to string

This is a discussion on Reading and cleaning text file to string within the C++ Programming forums, part of the General Programming Boards category; I have a function in a program that reads a text file into a string. My problem is that later ...

  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
    21,318
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,318
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:49 AM
  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, 06: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, 01:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21