Thread: Load Array From Whitespace-delimited TextFile

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    58

    Load Array From Whitespace-delimited TextFile

    Problem: I can read from the file, however the information is not doing what it should. I overlooked the fact that the text file is whitespace-delimited by either spaces or tabs.

    Problem - need to load the array from a whitespce-delimited text file!
    **also, if anyone is bored, im not sure if my pointers from my functions to my declarations in main are correct or not**

    Code:
    Code:
    #pragma warning(disable : 4996)
    #include <stdio.h>
    #include <stdlib.h>
    
    #define bool int
    #define true 1
    #define false 0
    
    #define	in_filename "Prog1Data.txt"
    #define out_filename "Report.txt"
    #define maxsize 100
    
    /* function prototypess */
    int load (FILE *InFile, int *ary_nbr, int size);
    void printtab (FILE *OutFile, int *ary_nbr, int size);
    void classifydata (int *ary_nbr, int size, int *nCount_Pos, int *nCount_Neg, int *nCount_Zer);
    
    int main(int argc,char *argv[])
    {
    	/* statements for pos,neg,zer numbers */
    	int nCount_Pos = 0; 
    	int nCount_Neg = 0; 
    	int nCount_Zer = 0; 
    
    	/* declare array */
    	int ary_nbr[maxsize];
    
    	/* I/O pointers */
    	FILE *InFile, *OutFile;
    
    	/* error checking */
    	bool file_error_flag;
    
    	/* Open the file to read */
    	file_error_flag = false;
    
    	InFile = fopen(in_filename, "r");
    	if (InFile == NULL) 
    	{
    		printf("Error opening \"%s\"\n\n", in_filename);
    		file_error_flag = true;
    	}
    
    	/* Creates and Opens the Write File */
    	OutFile = fopen(out_filename, "w");
    	if (OutFile == NULL)
    	{
    		printf("Error opening \"%s\"\n\n", out_filename);
    		file_error_flag = true;
    	}
    
    	if (file_error_flag)
    	{
    		printf("\nProgram terminated due to FILE error!\n\n");
    		/* Identify problem file and close files that are open */
    		if (InFile == NULL)
    			printf("Error with %s\n",in_filename);
    		else
    			fclose(InFile);
    		if (OutFile == NULL)
    			printf("Error with %s\n",out_filename);
    		else
    			fclose(OutFile);
    	}
    	else /* Files open successfully */
    	{
    
    		load (InFile, ary_nbr, maxsize);
    		printtab (OutFile, ary_nbr, maxsize);
    		classifydata (ary_nbr, maxsize, &nCount_Pos, &nCount_Neg, &nCount_Zer);
    
    
    
    
    		
    		
    	}	
    
    	/* else file_error_flag */
    	return file_error_flag;
    
    	/* reports postive, negative, & zeros */
    	
    		/* positive */
    		printf("\nThe amount of positive numbers in the array are : %i\n", nCount_Pos);
    		fprintf(OutFile, "Number of positive numbers in the array are : %i\n", nCount_Pos);
    
    		/* negative */
    		printf("\nThe amount of negative numbers in the array are : %i\n", nCount_Neg);
    		fprintf(OutFile, "Number of positive numbers in the array are : %i\n", nCount_Neg);
    
    		/* zero */
    		printf("\nThe amount of zeros in the array are : %i\n", nCount_Zer);
    		fprintf(OutFile, "The amount of zeros in the array are : %i\n", nCount_Zer);
    
    	/* close files */
    		fclose(InFile);
    		fclose(OutFile);
    }
    
    int load (FILE *InFile, int *ary_nbr, int size)
    {
    	int i; /* array sunscript and loop control */
    
    	for (i = 0; i < size; ++i)
    	{
    		fscanf(InFile, "%i\t", &ary_nbr[i]);
    	}
    	
    	return ary_nbr[i];
    }
    
    void printtab (FILE *OutFile, int *ary_nbr, int size)
    {
    	int i;
    	for (i = 0; i < size; ++i)
    
    		fprintf(OutFile, "%7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\n", &ary_nbr[i]);
    		printf("%7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\n", &ary_nbr[i]);
    }
    
    void classifydata (int *ary_nbr, int size, int *nCount_Pos, int *nCount_Neg, int *nCount_Zer)
    {
    	int i;
    
    	*nCount_Pos = 0;
    	*nCount_Neg = 0;
    	*nCount_Zer = 0;
    
    	for(i = 0; i < size; i++)
    	{
    		if (ary_nbr[i] < 0)
    			*nCount_Neg++ ;
    		else if (ary_nbr[i] > 0)
    			*nCount_Pos++;
    		else if (ary_nbr[i] = 0)
    			*nCount_Zer++;
    	}
    
    //	return nCount_Pos, nCount_Neg, nCount_Zer;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Don't use \t. If you let %i alone, it will skip all whitespace (spaces, tabs, and new-lines) without any work on your part.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    ok. well im supposed to be reading from a file that contains this:

    Code:
     -738	-1611	-2179	  -16	-1181	-3965	 -968	 -415	 2747	 -437	 -842	 -443
      358	 3783	  588	 1061	 2300	-4718	 4678	 3118	    0	-2390	 1985	-1993
    -3287	  -96	-4650	    0	-3199	 4354	 4535	  945	  346	  402	 4690	-4762
        0	-3367	 1225	-1296	 -184	 4836	 1679	-4552	-2859	  919	-4590	 1014
     1580	 2094	 2014	 1596	-2026	  383	 2022	-1880	-1139	  572	 4732	-1595
     4175	-1532	 4725	  412	 4044	  525	  486	 2315	 2716	 2217	    0	 -668
     2450	-1728	 4646	 3433
    i simply try to restate this data and it comes up as:

    Code:
    1244592	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244596	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244600	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244604	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244608	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244612	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244616	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244620	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244624	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244628	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244632	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244636	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244640	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244644	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244648	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244652	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244656	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244660	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244664	 1245032	 1244348	 2147344384	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460	 -858993460
    1244668	 1245032	 1244348	 2147344384
    does this mean im printing addresses instead of values?! how did this happen

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What makes you think this line
    Code:
    printf("%7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\t %7i\n", &ary_nbr[i]);
    is even a little bit legal?

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    lol i dont know. I have the array printing correctly now. Do you know how i could go about printing 10 numbers a line? ive look all over the inet and cant really find anything!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pantherman34 View Post
    lol i dont know. I have the array printing correctly now. Do you know how i could go about printing 10 numbers a line? ive look all over the inet and cant really find anything!
    Really? Are you sure you just didn't recognize it when you saw it?
    Code:
    for (int i = 0; i < 10; i++) {
        printf("%d ", i);
    }

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    58
    yes that works fine for the first 10...i want to print the entire array, but 10 fields a line.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pantherman34 View Post
    yes that works fine for the first 10...i want to print the entire array, but 10 fields a line.
    So now that you know how to print 10 items on a line, do so repeatedly. Or alternatively, print each element in a loop, and decide based on [i] when to print a new-line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

Tags for this Thread