Thread: From a file into an array.

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes it prints all 15, but ten on one line and five on another.

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    21
    Not with that code. Still all 15 on one line...

    Code:
    ....
    
    int s;
    	
    	for (s = 0; s < numberOfChar; s++)
       	{
    	int a = 0;
    	fscanf(fp, "&#37;d", &x[s]);
    	printf("%d ", x[s]);
    
    		if (a ==10)
    		{
    		printf("\n");
    		a++;
    
    		}
    	}
    
    ....
    outputs

    Code:
    
    475 3260 7102 6004 8467 7024 1819 2901 3844 3613 640 7549 3096 3365 771
    
    
    Press any key to continue...
    Last edited by fmsguy06; 11-09-2008 at 04:44 PM.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, now you moved your a++ into the if statement where it doesn't belong. a++ needs to always happen, regardless of whether we're at 10 or not.

  4. #19
    Registered User
    Join Date
    May 2007
    Posts
    21
    Nor does it work before the if or after the if.

    Code:
    int s;
    	
    	for (s = 0; s < numberOfChar; s++)
       	{
    	int a = 0;
    	fscanf(fp, "%d", &x[s]);
    	printf("%d ", x[s]);
    	a++;
    
    		if (a ==10)
    		{
    		printf("\n");
    		
    		}
    
    	}
    outputs:

    Code:
    475 3260 7102 6004 8467 7024 1819 2901 3844 3613 640 7549 3096 3365 771

    as does

    Code:
    	for (s = 0; s < numberOfChar; s++)
       	{
    	int a = 0;
    	fscanf(fp, "%d", &x[s]);
    	printf("%d ", x[s]);
    	
    
    		if (a ==10)
    		{
    		printf("\n");
    		
    		}
                       a++;
    	}
    return the same.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah -- now I see. The int a = 0 is inside the brace, which means it gets reset to 0 every time through the loop.

  6. #21
    Registered User
    Join Date
    May 2007
    Posts
    21
    You are correct. Placing the definition of a outside the brace, made it work, up to 20.

    Code:
    	
    	int s;
    	int a = 1;
    	for (s = 0; s < numberOfChar; s++)
       	{
    	
    	fscanf(fp, "%d", &x[s]);
    	printf("%d ", x[s]);
    	
    
    		if (a ==10)
    		{
    		printf("\n");
    		
    		}
    a++;
    
    	}
    Adding a line to make a = 0 after the print of the newline allowed it to work for over 20 numbers as well. Great. It does what I was interested in doing and some now. Now just trying to get it to display an error if the file to read from is not found (as right now it just freezes and quits the compiler). I thank you all for helping me get this far though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM