我想知道我怎样才能缩小范围,这样我就不必重复那么多代码了?我不太确定如何在主程序中创建函数并调用它们。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
FILE *openFile;
char fileName1[1500] = "", fileName2[1500] = "";
char character;
int lineCount, wordCount, charCount;
printf("Enter the first filename: ");
gets(fileName1);
openFile = fopen(fileName1, "r");
lineCount = 0;
wordCount = 0;
charCount = 0;
if (openFile) {
while ((character = getc(openFile)) != EOF)
{
if (character != ' ' && character != '\n')
{
++charCount;
}
if (character == ' ' || character == '\n')
{
++wordCount;
}
if (character == '\n')
{
++lineCount;
}
}
if (charCount > 0)
{
++lineCount;
++wordCount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("The number of lines : %d \n", lineCount);
printf("The number of words: %d \n", wordCount);
printf("The number of characters : %d \n", charCount);
fclose(openFile);
printf("Enter the second filename: ");
gets(fileName2);
openFile = fopen(fileName2, "r");
lineCount = 0;
wordCount = 0;
charCount = 0;
if (openFile) {
while ((character = getc(openFile)) != EOF)
{
if (character != ' ' && character != '\n')
{
++charCount;
}
if (character == ' ' || character == '\n')
{
++wordCount;
}
if (character == '\n')
{
++lineCount;
}
}
if (charCount > 0)
{
++lineCount;
++wordCount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("The number of lines : %d \n", lineCount);
printf("The number of words: %d \n", wordCount);
printf("The number of characters : %d \n", charCount);
fclose(openFile);
getchar();
}
最佳答案
1) 循环的存在是有原因的!通常当你不得不一遍又一遍地做同样的事情时,循环才是你真正需要的。只需使用运行 2 次的 for
循环。
注意:void main()
不是c 中main()
的有效签名, 而不是使用 int main(void)
因为你没有向 main()
使用 for
循环,您的 main()
看起来像:
char fileName[1500] = ""; //just a single variable is enough to store file name
//other declarations
for(int i = 1; i <= 2; i++) //loop to make it happen 2 times
{
printf("Enter the %d filename: ", i);
gets(fileName);
openFile = fopen(fileName, "r");
//handle file opening error
if(OpenFile == NULL){
printf("file opening error");
exit(1);
}
//code that gets repeated
fclose(openFile);
}
getch();
2) 您可以编写一个函数并调用它两次(这里我将文件名作为参数发送)
void my_function(char* file_name)
{
char character;
int lineCount, wordCount, charCount;
lineCount = 0;
wordCount = 0;
charCount = 0;
openFile = fopen(file_name, "r");
//handle file opening error
if(OpenFile == NULL){
printf("file opening error");
exit(1);
}
//code that gets repeated
fclose(openFile);
}
//your main
int main (void) {
char fileName1[1500] = "", fileName2[1500] = "";
printf("Enter the first filename: ");
gets(fileName1);
my_function(fileName1);
printf("Enter the second filename: ");
gets(fileName2);
my_function(fileName2);
getch();
}
3)可以同时使用循环和函数!
void my_function(char* file_name)
{
//body of the function same as in before example
}
//your main
int main (void) {
char fileName[1500] = "";
for(int i = 1; i <= 2; i++){
printf("Enter the %d filename: ", i);
gets(fileName);
my_function(fileName);
}
getch();
}
关于在我的 C 程序中创建函数? - 获取 2 个文本文件的统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832791/