从.cpp文件读取时出现问题

Issues when reading from a .cpp file

本文关键字:问题 读取 cpp 文件      更新时间:2023-10-16

我有一个文件,它调用text.cpp。我的任务是从文件中读取的。

text.cpp文件(文件内部)

#include <iostream>     
#include <cstring>
#include <ctype.h>
#include "program.h"
#include "binarytree.h" 
#include "linkedlist.h"

当我检测到时

 '#' symbol

系统将开始循环,如果它检测到'<' & '"'符号,它将打印出剩余的字。示例输出:iostraem

我可以检测'<'符号并打印输出成功。但当我检测到'"'符号时,我可以打印保留字(program.h),但它将跟随在字后面的烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫

我可以知道我的代码出了什么问题吗?

我的代码如下:

 #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    int main()
    {   char temPlate[100];
        char FileName[20];

    cout << "Enter Filename:" ;
    cin >> FileName;
    fstream read(FileName);
    if (read.is_open())
    {
        while(!read.eof())
        {
        read.getline(temPlate, 100);
            for (int k = 0; k < 100 ;k++)
            {
                if(temPlate[k] == '#')
                {
                    for(int i = 0; i < 100; i++)
                    {
                        if(temPlate[i] == '<' ||  temPlate[i] == '"')
                        {
                            for (int j = i+1; j < 100; j++)
                            {
                                if(temPlate[j] == '>' || temPlate[j] == '"')
                                {
                                    break;
                                }
                                cout << temPlate[j];
                            }
                            cout  <<endl;
                        }       
                    }
                 }
            }
        }
    }
    else
    {
        cout << "File does not exist, press enter to exit the program." <<  endl;
        system("pause");
        exit(EXIT_SUCCESS);
    }
    system("pause");
}

输出:

Enter FileName: text.cpp
iostream
cstring
ctype.h
program.h
 ype.h
binarytree.h
 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫
linkedlist.h
 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫

从近端来说,这是因为您正在扫描整个缓冲区,而不仅仅是在上一次getline()期间实际读取的部分。

但在更基本的层面上,这是因为您使用的是字符数组而不是字符串。C++字符串的存在有很多原因,但重要的一点是避免了与固定大小缓冲区相关的许多错误。

这确实比需要的要复杂得多

#include <iostream>
#include <string>
#include <fstream>
int main()
{   
    std::string inputFilename;
    std::cout << "Enter Filename:";
    std::cin >> inputFilename;
    std::ifstream inputFile(inputFilename);
    if(inputFile)
    {
        std::string line;
        while(std::getline(inputFile, line))
        {
            if(line.find('#') != std::string::npos)
            {
                size_t startPos = line.find_first_of("<"");
                size_t endPos = line.find_last_of(">"");
                if(startPos != std::string::npos && endPos != std::string::npos)
                {
                    //advance start to after the < or "
                    ++startPos;
                    //sanity check just in case the line was ill formed
                    if(startPos < endPos)
                    {
                        std::cout << line.substr(startPos, endPos - startPos) << std::endl;
                    }
                }
            }
        }
    }
    else
    {
        std::cout << "File '" << inputFilename << "' does not exist." <<  std::endl;
    }
    return 0;
}

您的线路:

for(int i = 0; i < 100; i++)

应该是:

for(int i = k+1; i < 100; i++)

您不应该使用:

while(!read.eof())

因为EOF位只有在读取文件中最后一个字符之后才设置。有关更多信息,请查看此线程:为什么循环条件中的iostream::eof被认为是错误的?

使用while (std::getline(stream, line))而不是while(!stream.eof()),同时避免使用C样式字符串~>使用std::string对象。

逐行分析文件可能如下所示:

std::string filename;
std::cout << "Enter Filename: ";
std::cin >> filename;
std::ifstream read(filename.c_str());
if (read.is_open())
{
    std::string line;
    while (std::getline(read, line))
    {
        // skip empty lines:
        if (line.empty())
            continue;
        ...

现在,不是你的"程序性"方法,每一行的处理可能看起来像这样:

std::size_t pos = line.find("#include");
if (pos != std::string::npos)
    processInclude(line.substr(pos + 8));

为了避免"烹饪意大利面条代码",有时将一些功能提取到独立函数中,而不是嵌套另一个范围(if(){ for(){ if(){ for(){ ...):

void processInclude(std::string line)
{
    std::string filename;
    for (size_t i = startPos; i < line.size(); ++i)
    {
        if (line[i] == '<')
        {
            std::getline(std::istringstream(line.substr(i + 1), filename, '>');
            break;
        }
        else if (line[i] == '"')
        {
            std::getline(std::istringstream(line.substr(i + 1), filename, '"');
            break;
        }
    }
    if (!filename.empty())
        std::cout << filename << std::endl;
}

希望这能帮助你,或者让你至少以一种不同的方式看待你的代码:)