c - 我需要创建一个程序,提示用户输入要读取的两个输入文件的名称。然后读取文件并显示统计信息

标签 c

这是我目前拥有的代码,但似乎无法编译。任何帮助将非常感激。

#include <stdio.h>
main (void)
{
  FILE *textfile1;
  FILE *textfile2;

  int c;
  char Filename;

  printf("Enter the name of text file:");
  scanf("%s", &Filename;

  textfile1 = fopen(textfile1, "r");
  if (textfile1 == NULL)
  {
    printf("File not found");
  }
  else
  {
    while (fscanf(textfile1, "%c", &textfile1) == 1)
   {
     printf("%c", textfile1);
    }
    fclose(textfile1);
    }
   }

最佳答案

#include <stdio.h>
int main (void) /* you had better specify the return value explicitly */
{
 FILE *textfile1;
 FILE *textfile2;

 int c;
 char Filename[1024]; /* allocate enough buffer to store the name */
 char textfiledata1; /* add this to store the data from file */

 printf("Enter the name of text file:");
 scanf("%1023s", Filename); /* add ), and other change including limit of length to read to avoid buffer overrun */

 textfile1 = fopen(Filename, "r"); /* you have to pass the name. Do not pass the uninitialized file pointer! */
 if (textfile1 == NULL)
 {
  printf("File not found");
 }
 else
 {
  while (fscanf(textfile1, "%c", &textfiledata1) == 1) /* store the data from file to the file pointer? nonsense. */
  {
   printf("%c", textfiledata1);
  }
  fclose(textfile1);
 }
 return 0; /* explicitly return something is a good practice */
}

关于c - 我需要创建一个程序,提示用户输入要读取的两个输入文件的名称。然后读取文件并显示统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046259/

相关文章:

C - 将 char 数组转换为整数指针?

c++ - 如何指定远程预处理器包含路径,如 192.0.2.17 ://usr/include

C : Dynamic spacing when printing values

c - 在 C 中通过字符串运行命令

c - (1u<<x<<y) 是什么意思?

c - 如何分离特定数据

c - C 中 "big"字符十六进制常量的值是多少?

C 程序在 For 循环中崩溃

捕获并显示调用 C system() 的返回值

c - 指向数组的指针有什么意义?