c++读取文件栈溢出,麻烦各位帮我看看什么原因
程序代码:
#include <fstream> #include <iostream> using namespace std; void enCode(char *buf, int size) { for (int index = 0; index <= size; ++index) ++(*buf++); } void dnCode(char *buf, int size) { for (int index = 0; index <= size; ++index) --(*buf++); } int main(int argc, char **argv) { if (argc != 3|| strcmp(argv[1] ,"-e") && strcmp(argv[1] ,"-d") ) { cout << "program name : -e|-d :filename" << endl; return 1; } fstream fin(argv[2], ios::binary | ios::in | ios::out); if (!fin) { cout << "读取文件错误" << endl; return 2; } char buf[100]; int pos1 = 0, pos2 = 0 ,bytes; void(*p)(char *, int) = argv[1][1] == 'e' ? enCode : dnCode; bool ok = true; while (ok) { fin.seekg(pos1); fin.read(buf, sizeof(buf)); if (!fin) { fin.clear(); ok = false; } else pos2 = fin.tellg(); bytes = fin.gcount(); p(buf, bytes); fin.seekp(pos1); fin.write(buf, fin.gcount()); pos1 = pos2; } return 0; }
把char buf[100]改成char buf[10000]就不会出现错误了,这是什么原因,我看陈宗权视屏中他改成100没有出现错误