文件格式如下:假如要删除第三列,此处用空格数和行数来标记某一列,i为行数,b为空格,enter为换行数,对读入的空格数和换行数进行计数,某一行定位到第三列的判断条件为b==3*enter+2,当满足此条件时便不再输出文件,直到b值改变或者enter改变,否则此式将一直维持恒等,这样便可以在输出时跳过某两个相邻空格之间的所有字符。
输入文件:
输出文件:
代码:
//******************************************* //***************删除某一列数据************** //******************************************* #include #include using namespace std; int main() { const char *filename = "case8.txt"; const char *file = "case8_0.txt"; fstream fin(filename, ios::in); fstream fout(file, ios::out); if (!fin) { cout << "fail" << endl; return 0; } const int num = 65555; char temp0[num] = { 0 }; char temp1[num] = { 0 }; int i = 0, enter = 0, b = 0; fin.getline(temp0,num-1,0);//按行以字符格式读整个文件 do { //对空格和回车予以标记 if ((int)temp0[i] == (int)(' ')) b++; if ((int)temp0[i] == (int)('\n')) enter++; //满足要求才输出 if (b != (3 * enter + 2)) { fout << temp0[i]; }i++; } while (temp0[i] != '\0'); return 0; fin.close(); fout.close(); }