A.读取指定文件,并返回文件中的最大字符
B.读取指定文件,并返回文件中字符的个数
C.写指定文件,并返回写入字符的个数
D.写指定文件,并返回文件中的最大字符
第1题
4. 下面是一个文件打不开的异常处理程序,分析程序并完成相应问题。 (1)若磁盘中没有myfile.txt 文件,则输出结果如何? (2分) (2)说明特定模板函数的作用。(3分) #include<fstream> #include<iostream> using namespace std; int main() { ifstream source("myfile.txt"); //打开文件 char line[128]; char ss[] = "myfile.txt"; try { if (!source) throw ss; // 如果打开失败,抛出异常 } catch (char * s) { cout << "error opening the file " << s << endl; exit(1); } while (!source.eof()) //判断是否到文件末尾 { source.getline(line, sizeof(line)); cout << line << endl; } source.close(); return 0; }
第2题
下面程序的功能是是读取文本文件in.txt中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符字列反转,并输出到文件out.txt中。 文件int.txt的长度不大于1000字节。部分程序已给出,请将程序补充完成。 reverse( )函数实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。 #include <iostream> #include <fstream> #include <cstring> using namespace std; class doc { private: char *str; //文本字符串首地址 int length; //文本字符个数 public: doc(char *filename); void reverse(); //将字符序列反转 ~doc(); void writeToFile(char *filename); }; doc::doc(char *filename) { ifstream myFile(filename); int len=1001,tmp; str=new char[len]; length=0; while((tmp=myFile.get())!=EOF) { str[length++]=tmp; } str[length]='\0'; myFile.close(); } void doc::reverse() { .............. } doc::~doc() { delete [] str; } void doc::writeToFile(char *filename) { ofstream outFile(filename); outFile< <str; outfile.close(); } int main() { doc mydoc("in.txt"); mydoc.reverse(); mydoc.writetofile("out.txt"); return 0;>
第3题
提示:补充横线上的代码,请使用英文半角输入,不要留空格及其他不可见字符。 #include <stdio.h> #include <stdlib.h> #define LL 80 #define COL 2 #define CSIZE LL/COL-9 #define PL 50 #define MARGIN 3 char buff[COL][PL][CSIZE]; int ln[COL][PL]; int col, row, p; void printout() { int k, i, lpos, col, d,j; char line[LL]; for (k = 0; k < MARGIN; k++) putchar('\n'); for (k = 0; k < PL; k++) { for (i = 0; i < LL - 1; i++) { line[i] = ' '; for (lpos = 0, col = 0; col < COL; lpos += CSIZE + 9, col++) { d = ln[col][k]; p = lpos + 4; while (d > 0) { line[p--] = d % 10 + '0'; d /= 10; } for (p = lpos + 7, j= 0; j < CSIZE;j++) line[p++] = buff[col][k][j]; } } line[p] = '\0'; puts(line); } for (k = 0; k < MARGIN; k++) putchar('\n'); } void nextline() { while (p < CSIZE) buff[col][row][p++] = ' '; if (++row >= PL) { if (++col >= COL) { printout(); col = 0; } row = 0; } p = 0; } void dprint(char* fname) { FILE* fp; int lin, c; if ((fp = fopen(fname, "r")) == NULL)exit(-1); lin = 0; p = 0; col = 0; c = getc(fp); while (c != EOF) { ln[col][row] = ++lin; while (c != '\n' && c != EOF) { if (p >= CSIZE) { nextline(); ln[col][row] = 0; } __________________; c = getc(fp); } nextline(); if (c != EOF)c = getc(fp); } while (col != 0 || row != 0) { ln[col][row] = 0; nextline(); } fclose(fp); } int main(int argc, char** argv) { int f; for (f = 1; f < argc; f++) dprint(argv[f]); return 0; }
第4题
A、(1,1)
B、(0,99)
C、(99,0)
D、(1,99)
E、(1,100)
第6题
B、result=37
C、result=46
D、编译错误
第7题
B、是输入流操作中的语法要求
C、确保不重复输出最后读取的数据
D、if判别是多余的,可以不用
第8题
已存在文件file.txt,其内容为sample1234ASDw,请写出程序的运行结果 。 #include <stdio.h> #include <stdlib.h> int main( ) { FILE *fp; char ch; int count=0; fp = fopen( "file.txt", "r" ); if ( fp == 0 ) { printf( "file error\n" ); exit(1); } ch = fgetc(fp); while( !feof( fp ) ) { if ( ch >= 'a' && ch <= 'z') count++; ch="fgetc(fp);" } printf( "%d\n", count); fclose( fp ); return 0;>
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!