Thread: Strange things (double increment needed????)

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    Strange things (double increment needed????)

    Hi guys, I've got a really strange problem with my program that I have written for an assignment that is due today. Below is a sample program that I have written that demonstrates the weird thing that keeps happening to me:*/

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char fileName[30];
    	char location[100][30];
    	double depth[100];
    	FILE *filePtr;
    	int i, j;
    	
    	printf("Enter name of file to load: ");
    	fgets(fileName, sizeof(fileName),stdin);
    	strtok(fileName, "\n");
    	printf("\n");
    	filePtr = fopen(fileName, "r");
    	if(filePtr == NULL)
    		return 0;
    	else 
    	{
    		
    		
    		for(i = 0; i < 100; i++)
    		{	
    			if(feof(filePtr))
    			{
    				break;
    			}
    					
    		fgets (location[i],sizeof(location[i]),filePtr);
    		strtok(location[i], "\n");	     
    		fscanf(filePtr,"%lf", &depth[i]);
    		}
    	}
    	fclose(filePtr);	
    	for(j=0; j<i;j = j+2)
    	{
    		printf("%s\n%lf\n", location[j], depth[j]);
    	}
    
    	return 0;
    }
    /*The Program runs perfectly how it should 'like this', What I cant understand is why I need to increment j by 2?
    because for some reason the for loop that reads the data from a file only assignes the correct values of depth and location to every second array element??? Am I missing something, I am really stumpped by this would appreciate the help.

    btw an example of a file is
    Name:
    eldorado.txt
    Contents of file:
    Eldorado 01
    22.32
    Eldorado 02
    67.93


    ARRRRRRRGGGGGGGG it doesnt make sense!!!!!!!!!!!!!!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess would be that you aren't getting the right data from your fscanf(). Try checking the return value from fscanf(), e.g.
    Code:
    if (fscanf("%lf", &...) != 1)
    {
       printf("Wrong result from fscanf\n");
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dezz101 View Post
    Hi guys, I've got a really strange problem with my program that I have written for an assignment that is due today.

    Code:
    	
                   	strtok(location[i], "\n");
    Are you using strtok to chomp the \n in location[i]?

    But then fscanf leaves the next newline in filePtr, so on odd iterations of your loop that remaining \n ends up in location[i], is eliminated by strtok (leaving location[i] with nothing) and then the next line is a string, so fscanf leaves it for the next (properly ordered) iteration.

    Throw enough printf's into the right place and you'll see what I mean.

    ps. if you change fscanf to take this: "&#37;lf\n" you will be fine, since depth[i] will only contain a float anyway.
    Last edited by MK27; 10-30-2008 at 08:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    53
    THANK YOU VERY VERY VERY MUCH!!!!!!!!!! I have to finish off the assignment and then have alook at how this works, but you solved the problem and for that I thank you mate. cheers

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Funny how the same problem crops up close together:
    http://cboard.cprogramming.com/showthread.php?t=108708

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. New To C. Need Help
    By raortega3 in forum C Programming
    Replies: 3
    Last Post: 10-10-2007, 11:24 PM
  3. what this error message says?
    By turkertopal in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2006, 02:44 PM
  4. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 02:23 PM
  5. Errors that make no sense...
    By applescruff in forum C++ Programming
    Replies: 22
    Last Post: 03-04-2005, 05:44 PM