파일에서 정수를 읽고 정렬하는 연습 프로그램을 작성하고 있습니다. 나는 C에서 파일 IO에 대해 조금 혼란 스러워요 무엇 내가 지금까지이 아래에, 나는 그들이 어떤이있는 경우 누군가가 그것을 살펴보고/제안을 어떤 수정을 제공 할 수있는 기대했다 ...파일에서 일련의 정수를 읽으려고 시도합니다.
// TODO: Open input file and do same as above
char *mode = "r";
FILE *fp = fopen(inputFile, mode);
if(fp == NULL){
fprintf(stderr, "Can't open input file!");
exit(1);
}
// Load the numbers into a buffer and get a count
int buffer[100];
int count = 0;
while(fscanf(fp, "%d", &buffer[count]) == 1) {
count++;
}
// Initialize the array with the proper size
integers = (int*)malloc(sizeof(count*sizeof(int)));
// Load the integers into the array
rewind(fp);
for(int i = 0; i < count; i++){
if(fscanf(fp, "%d", &integers[count] != 1)){
fprintf(stderr, "Error loading integers into array");
exit(1);
}
}
어디 정수 배열을 decleare합니까? – PeerNet
@PeerNet 그것은 글로벌 int 포인터입니다. 나는 단지 내 프로그램의 함수를 포함했다. – JayB
@JayB'fscanf()'에서'n'은 무엇을하는지 압니까? 읽히는 문자 수 그래서 파일에 10 개가 있다면 입력 내용은 무엇입니까? – Gopi