CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    4

    Load Array from Whitespace-delimited text file

    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**

    *** Keep in mind, C only, not C++ please

    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 \"&#37;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;
    }
    Last edited by pantherman34; April 30th, 2010 at 06:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured