Thread: Read File Problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    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;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Is DATA declared globally?
    Sent from my iPadŽ

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    What is a "memory reference error", and which line of code exactly do you get that warning on?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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
    Code:
    temps[i++] = temp;
    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.
    Last edited by Ancient Dragon; 11-29-2005 at 09:16 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    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;
    }
    Last edited by lonewolf367; 11-29-2005 at 10:51 PM. Reason: changes.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    When would this ever be true...
    Code:
    while (fscanf(filedata,"%lf",&temp) == 2)
    ...scanning for one value and continuing only when you get two?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Ok, now it reads the first number in my file. But thats it...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So let's see your latest code.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    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;
    
    }

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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?

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    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;
    }

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    thanks. i got it working. just had to add a little pointer in there from my function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM