在C语言中,fopen()正在IDE(Xcode)中运行,但无法在Terminal(Mac)中打开文件

时间:2019-05-15 20:35:27

标签: c fopen

我是新手,正在学习C。在IDE(Xcode)中运行时,我的代码可以完美运行。 fopen()打开文件inName.dat-解析字符-并按预期输出到两个outName文件和stdout。

但是,当我尝试从Terminal(我使用Mac)运行该程序时-inName.dat文件将不会打开。

我尝试用直接文件名“ inName.dat”替换fopen()调用中的指针-但在终端中运行时仍无法打开。

inName.dat,outName1.dat和outName.dat2这三个文件都存在,并且与应用程序文件位于同一文件中。

我知道错误是在打开文件的那一刻-为了确定我已经添加了打印的错误消息。

/* My main application */

int main() {
    char *inFileName;
    char *outFileName1 = "outName1.dat";
    char *outFileName2 = "outName2.dat";

    if((inFileName = calloc(25, sizeof(char))) == NULL) {
        fprintf(stderr, "Failure allocating memory\n");
        return EXIT_FAILURE;
    }

    printf("Enter filename to split: \n");
    scanf("%s", inFileName);

    split(inFileName, outFileName1, outFileName2);

    return EXIT_SUCCESS;
}

/* My implementation file */

int split(const char* inName, const char* outName1, const char*
    outName2) {
    FILE *inFile;
    FILE *outFile1;
    FILE *outFile2;
    char *inBuffer;
    char *outBuffer1;
    char *outBuffer2;
    char *stdoutBuff;
    int out1, out2, outstd;

    /* open three files */
    if((inFile = fopen(inName, "r")) == NULL) {
        printf("The error is here"); /* Prints, then app ends */
        return 0;
    }
    if((outFile1 = fopen(outName1, "w")) == NULL) {
        fclose(inFile);
        return 0;
    }
    if((outFile2 = fopen(outName2, "w")) == NULL) {
        fclose(inFile);
        fclose(outFile1);
        return 0;
    }

    /* allocate memory to buffers */
    if((inBuffer = calloc(500, sizeof(char))) == NULL) {
        fprintf(stderr, "Inbuffer memory allocation error\n");
        return EXIT_FAILURE;
    }
    if((outBuffer1 = calloc(500, sizeof(char))) == NULL) {
        fprintf(stderr, "Outbuffer1 memory allocation error\n");
        return EXIT_FAILURE;
    }
    if((outBuffer2 = calloc(500, sizeof(char))) == NULL) {
        fprintf(stderr, "Outbuffer2 memory allocation error\n");
        return EXIT_FAILURE;
    }
    if((stdoutBuff = calloc(500, sizeof(char))) == NULL) {
        fprintf(stderr, "Stdoutbuffer memory allocation error\n");
        return EXIT_FAILURE;
    }

    /* read in from input file to inBuffer */
    if(fgets(inBuffer, 500, inFile) == NULL) {
        fprintf(stderr, "Input from file error\n");
        return EXIT_FAILURE;
    }

    /* sort each letter to out buffers as lower/uppercase/other */
    for(out1 = 0, out2 = 0, outstd = 0; *inBuffer; inBuffer++) {
        if(islower(inBuffer[0])) {
            outBuffer1[out1] = inBuffer[0];
            out1++;
        } else if(isupper(inBuffer[0])) {
            outBuffer2[out2] = inBuffer[0];
            out2++;
        } else {
            stdoutBuff[outstd] = inBuffer[0];
            outstd++;
        }
    }

    /* print the out buffers to file 1,2 and stdout */
    if(fputs(outBuffer1, outFile1) == EOF) {
        return 0;
    }
    if(fputs(outBuffer2, outFile2) == EOF) {
        return 0;
    }
    printf("Non lower / uppercase letters from file:\n");
    if(fputs(stdoutBuff, stdout) == EOF) {
        return 0;
    }  
    return 1;
}

没有崩溃或错误消息-它只打印我的行“错误在这里”,然后向main返回0,从而结束程序。

0 个答案:

没有答案