我正在编写一个程序来执行此操作但有错误消息。我已将fopen-s行更改为现在的行,但输入两个文件名后会出现此消息? error message
这里没有Visual Studio中出现的错误消息,但不确定这不是问题。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
char letter;
char filename1[50];
char filename2[50];
//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;
//Gets the user to enter name of file, then puts it in string.
printf("\n Enter first text document\n");
gets_s(filename1);
printf("\n Enter second text document\n");
gets_s(filename2);
//opens then reads the first file.
fopen_s(&file_in, filename1, "r");
// counts the number of words, then lines, then letters in doc 1.
while ((letter = getc(file_in)) != EOF);
{
if (isspace(letter) && !isspace(getchar()))
{
wordcount++;
}
if (letter == '\n');
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
//opens then reads the second file.
fopen_s(&file_in, filename2, "r");
// counts the number of words, then lines, then letters in doc 2.
while ((letter = getc(file_in)) != EOF);
{
if (isspace(letter) && !isspace(getchar()))
{
wordcount++;
}
if (letter == '\n');
{
linecount++;
}
if (letter == '-')
{
charcount++;
}
}
//displays the total on screen.
printf_s("Words:", wordcount, "\n");
printf_s("Letters", charcount, "\n");
printf_s("Lines", linecount, "\n")
}
答案 0 :(得分:0)
fopen_s需要3个参数。首先是指针,你要为其分配文件,第二个是文件名,第三个是你想要如何访问文件(&#34; r&#34;,&#34; w&#34;等)
答案 1 :(得分:-2)
我发现这里有一些变化......这段代码对我有用:
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
int main(int argc, char *argv[])
{
//setting names of ints and chars.
FILE *file_in;
int wordcount, linecount, charcount;
int letter;
char filename1[50];
char filename2[50];
//setting all counts to 0.
wordcount = 0;
linecount = 0;
charcount = 0;
printf("Enter first text document: ");
fgets(filename1, 50, stdin);
ch = filename1;
while(*ch != '\n')
ch++;
*ch = '\0';
printf("Enter second text document: ");
fgets(filename2, 50, stdin);
ch = filename2;
while(*ch != '\n')
ch++;
*ch = '\0';
//opens then reads the first file.
file_in = fopen(filename1, "r");
// counts the number of words, then lines, then letters in doc 1.
while((letter = fgetc(file_in)) != EOF)
{
if (letter == ' ')
{
wordcount++;
}
else if (letter == '\n')
{
linecount++;
}
else
{
charcount++;
}
}
//displays the total on screen.
printf("File 1...\n");
printf("Words: %d\n", wordcount);
printf("Letters: %d\n", charcount);
printf("Lines: %d\n", linecount);
//opens then reads the second file.
file_in = fopen(filename2, "r");
//reset counts
wordcount = 0;
linecount = 0;
charcount = 0;
// counts the number of words, then lines, then letters in doc 2.
while((letter = fgetc(file_in)) != EOF)
{
if (letter == ' ')
{
wordcount++;
}
else if (letter == '\n')
{
linecount++;
}
else
{
charcount++;
}
}
//displays the total on screen.
printf("File 2...\n");
printf("Words: %d\n", wordcount);
printf("Letters: %d\n", charcount);
printf("Lines: %d\n", linecount);
}