LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 978|回复: 4

请教一些C++的很基本的问题

[复制链接]
发表于 2004-9-5 09:21:32 | 显示全部楼层 |阅读模式
刚学C++,很多简单问题都解决不了.

1.有如下定义:
      char *c[];
      要求:使用ifstream fin;fin>>或从终端使用cin>>读取一行的数据,将每个字符串存入数组中.


2.生成一个文件,大小为long L(字节),文件名为 const char *s,文件内容随意,可以用某一字符(如#)填充.

3.模拟磁盘的输入输出

      string file;
      const int BLOCKSIZE=512;
      char c[BLOCKSIZE];
      readBlock(string file,long blockoffset,const int blocksize,char *c);
      writeBlock(string file,long blockoffset,const int blocksize,char *c);

其中blockoffset为第几块,blocksize为块大小.c为数据缓冲,大小为一个块.file是虚拟的磁盘的文件名.由上面第二个问题生成.
发表于 2004-9-5 12:02:24 | 显示全部楼层

第一题

/*
* A C plus plus demo.
* write by smileonce at sep.5
* */
#include <iostream>
#include <string>

using namespace std;

int main() {
    string sbuf;
    cout << "Input a string for test :" << endl;
    getline(cin, sbuf);

    const char * c;
    c = sbuf.c_str();

    //let us test the result
    cout << "Echo the result:" << endl;
    int i=0;
    while(c!='\0') cout << c[i++];
    cout << endl;
    return 0;
}
发表于 2004-9-5 12:03:35 | 显示全部楼层
怪了,tab会自动被剃调?怎么办?我用的是vim 6.3
发表于 2004-9-5 12:20:03 | 显示全部楼层

第二题

root@smileonce:/bak/tmp/source# cat ./T2.cpp
/**
* A c plus plus demo
* write by smileonce
*              at sep 5
* */

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

        string fname;
        long fsize;

        cout << "How large the file is (byte): ";
        cin >> fsize;
        cout << "FileName :";
        cin >> fname;

        ofstream fout(fname.c_str());
        for (int i=0; i<fsize; ++i) fout << "#";

        return 0;
}
root@smileonce:/bak/tmp/source# g++ T2.cpp -o T2
root@smileonce:/bak/tmp/source# ./T2
How large the file is (byte): 20
FileName :abc.txt
root@smileonce:/bak/tmp/source# ls -al abc.txt
-rw-r--r--  1 root root 20 2004-09-05 12:19 abc.txt
root@smileonce:/bak/tmp/source#
 楼主| 发表于 2004-9-5 12:24:53 | 显示全部楼层
Thank
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表