简体   繁体   English

为什么我不能使用Visual Studio在C语言中读取文本文件?

[英]Why can't I read a text file in C using Visual Studio?

I've created a C program which reads in a character one by one and determines if it is a vowel, numeric, punctuation etc. 我创建了一个C程序,它逐个读取一个字符,并确定它是否是元音,数字,标点符号等。

The user can choose to input the characters via the keyboard or from a text file. 用户可以选择通过键盘或文本文件输入字符。

The first part works fine, and does everything as expected, however when I run the program and select 'read from file' choice, it simply prints out the statement in the 'else if' and completely fails to read in the text file. 第一部分工作正常,并按预期完成所有操作,但是,当我运行该程序并选择“从文件读取”选项时,它只是在“ else if”中打印出语句,而完全无法在文本文件中读取。

I have the text file saved in the 'Source Files' folder in the Visual Studio 2015 directory, and my code is as follows (the problem area in question is close to the bottom): 我将文本文件保存在Visual Studio 2015目录的'Source Files'文件夹中,我的代码如下(有问题的问题区域靠近底部):

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

FILE *pInputFile;
FILE *pOutputFile;

int answer = 0;

int chr = 0;
int numLowerCaseVowels = 0;
int numUpperCaseVowels = 0;
int numPunctuationChars = 0;
int numNumericals = 0;

int *pCharPointer;
int previousValue = 0;

int main()
{
    // ##### KEYOBARD OR FILE INPUT #####
    printf("\nKEYBOARD OR FILE INPUT?\n");
    printf("Press 'k' for keyboard, 'f' for file.\n");
    answer = _getch();

    if (answer == 'k')
    {
        printf("You selected keyboard.\n\n");
        printf("Enter a character.\n");

        // ##### KEYBOARD INPUT #####
        while ((chr = _getch()) != '\r')
        {
            pCharPointer = &chr;

            printf("%c", chr);

            // Determine if input is lower/upper case vowel.
            switch (chr)
            {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                printf(" Lowercase Vowel.");
                numLowerCaseVowels++;
                break;
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                printf(" Uppercase Vowel.");
                numUpperCaseVowels++;
                break;
            default:
                break;
            }

            //Determine if char is punctuation.
            if (_istpunct(chr)) 
            {
                printf(" Punctuation Character.");
                numPunctuationChars++;
            }

            //Determine if char is numeric.
            if (iswdigit(chr))
            {
                printf(" Numeric Character.");
                numNumericals++;
            }

            //Determine if char is duplicate character.
            if (previousValue == chr) 
            {
                printf(" Duplicate Character.");
                previousValue = *pCharPointer;
            }
            else 
            {
                previousValue = *pCharPointer;
            }

            printf("\n");
        }
    }

    // ##### PROBLEM AREA  #####
    else if (answer == 'f')
    {
        printf("You've chosen file.\n\n");

        fopen_s(&pInputFile, "Text.txt", "r");
        chr = getc(pInputFile);

        while ((chr = getc(pInputFile)) != EOF)
        {
            printf("%c", chr);
        }

        fclose(pInputFile);
    }
    else
    {
        printf("Invalid Input. Exiting...\n");
        exit(1);
    }



    // ##### ANALYSIS #####
    printf("\nNumber of lowercase vowels: %d\n", numLowerCaseVowels);
    printf("Number of uppercase vowels: %d\n", numUpperCaseVowels);
    printf("Number of punctuation characters: %d\n", numPunctuationChars);
    printf("Number of lowercase vowels: %d\n\n", numNumericals);

    return 0;
}

Sorry for the amount of code, I thought it would be best to include the entire source code. 对不起代码量,我认为最好包含整个源代码。

It doesn't actually perform any analysis on the characters from the text file yet, I'm simply trying to get to the stage where it will actually read the file first. 它实际上还没有对文本文件中的字符进行任何分析,我只是试图进入阶段,它将首先实际读取文件。 Any help would be appreciated. 任何帮助,将不胜感激。

The problem you'll have even if the file is found is that you lose the first char of your input. 即使找到文件,您也会遇到的问题是您丢失了输入的第一个字符。

Correct way of doing it: 正确的做法:

errno_t err;

if ((err=fopen_s(&pInputFile, "Text.txt", "r"))==0)
{
       // file exists: don't read a char before the loop or
       // it will be lost

        while ((chr = getc(pInputFile)) != EOF)
        {
            printf("%c", chr);
        }

        fclose(pInputFile);
}
else
{
   fprintf(stderr,"Cannot open file, error %d\n",err);
   // handle the error further if needed
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM