-
fscanf issue!
Hi all!
I'm learning how to separate a program into functions and have come accross a strange problem which works before function breakdown, but not after!
Please find my code below, the problem is that my file header (10) is read as 1 - missing the 0!
Therefore, the array is created with storage for a single member! Any help will be much appreciated! :^)
Code:
//////////////////////////////////////////////////////////////////////////
// Program Title : Tutorial 8, Exercise 3
// Author Name : Joe
// Date of Creation : 17/05/2011
// Date of Last Edit : 17/05/2011
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Pre-processor Directives
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "G:\\numbers.txt"
//////////////////////////////////////////////////////////////////////////
// Type Definitions
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Function Prototypes
//////////////////////////////////////////////////////////////////////////
void display(int [], int);
int load_head();
void load_array(int *, int);
//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////
int main(void)
{
int header = load_head();
printf("\nHeader = %d", header);
int array[header], i;
load_array(array, header);
display(array, header);
int target;
printf("\nEnter target: ");
scanf("%d", &target);
for(i = 0; i < header; i++) {
if(target < header) {
printf("\n%d is not present!", target);
break;
} else if(target == array[i]) {
printf("\n%d is at position %d", target, i + 1);
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
// Sub-function Implementations
//////////////////////////////////////////////////////////////////////////
void display(int a[], int n)
{
int i;
for(i = 0; i < n; i++) {
printf("\n%d", a[i]);
}
}
//////////////////////////////////////////////////////////////////////////
int load_head()
{
int a;
FILE *fp;
fp = fopen(FILE_NAME, "r");
if(fp == NULL) {
printf("\nCannot open %s", FILE_NAME);
exit(EXIT_FAILURE);
}
return fscanf(fp, "%d", &a);
fclose(fp);
}
//////////////////////////////////////////////////////////////////////////
void load_array(int *m, int a)
{
int i;
FILE *fp;
fp = fopen(FILE_NAME, "r");
if(fp == NULL) {
printf("\nCannot open %s", FILE_NAME);
exit(EXIT_FAILURE);
}
fseek(fp, sizeof(i), SEEK_SET);
for(i = 0; i < a; i++) {
fscanf(fp, "%d", &m[i]);
}
}
-
Read what fscanf() does before using it. OK?
http://www.manpagez.com/man/3/scanf/
After your function returns fclose() is never called. It's dead code.
-
Ok... you're breaking things down too far... load_head and load_array should probably be one LoadFile function... because they both contribute to the same goal... loading the array from the file.
You actual problem is that you are scanning for a in load_head but returning the result from fscanf()... which will be 1 because it converted one value.
Also since your return statement is before fclose() the file never gets closed.
Try this...
Code:
int load_head(char* FileName)
{
int a;
FILE *fp;
fp = fopen(FileName, "r");
if(fp == NULL) {
printf("\nCannot open %s", FileName);
exit(EXIT_FAILURE);
}
fscanf(fp, "%d", &a);
fclose(fp);
return a;
}
Now when you call load_array the fopen() could fail and you have the wrong size.
By combining the two functions you A) open the file once B) read it sequentially C) close it once ... I'm thinking that's a much better approach.
Also in main() you are doing this...
Code:
int header = load_head();
int array[header], i;
You should know this is compiler specific behavior. Only C-99 compliant compilers will support it.
This will produce errors on any prior version.