Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++_Parsing - Fatal编程技术网

C++ 文件解析器-如何读取行

C++ 文件解析器-如何读取行,c++,parsing,C++,Parsing,我正试图在OpenGL中从stdin实现线扫描转换,该转换列出了线端点,如下所示: L 0, 0, 150, 150 C 200 300 L -20, 40, 22, 55 [...] Z 其中,[…]几乎相同,Z字符是一个方便的终止运算符,尽管文件结尾也可以工作。L和C分别表示用于绘制直线/圆的点/数据集 下面是我正在使用的解析器(包括所有正在使用的函数的库): void解析文件() { If流线; lines.open(“C:/Users/Me/Desktop/lines.txt”); 字

我正试图在OpenGL中从stdin实现线扫描转换,该转换列出了线端点,如下所示:

L 0, 0, 150, 150
C 200 300
L -20, 40, 22, 55
[...]
Z
其中,[…]几乎相同,Z字符是一个方便的终止运算符,尽管文件结尾也可以工作。L和C分别表示用于绘制直线/圆的点/数据集

下面是我正在使用的解析器(包括所有正在使用的函数的库):

void解析文件()
{
If流线;
lines.open(“C:/Users/Me/Desktop/lines.txt”);
字符串currentLine=0;
while(getline(line,currentLine))
{
开关(currentLine[0]=“L”)
{
字符串coordLine=currentLine.substr(1,currentLine.length());
while(getline(line,coordLine,,')){
对于(int i=0;icout您当前的代码存在许多问题。我提供了一个示例,说明如何解析此代码:

void parseFile()
{
    ifstream lines;
    lines.open("test.txt");
    string currentLine; // Do NOT make this = 0 it will cause a crash!

    while(getline(lines, currentLine))
    {
        // no need to process blank lines
        // this also fixes potential crash
        // when you check the first character
        if(currentLine.empty())
            continue;

        // recommend std::stringstream for parsing strings
        std::istringstream iss(currentLine); // turn line into stream

        char type; // L, C or Z

        if(!(iss >> type)) // ALWAYS check for errors
            continue;

        // Your switch was incorrectly formed
        switch(type)
        {
            case 'L':
            {
                char comma;
                int x0, y0, x1, y1;

                // read the numbers skipping the commas
                if(!(iss >> x0 >> comma >> y0 >> comma >> x1 >> comma >> y1))
                {
                    // ALWAYS check for errors
                    std::cerr << "ERROR: Failed to read L: " << currentLine << std::endl;
                    continue;
                }

                // do something with coordinates (x0, y0, x1, y1) here
                std::cout << "(" << x0 << ", " << y0 << ", " << x1 << ", " << y1 << ")" << '\n';

                break;
            }
            case 'C':
            {
                break;
            }
            case 'Z':
            {
                break;
            }
            default:
                std::cerr << "ERROR: Unrecognized type: " << currentLine << std::endl;
        }
    }
}
void parseFile()
{
If流线;
行打开(“test.txt”);
string currentLine;//不要将其设为0,否则会导致崩溃!
while(getline(line,currentLine))
{
//无需处理空白行
//这也修复了潜在的崩溃
//当您检查第一个字符时
if(currentLine.empty())
继续;
//建议使用std::stringstream解析字符串
std::istringstream iss(currentLine);//将行转换为流
字符类型;//L、C或Z
if(!(iss>>类型))//始终检查错误
继续;
//您的交换机格式不正确
开关(类型)
{
案例“L”:
{
字符逗号;
int x0,y0,x1,y1;
//阅读跳过逗号的数字
如果(!(iss>>x0>>逗号>>y0>>逗号>>x1>>逗号>>y1))
{
//始终检查错误

标准::cerr
*(c+i)=协调线[i]
但是c是空的???
string currentLine=0;
ah,fair point@Galik。这完全是我头脑混乱的副产品。这是一个很好的答案!关于错误检查的一个很好的观点-这始终是最佳实践,我不应该把它留到最后,否则我就不会有所有这些荒谬的初始化错误。我仍然必须给出e有效的存储问题有些想法,但您帮助我优化了字母的switch语句并验证了我的想法,我非常感谢您!干杯!运行它告诉我项目中存在错误,但如果我忽略它们并运行,它会按预期打印。知道原因吗?我正在Windows 7上使用Eclipse IDE。尝试清理和重新打印正在生成项目,我看不到红色下划线的错误。@Rome\u Leader Eclipse不会从“问题”窗口中删除以前的错误。您必须选择这些错误并手动删除它们。可能就是这样的情况?请尝试选择“问题”窗口中的所有错误窗口并删除它们。我现在看到它再也找不到包含项了…比如OpenGL库。奇怪的是,它们以前在其他项目中工作过,我以相同的方式配置它们(项目属性->C/C++常规/路径和符号)。我的知识没有任何变化,但现在它们在构建时都给出了相同的错误(当我忽略“错误”时,它们仍然可以正常启动)。我将尝试重新启动Eclipse.Hmmm…重新打开后,警告将保留在“问题”选项卡中,但错误弹出窗口不再存在。
void parseFile()
{
    ifstream lines;
    lines.open("test.txt");
    string currentLine; // Do NOT make this = 0 it will cause a crash!

    while(getline(lines, currentLine))
    {
        // no need to process blank lines
        // this also fixes potential crash
        // when you check the first character
        if(currentLine.empty())
            continue;

        // recommend std::stringstream for parsing strings
        std::istringstream iss(currentLine); // turn line into stream

        char type; // L, C or Z

        if(!(iss >> type)) // ALWAYS check for errors
            continue;

        // Your switch was incorrectly formed
        switch(type)
        {
            case 'L':
            {
                char comma;
                int x0, y0, x1, y1;

                // read the numbers skipping the commas
                if(!(iss >> x0 >> comma >> y0 >> comma >> x1 >> comma >> y1))
                {
                    // ALWAYS check for errors
                    std::cerr << "ERROR: Failed to read L: " << currentLine << std::endl;
                    continue;
                }

                // do something with coordinates (x0, y0, x1, y1) here
                std::cout << "(" << x0 << ", " << y0 << ", " << x1 << ", " << y1 << ")" << '\n';

                break;
            }
            case 'C':
            {
                break;
            }
            case 'Z':
            {
                break;
            }
            default:
                std::cerr << "ERROR: Unrecognized type: " << currentLine << std::endl;
        }
    }
}