Jimmy Chen

A Programmer

C Primer Plus 第六版 第十三章课后编程练习答案

编程环境Visual Studio 2017

此文为博主原创文章,转载请注明出处

  1. Oct5h说道:

    关于12题可以用随机函数 生成数字存在文档里

  2. Oct5h说道:

    #include
    #include

    int main()
    {
    FILE *p;
    p=fopen("F:\\data2.txt","w");
    for(int i=0;i<600;i++)
    {
    fprintf(p,"%-5d",rand()%10);
    if((i+1)%30==0)
    {
    fprintf(p,"%-5d\n",rand()%10);
    }
    }
    fclose(p);

    return 0;
    }

  3. smwap说道:

    第9题我是查找每行开头的序号然后获取序号,比你这直接计算行数蠢多了。。。 :exclaim:

    1. jimmychen说道:

      :mrgreen: :mrgreen: 毕竟那是最直接的办法啊

  4. smwap说道:

    0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 0 0 0 0 0 0 0
    0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 0 4 5 2 0 0 0 0 0 0 0 0
    0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 4 5 2 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 1 8 5 0 0 0 4 5 2 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 4 5 2 0 0 0 0 0
    5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
    8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
    9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 3 9 9 9 9 9 9 9
    8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
    5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
    0 0 0 0 2 2 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
    0 0 0 0 3 3 0 0 0 0 0 0 5 8 9 9 8 5 0 5 6 1 1 1 1 6 5 0 0 0
    0 0 0 0 4 4 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
    0 0 0 0 5 5 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0

    帮你贴过来了,不用谢

    1. jimmychen说道:

      :razz: :razz: 所以成斗鸡眼没?

    2. 说道:

      好人一生平安 :redface: :redface:

    3. MrWang_tju说道:

      哈哈 就是来找它的! 感谢!

    4. 小高说道:

      好人一生平安,就是来找它的 :lol:

  5. ch说道:

    第7题的B,要是文件1比2大后面的每行不就全部在一起了吗

  6. panda说道:

    第二题运行时会出现Debug Assertion Failed ,返回can’t open是怎么回事 ????

    1. jimmychen说道:

      是不是文件打开失败了?确定文件存在吗?

  7. Alex Mercer说道:

    第7题,关于添加代码实现 B 的内容,
    如果是分步进行的话,应该要再添加两个rewind(),
    但如果是同步进行,演示效果会不会受影响呢?

  8. Alex Mercer(boy)说道:

    第7题 关于添加代码以实现 B 的内容,
    如果分步执行 A ,B,应该添加两个rewind(),
    但如果同步执行,演示效果是否会受影响呢?

    1. jimmychen说道:

      你可以试一下,我这边没有试过这种情况

  9. djv说道:

    第三题 可以用用两个文件指针打开同一个文件,一个读,一个写。 也可以用一个文件指针同时读写,但是要在每次写入之前设置文件位置指针fseek(fp, -1, SEEK_CUR), 写入之后要重新设置指针到当前位置 fseek(fp, 0, SEEK_CUR)。

    1. jimmychen说道:

      你有试过这种方法吗?我当时有试过这样做,但是貌似以写的方式打开的话会将文件清空

    2. 小高说道:

      兄dei,这种方法你试过吗?用写打开文件直接清空了呀,麻烦指教一下

  10. wslzd说道:

    char *tmp_file = "tmp.txt";这行代码晒什么意思呢?求解答

  11. YALIN友達说道:

    第九题用 fseek() 移动光标获取文件最后一个单词的编号,然后用 atoi() 将编号转为数值。(编程环境: gcc 版本 9.3.0 (Arch Linux 9.3.0-1))
    #include
    #include
    #include
    #include
    #define MAX 41
    #define LIMIT 1024 //单词编号上限

    int main(void)
    {
    FILE *fp; //指向目标文件
    char words[MAX]; //单词最大字符数
    int num = 0; //单词编号
    char num_s[LIMIT]; //单词编号字符串,方便调用atoi()函数
    long len = 0; //文件长度
    char ch; //存储从文件中读取字符
    int i; //计数器
    char temp; //临时存储字符

    if ((fp = fopen("wordy", "ab+")) == NULL)
    {
    fprintf(stderr, "Can't open \"wordy\" file.\n");
    exit(EXIT_FAILURE);
    }
    fseek(fp, 0L, SEEK_END); //光标定位到文件末尾
    len = ftell(fp);
    for (long count = 1; count < len; count++)
    { //获取打开的文件中最末尾单词的标号
    fseek(fp, -count, SEEK_END); //光标回退
    ch = getc(fp);
    if (ch == ']')
    i = 0;
    if (isdigit(ch))
    num_s[i++] = ch;
    if (ch == '[')
    break;
    }
    for (int j = 0; j < i / 2; j++)
    { //将倒置的单词编号调整为正确值
    temp = num_s[j];
    num_s[j] = num_s[i - j - 1];
    num_s[i - j - 1] = temp;
    }
    num = atoi(num_s);
    puts("Enter words to add to the file; press the #");
    puts("key at the beginning of a line to terminate.");
    while ((fscanf(stdin, "%s", words) == 1) && (words[0] != '#'))
    fprintf(fp, "[%d]%s\n", ++num, words);
    puts("File contents:");
    rewind(fp);
    while (fscanf(fp, "%s", words) == 1)
    puts(words);
    if (fclose(fp) != 0)
    fprintf(stderr, "Error closing file.\n");

    return 0;
    }

  12. 大大小小白说道:

    大神好 :smile: ,我想问一下我在每次打开文件时,获取每个单词的序号的代码我是这样写的,结果导致了只 :confused: 用第一次运行程序时可以添加单词,后面再运行就添加不了单词,我很苦恼 :cry: ,不知道为什么,请大神帮忙看看 :cry:
    我写入单词时在单词前面加了“[单词对应序号]”
    #include
    #include

    int main()
    {
    FILE *fp;
    char words[50];
    int count, ch; //编号
    if((fp = fopen("a.txt", "ab+")) == NULL)
    {
    fprintf(stderr, "Open of %s is fail!", "a.txt");
    exit(1);
    }
    rewind(fp);
    //找到上次最后一个编号
    while( (ch = getc(fp)) != '[' && ch != EOF)
    continue;
    if(ch == EOF)
    count = 0;
    else
    {
    ch = getc(fp);
    count = atoi(ch);
    }
    //添加单词到文件中
    puts("Enter words to add to the file; press #");
    puts("to quit.");
    while( (fscanf(stdin, "%s", words) == 1) && (words[0] != '#') )
    {
    printf("%d***", count);
    fprintf(fp, "[%d]%s ", ++count, words); //将读取的单词输出到文件
    }

    puts("File contents:");
    rewind(fp); //回到文件开始
    //显示文件内容
    while(fscanf(fp, "%s", words) == 1)
    puts(words);
    puts("done!");
    if(fclose(fp) != 0)
    {
    fprintf(stderr, "Error in closing file!");
    exit(1);
    }
    return 0;
    }

回复 smwap 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注