0
我是C新手,我试图让我的int等于4. 4正在从文本文件中读取,但是当我打印出来时,它会打印出32767 。C - fscanf无法正确读取文本文件中的编号
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include "Component.h"
using namespace std;
int main(int argc, char** argv) {
FILE *fp;
fp=fopen("text.txt","r");
int a;
fscanf(fp,"%d",&a);
if(fp == NULL) {
printf("cannot open");
}
printf("%d",a);
}
您没有检查该文件是否打开。 'if(fp == NULL){/ * error report * /}' –
我添加了检查并且文件确实打开了 –
另外,您没有'#include' –