鱼C论坛

 找回密码
 立即注册
查看: 975|回复: 9

[已解决]用freopen函数把stdin赋予文件后,gets函数从stdin中读不到东西

[复制链接]
发表于 2021-12-4 19:39:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用freopen函数将stdin赋予了文件,用gets函数从stdin中读不到东西,但是用scanf读到了,就像gets函数啥也没干一样,这是为什么


我的代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LENGTH 81
int main(void) {
        FILE *pOut = NULL;
        FILE *pIn = NULL;
        char *filename = (char*)"myfile.txt";
        char mystr[100]={};
        char mystr_1[100] = {};
       
        if(NULL == (pOut = freopen(filename, "w+", stdout)) ) {
                fprintf(stderr, "\nError assigning stdout to %s. Program terminated.", filename);
                exit(1);
        }
        fprintf(stderr, "This output goes to %s.\n", filename);
        printf("\nThis sentence should be writed to %s", filename);                         //把这句话写入文件
        fclose(pOut);
        pOut = NULL;
       
        if(NULL == (pIn = freopen(filename, "r+", stdin)) ) {
                fprintf(stderr, "\nError assigning stdin to %s. Program terminated.", filename);
                exit(1);
        }
        gets(mystr_1);                                                                //从文件中读取字符串
        scanf("%s", mystr);                                                        //再尝试从文件中读取字符串
        fprintf(stderr, "\n%s%s", mystr_1, mystr);
        fclose(pIn);
        pIn = NULL;
       
        return 0;
}


屏幕上的输出如下:
This output goer to myfile.txt.

This
最佳答案
2021-12-4 20:11:34
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define LENGTH 81

  5. int main(void) {
  6.     FILE *pOut = NULL;
  7.     FILE *pIn = NULL;
  8.     //char *filename = (char *)"myfile.txt";
  9.     const char *filename = "myfile.txt";
  10.     //cchar mystr[100] = {};
  11.     //char mystr_1[100] = {};
  12.     if(NULL == (pOut = freopen(filename, "w+", stdout))) {
  13.         fprintf(stderr, "\nError assigning stdout to %s. Program terminated.", filename);
  14.         exit(1);
  15.     }
  16.     fprintf(stderr, "This output goes to %s.\n", filename);
  17.     printf("\nThis sentence should be writed to %s", filename); //把这句话写入文件
  18.     fclose(pOut);
  19.     pOut = NULL;
  20.     if(NULL == (pIn = freopen(filename, "r+", stdin))) {
  21.         fprintf(stderr, "\nError assigning stdin to %s. Program terminated.", filename);
  22.         exit(1);
  23.     }
  24.     //gets(mystr_1);      //从文件中读取字符串
  25.     /*
  26.     scanf("%s", mystr);   // 不是所有的系统都可以用gets函数,我这边就不能用
  27.     scanf("%s", mystr_1); //再尝试从文件中读取字符串
  28.     //fprintf(stderr, "\n%s%s", mystr_1, mystr);
  29.     fprintf(stderr, "%s\n", mystr);
  30.     fprintf(stderr, "%s\n", mystr_1);
  31.     */
  32.     int ch;
  33.     while((ch = getchar()) != EOF) fputc(ch, stderr);
  34.     fputc('\n', stderr);
  35.     fclose(pIn);
  36.     pIn = NULL;
  37.     return 0;
  38. }
复制代码

  1. $ ./main
  2. This output goes to myfile.txt.

  3. This sentence should be writed to myfile.txt
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-12-4 19:59:08 | 显示全部楼层
         这个代码你想证实什么东西?不会仅仅是读写文本文件那么简单吧?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-4 20:09:00 | 显示全部楼层
这个代码没有问题,只是你的例子举的不好,导致你认为有问题
我这边用不了gets函数,我想gets得到的是最前面的 '\n'
scanf 得到的是 This
你的这个代码没有问题

