所以我有这段代码:
#include <stdio.h>
#include <stdlib.h>
#define MASK 0x07FF
#define TABLE_SEZE 32
#define LITTLE_SIZE 8
#define SIZE 256
#define DEBUG 1
int main() {
unsigned v_addr = 0;
FILE *inFilePtr = NULL;
FILE *outfilePtr = NULL;
char inFileName[SIZE];
char outfileName[SIZE];
printf("Enter the name of the file we are working with!.\n");
fgets(inFileName, sizeof(inFileName), stdin);
//open 'er up
inFilePtr = fopen(inFileName, "r");
outfilePtr = fopen(outfileName, "w");
if (inFilePtr == NULL || outfilePtr == NULL){
printf("Could not open the file.\n");
exit(1);
}//end of if
}//end main
我想让它读取一个文件,但它继续说该文件不存在。
问题是,该文件位于当前目录中。发生了什么事?
答案 0 :(得分:3)
请注意,fgets()通常会返回一个包含\ n的字符串,因此如果有的话,您需要将其删除。
e.g。
char* p = strchr(inFileName, '\n');
if ( NULL != p )
{
*p = '\0';
}
你还需要将ofFileName设置为
e.g。
strcpy( outFileName, "_" );
strcat( outFileName, inFileName );
答案 1 :(得分:1)
您可以更改
fgets (inFileName, sizeof(inFileName), stdin);
到
scanf ("%[^\n]s", inFileName);
它会完美运作!