reading files

This is a discussion on reading files within the C++ Programming forums, part of the General Programming Boards category; Alright, i've got a problem! my program reads a text file that has x and y co-ordinates for a number ...

  1. #1
    MrJCake
    Guest

    Thumbs up reading files

    Alright, i've got a problem! my program reads a text file that has x and y co-ordinates for a number of points on a graph, i can read the file ok, but need to find the max and min values for x and y thats where it goes wrong!
    this is what i'e got:

    Code:
    void getPoints(void)
    {
    
    	char buf[100];
    	int length =0;
    	int a;
    	int b;
    	int v=0;
    	int i =0;
    
    FILE *stream;
    	if ((stream = fopen("Points.txt","r")) == NULL)
    	{
    	  fprintf(stderr,"Error opening file.");
    	  exit(1);
    
                     }
    
                     while(!feof(stream))
    	{
                      fgets(buf,30,stream);
                      if (sscanf(buf,"%d %d", &a,&b))
    		{	
    	                       pointX[i] = a;
    		       pointY[i] = b;
    		       i++;
                                     }
                       }
         Max_X = pointX[0];
         Min_X = pointX[0];
         Min_Y = pointY[0];
         Max_Y = pointY[0];
    
        for(v=2;v=35266;v++)
      	{
    	if(Max_X < pointX[v] ) Max_X = pointX[v];
    	if(Min_X > pointX[v] ) Min_X = pointX[v];
    	if(Max_Y < pointY[v] ) Max_Y = pointY[v];
    	if(Min_Y > pointY[v] ) Min_Y = pointY[v];
    		
      		}
    
    	}
    i cann't get it to scan through all the coordinates correctly. any help appreciated!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It seems ok. How do you declare pointX and pointY? Perhaps your indexes are wrong (an array with n elements range from 0 to n-1)?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    MrJCake
    Guest
    declared like this:
    Code:
    int pointX[36000];
    int pointY[36000];
    int Max_X,Min_X,Max_Y,Min_Y;
    when i compile the max values have some random number in them and te min values are always 0,

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    As always, the error is right in front of the nose .
    Code:
    for(v = 2; v = 35266; v++)
    Should be
    Code:
    for(v = 2; v < 35266; v++)
    (Why start at 2? That's the third element)

    If that wasn't the problem, perhaps you're compiling in a 16-bit compiler? Integers then range from ~ -32000 to +32000, making it impossible to address 35266.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 01:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 12:40 PM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21