Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我可以改进从文件读取字符串的能力吗?_C++_String_File_Stl - Fatal编程技术网

C++ 我可以改进从文件读取字符串的能力吗?

C++ 我可以改进从文件读取字符串的能力吗?,c++,string,file,stl,C++,String,File,Stl,我正在尝试将逗号分隔文件中的数据读入字符串。此外,我希望从字符串中删除额外的空格 我已经设法实现了一个有效的解决方案,但我很感兴趣这是否可以更有效地实现。我的主要目标是删除带有默认字符串大小std::string lName 100,0的临时字符串初始化;因为文件中的数据长度可变 另外,如果你有一些建设性的建议,我将不胜感激 我正在使用MS Visual Studio 2008 以下是SSCCE示例: #include <iostream> #include <algorith

我正在尝试将逗号分隔文件中的数据读入字符串。此外,我希望从字符串中删除额外的空格

我已经设法实现了一个有效的解决方案,但我很感兴趣这是否可以更有效地实现。我的主要目标是删除带有默认字符串大小std::string lName 100,0的临时字符串初始化;因为文件中的数据长度可变

另外,如果你有一些建设性的建议,我将不胜感激

我正在使用MS Visual Studio 2008

以下是SSCCE示例:

#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>

// helper function for removing extra spaces
void removeSpace( std::string &str )
{
    str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
}

int main()
{
    //========== let us construct a test file =====================//
    //===== format is Last Name, First Name, Gender, Color, Birth Date =======//
    std::ofstream os;
    os.open( "test.txt" );

    // first row
    os << " Smith  ," << " John  ," << "   Male , " 
        << " Green  , " << " 6  / 7 / 1960  \n";

    // second row
    os << " Mortensen ," << " Mike  ," << " Male  , " 
        << " Red  , " << "5/5/  1975 \n";

    // third row
    os << " Johnson ," << " Ann  ," << " Female , " 
        << " Blue , " << " 4/ 4 /1985 \n";

    os.close();

    // now let us read data from it
    std::ifstream g;
    g.open( "test.txt" );

    if( g.is_open() )
    {
        while( !g.eof() )
        {
            // temporary strings
            std::string lName( 100, 0 );
            std::string fName( 100, 0 );
            std::string gen( 100, 0 );
            std::string clr( 100, 0 );
            std::string date( 100, 0 );

            // get data from file
            g.getline( &lName[0], 100, ',' );
            g.getline( &fName[0], 100, ',' );
            g.getline( &gen[0], 100, ',' );
            g.getline( &clr[0], 100, ',' );
            g.getline( &date[0], 100 );

            // remove extra spaces from strings
            removeSpace( lName );
            removeSpace( fName );
            removeSpace( gen );
            removeSpace( clr );
            removeSpace( date );

            // display the result
            std::cout << lName.c_str() 
                << ' ' << fName.c_str() 
                << ' ' << gen.c_str()
                << ' ' << clr.c_str()
                << ' ' << date.c_str()
                << std::endl;

            //cleanup
            lName.clear();
            fName.clear();
            gen.clear();
            clr.clear();
            date.clear();
        }
        g.close();
    }

    // since our SSCCE example is done, let us delete the test file
    if( 0 != std::remove( "test.txt" ) )
        std::cout << "Couldn't delete test file!\n\n";
    else
        std::cout << "Successfully deleted test file!\n\n";

    return 0;
}

对于简单的逗号分隔,而不是超出原始问题范围的真实CSV格式,明智地将std::getline与std::istringstream结合使用可能会帮助您摆脱困境,尤其是在逐行限定的情况下。我冒昧地

下面是一个完全修改过的示例。祝你好运。使用remove-erase习惯用法删除空格时,使用+1

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>

// helper function for removing extra spaces
void removeSpace( std::string &str )
{
    str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
}

