输入文本,然后保存到文件 [英] Enter text and save to file

查看:157
本文介绍了输入文本,然后保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数创建一个新的文本文件,并允许用户输入文本被保存到该文件。说我有麻烦固定的主要问题是1)允许的话2)按下回车键,而不是去一个新行保存的文本,之间的空格。

 无效NEW_FILE(无效)
{
    焦C [10000]
    字符文件[10000]
    诠释的话;
    的printf(请输入文件\\ n的名称);
   scanf函数(%123S,文件);
    strcat的(文件,TXT);
    FILE * PF;
   PF =的fopen(文件,在w);   如果(!PF)
   fprintf中(标准错误,我无法打开文件\\ n);   其他
   {
        的printf(请输入文字保存\\ n);
        scanf函数(%S,C);
        fprintf中(PF,%S,C);
    }    FCLOSE(PF); //关闭文件
    的printf(\\ n \\ nReturning主菜单... \\ n \\ n);
}


解决方案

使用的 与fgets() 而不是 scanf()的来获得用户输入文本。

要这么做替换此行

  scanf函数(%S,C);

具有以下code:

 如果(NULL!=与​​fgets(C,的sizeof(C),标准输入))
{
  fprintf中(PF,%S,C);
}
其他
{
  如果(0!= FERROR(标准输入))
  {
    fprintf中(标准错误,一个错误,而从标准输入\\ n发生读);
  }
  其他
  {
    fprintf中(标准错误,EOF是试图从标准输入\\ n读取达到了);
  }
}


要允许用户读取一个以上的线把环围绕code以上。这样做,你需要定义它告诉程序停止循环的条件:

下面的示例停止进入的一个点时,读行。和pressing <大骨节病>返回

 
{
  如果(NULL!=与​​fgets(C,的sizeof(C),标准输入))
  {
    如果(0 == STRCMP(C,\\ n))/ *可能有必要使用\\ r \\ n,如果在Windows上。 * /
    {
      打破;
    }    fprintf中(PF,%S,C);
  }
  其他
  {
    如果(0!= FERROR(标准输入))
    {
      fprintf中(标准错误,一个错误,而从标准输入\\ n发生读);
    }
    其他
    {
      fprintf中(标准错误,EOF是试图从标准输入\\ n读取达到了);
    }    打破;
  }
}而(1);

The following function creates a new text file and allows the user to input text to be saved to the file. The main issues that I'm having troubles fixing is 1)allowing spaces between words 2)hitting enter saves the text, instead of going to a new line.

void new_file(void) 
{
    char c[10000];              
    char file[10000];
    int words;
    printf("Enter the name of the file\n");
   scanf("%123s",file);
    strcat(file,".txt"); 
    FILE * pf; 
   pf = fopen(file, "w" );

   if (!pf)
   fprintf( stderr, "I couldn't open the file.\n" );

   else
   {
        printf("Enter text to be saved\n");
        scanf("%s", c);     
        fprintf(pf, "%s", c); 
    }

    fclose(pf);  // close file  
    printf("\n\nReturning to main menu...\n\n"); 
}

解决方案

Use fgets() instead of scanf() to get the input text from the user.

To do so replace this line

scanf("%s", c); 

with the following code:

if (NULL != fgets(c, sizeof(c), stdin))
{
  fprintf(pf, "%s", c);
}
else
{
  if (0 != ferror(stdin))
  {
    fprintf(stderr, "An error occured while reading from stdin\n");
  }
  else
  {
    fprintf(stderr, "EOF was reached while trying to read from stdin\n");
  }
}


To allow the user to read in more then one line put a loop around the code above. Doing so you need to define a condition which tells the program to stop looping:

The following example stops reading in lines when entering a single dot "." and pressing return:

do
{
  if (NULL != fgets(c, sizeof(c), stdin))
  {
    if (0 == strcmp(c, ".\n")) /* Might be necessary to use ".\r\n" if on windows. */
    {
      break;
    }

    fprintf(pf, "%s", c);
  }
  else
  {
    if (0 != ferror(stdin))
    {
      fprintf(stderr, "An error occured while reading from stdin\n");
    }
    else
    {
      fprintf(stderr, "EOF was reached while trying to read from stdin\n");
    }

    break;
  }
} while (1);

这篇关于输入文本,然后保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