打开文件并使用toupper

时间:2016-05-15 09:38:54

标签: c

我正在为学校工作,需要我打开一个文件名,然后重新打印,使一切都成为大写。我尝试编译时遇到多个错误,从“函数不接受参数”到“char与int类型不兼容”和“fileName未声明标识符”等等。我一直在搜索互联网和我的C编程书几个小时,我只是不理解。任何帮助是极大的赞赏。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


int main()
{

    FILE *inFile;
    char *fileName[20];



    printf("Enter a file name: ");
    fgets(*fileName);
    inFile = fopen_s(*fileName, "r");
    if (inFile == NULL)
    {
        printf("\nThe file %s was not successfully opened.", *fileName);
        printf("\nPlease check that the file currently exists.\n");
        exit(1);
    }

    printf("\nThe file has been successfully opened for reading.\n");

    printf("\n%fileName", toupper(*fileName));

    return 0;
}

2 个答案:

答案 0 :(得分:2)

  1. 更改

    char *fileName[20];

  2.    char fileName[20];
    
    1. 了解proper use of fgets

    2. 阅读toupper的手册页

    3. ...

      事实上,在使用某些东西时,最好阅读手册而不是希望获得最佳

答案 1 :(得分:1)

是的,错误太多了。您应该学习如何使用这些功能。

这个固定代码至少可以编译。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* include this to use strchr */
#include <ctype.h>

int main(void)
{

    FILE *inFile;
    char fileName[20];
    char *lf;

    printf("Enter a file name: ");
    fgets(fileName, sizeof(fileName), stdin);
    if ((lf = strchr(fileName, '\n')) != NULL) *lf = '\0'; /* remove newline character after the string */
    inFile = fopen(fileName, "r");
    if (inFile == NULL)
    {
        printf("\nThe file %s was not successfully opened.", fileName);
        printf("\nPlease check that the file currently exists.\n");
        exit(1);
    }

    printf("\nThe file has been successfully opened for reading.\n");

    printf("\n%cfileName", toupper((unsigned char)*fileName));
    fclose(inFile);

    return 0;
}