从.cpp文件读取时出现问题
Issues when reading from a .cpp file
我有一个文件,它调用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;
}
希望这能帮助你,或者让你至少以一种不同的方式看待你的代码:)
相关文章:
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 读取最后一行代码算法 - c++ 时出现问题
- 使用 seekg() 读取C++中的文件时出现问题
- 从矢量C++读取字符时出现问题
- 读取文件并将其存储在unordered_map中时出现问题
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 读取制表符分隔的文件时出现问题 C++
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 我遇到使用 c++ 逐行读取输入文件的问题
- 关于读取有效 c++ 第 4 项(将非局部静态变量替换为局部静态变量)的问题
- 使用getline读取.csv文件时出现问题
- 在C++中读取 unicode 输入时出现问题
- 从文件中读取文本时出现编码问题
- 从标准输入读取多个类型时出现问题
- 使用管道从 STDIN 读取分叉进程时出现问题
- FSTREAM的问题读取确定,但不编写用户输入字符串
- 计数排序输出问题:读取访问冲突
- 问题读取预编译的着色器文件ID3DBlob