printf("\nThis sentence should be writed to %s", filename); //把这句话写入文件

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define LENGTH 81

  5. int main(void) {
  6.     FILE *pOut = NULL;
  7.     FILE *pIn = NULL;
  8.     //char *filename = (char *)"myfile.txt";
  9.     const char *filename = "myfile.txt";
  10.     char mystr[100] = {};
  11.     char mystr_1[100] = {};
  12.     if(NULL == (pOut = freopen(filename, "w+", stdout))) {
  13.         fprintf(stderr, "\nError assigning stdout to %s. Program terminated.", filename);
  14.         exit(1);
  15.     }
  16.     fprintf(stderr, "This output goes to %s.\n", filename);
  17.     printf("\nThis sentence should be writed to %s", filename); //把这句话写入文件
  18.     fclose(pOut);
  19.     pOut = NULL;
  20.     if(NULL == (pIn = freopen(filename, "r+", stdin))) {
  21.         fprintf(stderr, "\nError assigning stdin to %s. Program terminated.", filename);
  22.         exit(1);
  23.     }
  24.     //gets(mystr_1);      //从文件中读取字符串
  25.     scanf("%s", mystr);   // 不是所有的系统都可以用gets函数,我这边就不能用
  26.     scanf("%s", mystr_1); //再尝试从文件中读取字符串
  27.     //fprintf(stderr, "\n%s%s", mystr_1, mystr);
  28.     fprintf(stderr, "%s\n", mystr);
  29.     fprintf(stderr, "%s\n", mystr_1);
  30.     fclose(pIn);
  31.     pIn = NULL;
  32.     return 0;
  33. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-4 20:09:58 | 显示全部楼层
jackz007 发表于 2021-12-4 19:59
这个代码你想证实什么东西?不会仅仅是读写文本文件那么简单吧?

呃,我c语言正在学习文件,在熟悉使用文件的操作
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-4 20:11:04 | 显示全部楼层
  1. $ ./main
  2. This output goes to myfile.txt.
  3. This
  4. sentence
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-4 20:11:34 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define LENGTH 81

  5. int main(void) {
  6.     FILE *pOut = NULL;
  7.     FILE *pIn = NULL;
  8.     //char *filename = (char *)"myfile.txt";
  9.     const char *filename = "myfile.txt";
  10.     //cchar mystr[100] = {};
  11.     //char mystr_1[100] = {};
  12.     if(NULL == (pOut = freopen(filename, "w+", stdout))) {
  13.         fprintf(stderr, "\nError assigning stdout to %s. Program terminated.", filename);
  14.         exit(1);
  15.     }
  16.     fprintf(stderr, "This output goes to %s.\n", filename);
  17.     printf("\nThis sentence should be writed to %s", filename); //把这句话写入文件
  18.     fclose(pOut);
  19.     pOut = NULL;
  20.     if(NULL == (pIn = freopen(filename, "r+", stdin))) {
  21.         fprintf(stderr, "\nError assigning stdin to %s. Program terminated.", filename);
  22.         exit(1);
  23.     }
  24.     //gets(mystr_1);      //从文件中读取字符串
  25.     /*
  26.     scanf("%s", mystr);   // 不是所有的系统都可以用gets函数,我这边就不能用
  27.     scanf("%s", mystr_1); //再尝试从文件中读取字符串
  28.     //fprintf(stderr, "\n%s%s", mystr_1, mystr);
  29.     fprintf(stderr, "%s\n", mystr);
  30.     fprintf(stderr, "%s\n", mystr_1);
  31.     */
  32.     int ch;
  33.     while((ch = getchar()) != EOF) fputc(ch, stderr);
  34.     fputc('\n', stderr);
  35.     fclose(pIn);
  36.     pIn = NULL;
  37.     return 0;
  38. }
复制代码

  1. $ ./main
  2. This output goes to myfile.txt.

  3. This sentence should be writed to myfile.txt
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-12-4 20:13:05 | 显示全部楼层
chenlifeng 发表于 2021-12-4 20:09
呃,我c语言正在学习文件,在熟悉使用文件的操作

       可是 freopen() 可不是一般的文件读写操作,而是输入输出流的改向,你确定知道自己在干什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-4 20:15:19 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-4 20:20:00 | 显示全部楼层
jackz007 发表于 2021-12-4 20:13
可是 freopen() 可不是一般的文件读写操作,而是输入输出流的改向,你确定知道自己在干什么?

我知道它可以把标准流指针和文件关联起来,只是我刚看到这个函数这里,想尝试一下它的功能
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-4 20:25:11 | 显示全部楼层
chenlifeng 发表于 2021-12-4 20:20
我知道它可以把标准流指针和文件关联起来,只是我刚看到这个函数这里,想尝试一下它的功能

      Ok!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 06:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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