报告文件应包含以下内容: 1.单词数量 2.大写字母数 3.小写字母数 4.位数
我已成功阅读该文件并计算字母和数字,但我在将内容写入新文件时遇到问题,我们将不胜感激。
#include <stdio.h>
#include <ctype.h>
#define SIZE 40
int main(void)
{
char ch, filename[SIZE];
int digits = 0;
int upper = 0;
int lower = 0;
int entered = 0;
int words = 0;
unsigned long count = 0;
FILE *fp;
printf("Please enter the filename to read: ");
gets(filename);
// "r" reads the file fopen opens the file
if ((fp = fopen(filename, "r")) == NULL)
{
printf("Cannot open the file, %s\n", filename);
}
else
{
puts("Successfully opened, now reading.\n");
while ((ch=getc(fp)) != EOF)
{
if (isalnum(ch))
{
if(!entered)
{
entered = 1;
words++;
}
}
else
{
if (entered)
{
entered = 0;
}
}
if (isupper(ch))
{
upper++;
}
else if (islower(ch))
{
lower++;
}
else if (isdigit(ch))
{
digits++;
}
}
}
fclose(fp); //make sure to close the file if you open one
char filename2 [SIZE];
FILE *fp2;
fprintf(stdout, "Please enter the file name to write in: ");
gets(filename2);
if ((fp2 = fopen("filename2", "w")) == NULL)
{
printf("Cannot create the file, %s\n", filename2);
}
else
{
fprintf(fp2, "The file \"%s\" has %lu Words.\n", filename, words);
fprintf(fp2, "The file \"%s\" has %lu Digits.\n", filename, digits);
fprintf(fp2, "The file \"%s\" has %lu upper case letters.\n", filename, upper);
fprintf(fp2, "The file \"%s\" has %lu lower case letters.\n", filename, lower);
}
fclose(fp2);
return 0;
}
答案 0 :(得分:2)
而不是
if ((fp2 = fopen("filename2", "w")) == NULL)
写
if ((fp2 = fopen(filename2, "w")) == NULL)
然后,踢你自己。