int main()
{
    //===== format is Last Name, First Name, Gender, Color, Birth Date =======//
    std::ofstream os;
    os.open( "test.txt" );

    // first row
    os << " Smith  ," << " John  ," << "   Male , "
    << " Green  , " << " 6  / 7 / 1960  \n";

    // second row
    os << " Mortensen ," << " Mike  ," << " Male  , "
    << " Red  , " << "5/5/  1975 \n";

    // third row
    os << " Johnson ," << " Ann  ," << " Female , "
    << " Blue , " << " 4/ 4 /1985 \n";

    os.close();

    // now let us read data from it
    std::ifstream g;
    g.open( "test.txt" );

    if( g.is_open() )
    {
        std::string line;
        while( std::getline(g, line) )
        {
            std::istringstream iss(line);
            std::string lName, fName, gen, clr, date;
            if (std::getline(iss, lName, ',') &&
                std::getline(iss, fName, ',') &&
                std::getline(iss, gen, ',') &&
                std::getline(iss, clr, ',') &&
                std::getline(iss, date))
            {
                // remove extra spaces from strings
                removeSpace( lName );
                removeSpace( fName );
                removeSpace( gen );
                removeSpace( clr );
                removeSpace( date );

                // display the result
                std::cout << lName
                          << ' ' << fName
                          << ' ' << gen
                          << ' ' << clr
                          << ' ' << date << '\n';
            }
        }
        g.close();
    }

    // since our SSCCE example is done, let us delete the test file
    if( 0 != std::remove( "test.txt" ) )
        std::cout << "Couldn't delete test file!\n\n";
    else
        std::cout << "Successfully deleted test file!\n\n";

    return 0;
}

SSCCE不需要编写文本文件,您可以简单地列出文本文件,否?@flup:这些解决方案主要使用Boost库,但我不能使用它。的可选分隔符对此很有用,特别是通过填充了从同一函数读取的整行的字符串流。但是在你的循环中丢失.eof检查。@WhozCraig:英语不是我的母语,你能详细说明你的评论吗?到目前为止,我还没有机会使用stringstreams,这也增加了我理解评论的能力。我只是用类似的代码编辑我的问题。我想请您检查我是否错过了一些I/O错误检查。。。感谢您向我介绍stringstreams并帮助我解决这个问题+1@user3261013我很高兴这有帮助。您很少会发现需要使用大多数库代码手动调整std::string的大小。当/如果您这样做了,您可能会发现需要将std::vector与一些老式的字符串操作内容一起使用。留到下一天吧。祝你过得愉快。
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>

// helper function for removing extra spaces
void removeSpace( std::string &str )
{
    str.erase( std::remove( str.begin(), str.end(), ' ' ), str.end() );
}

int main()
{
    //===== format is Last Name, First Name, Gender, Color, Birth Date =======//
    std::ofstream os;
    os.open( "test.txt" );

    // first row
    os << " Smith  ," << " John  ," << "   Male , "
    << " Green  , " << " 6  / 7 / 1960  \n";

    // second row
    os << " Mortensen ," << " Mike  ," << " Male  , "
    << " Red  , " << "5/5/  1975 \n";

    // third row
    os << " Johnson ," << " Ann  ," << " Female , "
    << " Blue , " << " 4/ 4 /1985 \n";

    os.close();

    // now let us read data from it
    std::ifstream g;
    g.open( "test.txt" );

    if( g.is_open() )
    {
        std::string line;
        while( std::getline(g, line) )
        {
            std::istringstream iss(line);
            std::string lName, fName, gen, clr, date;
            if (std::getline(iss, lName, ',') &&
                std::getline(iss, fName, ',') &&
                std::getline(iss, gen, ',') &&
                std::getline(iss, clr, ',') &&
                std::getline(iss, date))
            {
                // remove extra spaces from strings
                removeSpace( lName );
                removeSpace( fName );
                removeSpace( gen );
                removeSpace( clr );
                removeSpace( date );

                // display the result
                std::cout << lName
                          << ' ' << fName
                          << ' ' << gen
                          << ' ' << clr
                          << ' ' << date << '\n';
            }
        }
        g.close();
    }

    // since our SSCCE example is done, let us delete the test file
    if( 0 != std::remove( "test.txt" ) )
        std::cout << "Couldn't delete test file!\n\n";
    else
        std::cout << "Successfully deleted test file!\n\n";

    return 0;
}
Smith John Male Green 6/7/1960
Mortensen Mike Male Red 5/5/1975
Johnson Ann Female Blue 4/4/1985