-
Read File Problem
I am trying to get a list of numbers from a file and store each number in an array.
I think I have the structure right but I'm getting a memory referance error and the warning that comes up is:
'l' : unrecognized character espace sequence
what does that mean?
here is my read_file function
Code:
// Read data file
double read_file(int num_temps, double temps[])
{
int i=0;
double temp;
FILE *filedata;
filedata = fopen(DATA,"r");
if(filedata == NULL)
{
printf("Error opening file.\n");
} else {
while (fscanf(filedata,"%lf",&temp) == 2)
{
temps[num_temps] = temp;
num_temps++;
}
}
fclose(filedata);
return num_temps;
}
-
Is DATA declared globally?
-
What is a "memory reference error", and which line of code exactly do you get that warning on?
-
num_temps is probably the number of elements in the array. So using that as an index into the array will produce buffer overflow. you need a different counter that starts with 0 and goes up to, but not including, num_temps as the array index. For example
now change the loop to stop when i == num_temps as well as the existing condition.
Finally, I think fscanf() will return 1, not 2, which is the nuumber of items converted.
-
I made changes as suggested but still same error.
Ok I made some changes. However no data is pulled from the text file.
Code:
double read_file(int num_temps, double temps[])
{
int i=0;
double temp;
FILE *filedata;
filedata = fopen("c:\\labdata.txt","r");
if(filedata == NULL)
{
printf("Error opening file.\n");
} else {
while (fscanf(filedata,"%lf",&temp) == 1)
{
temps[i++] = temp;
}
}
fclose(filedata);
num_temps = i;
return num_temps;
}
-
When would this ever be true...
Code:
while (fscanf(filedata,"%lf",&temp) == 2)
...scanning for one value and continuing only when you get two?
-
Ok, now it reads the first number in my file. But thats it...
-
So let's see your latest code.
Quzah.
-
Code:
}
// Read data file
double read_file(int num_temps, double temps[])
{
int i=0;
double temp;
FILE *filedata;
filedata = fopen("c:\\labdata.txt","r");
if(filedata == NULL)
{
printf("Error opening file.\n");
} else {
while ((fscanf(filedata,"%lf ",&temp)) == 1)
{
temps[i] = temp;
i++;
}
}
fclose(filedata);
num_temps = i;
return num_temps;
}
-
what is the complete type of the thing passed as the second parameter to the function?
What error/warnings are you getting, and what line(s) do they refer to?
How do you know the problem is with that function?
-
hmmm I copied your code and made it into a main function so I could run it, and its working fine. I added some printf's so i could see if it was working correctly, and it seemed to be. here's the code I have:
Code:
#include <stdio.h>
int main()
{
int i=0;
double temp;
FILE *filedata;
int num_temps;
double temps[100];
filedata = fopen("c:\\labdata.txt","r");
if(filedata == NULL)
{
printf("Error opening file.\n");
} else {
while ((fscanf(filedata,"%lf ",&temp)) == 1)
{
temps[i] = temp;
printf("%g\n", temps[i]);
i++;
}
}
fclose(filedata);
num_temps = i;
printf("%d numbers in the file.\n", num_temps);
return 0;
}
-
thanks. i got it working. just had to add a little pointer in there from my function :)