Я новичок и изучаю C. Мой код отлично работает при работе в IDE (Xcode). fopen () открывает файл inName.dat - анализирует символы - и выводит в два файла outName и stdout - как и ожидалось.
Но когда я пытаюсь запустить программу из Терминала (я использую Mac) - файл inName.dat не открывается.
Я пытался заменить указатель в вызове fopen () прямым именем файла "inName.dat" - но он все равно не открывается - при запуске в Терминале.
Все три файла 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;
}
Нет сообщения о сбое или ошибке - он просто печатает мою строку «Ошибка здесь», затем возвращает 0 в main, что завершает программу.