这个代码有什么问题吗?
Is there any problem with this code?
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
cout << "Hello.";
ifstream attack,output;
attack.open("CattackList");
output.open("finaltestOutput.txt");
if (!attack)
cout << "file1 not opened.n";
if (!output)
cout << "file2 not opened.n";
char buf1[100],buf2[100];
attack.getline(buf1,100);
int count = 0, C=0;
cout << "hello";
while (!attack.eof())
{
C++;
cout << "ok";
output.open("finaltestOutput");
while (!output.eof())
{
output.getline(buf2, 80);
if (strncmp(buf1, buf2, 51) == 0)
{
cout << buf2 << endl;
count++;
}
}
attack.getline(buf1, 80);
}
cout << "nTotal Attacks : " << C << endl;
cout << "Attacks detected: " << count << endl;
return 0;
}
我连第一个"Hello"都打印不出来
让我看看…
#include<iostream>
#include<fstream>
#include<cstring>
请在每个#include
后面加空格。
if (!attack)
cout << "file1 not opened.n";
if (!output)
cout << "file2 not opened.n";
错误信息应该转到cerr
,而不是cout
。
char buf1[100],buf2[100];
attack.getline(buf1,100);
您是否允许在字符串末尾为null留出空间?还有,每个逗号后面有一个空格。
int count = 0, C=0;
二进制运算符(如=
)周围的空格。此外,不鼓励使用单字母变量名。
output.open("finaltestOutput");
您已经打开了这个文件。你为什么又打开了?此外,对于将输入文件流命名为output
,还需要注意。
output.getline(buf2, 80);
if (strncmp(buf1, buf2, 51) == 0)
你从哪里得到80
和51
这两个数字的?
可能还有更多;
这个代码有什么问题吗?
- 是c++,不是C。
- 缩进被打破
- 每行坚持一条语句 它充满了无法解释的魔法常数。
先把这些问题整理出来,然后再问。
在你的代码中,你在两个部分给出了文件名扩展名,一个是在attack.open("CattackList");
,另一个是在output.open("finaltestOutput");
。
相关文章:
- 警告处理为错误这里有什么问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 当我尝试添加 2 个大字符串时,我无法弄清楚出了什么问题
- 违反const正确性:我应该现实地期待什么问题
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 我的逻辑反转字符串中的元音有什么问题?
- 需要以下代码的帮助,下面的代码有什么问题
- 常量公共成员有什么问题?
- c++无值sort()的问题是什么?
- 以下代码中的函数模板有什么问题?
- 这个返回元素位置的基于循环的函数有什么问题?
- creat_list2功能有什么问题?
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 什么是钻石问题?是一系列问题还是特定问题?
- 格式说明符C++有什么问题
- 我应该在 main 函数中写什么来测试我的问题?
- 任何人都可以告诉我我的 C++ 代码出了什么问题?
- 方法问题 - 什么会改变值,什么不会改变?什么是无效的?