CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Load Array from Whitespace-delimited text file

    Your load() function is mostly fine. I don't know what you think you're returning at the end; at that point i is an invalid index. Since you ignore the return this isn't very important though. Potentially more important is what happens if there aren't exactly 100 numbers in the file. Is it possible for there to be fewer? If not, do you want to be able to catch that condition as an error? If so, you'll need to check the return value of fscanf(), and you'll want to return the actual number of values in the array.

    I have no idea what you're trying to do in your printtab() function. That's just strange.

    classifydata() looks to be fine.

    The fact that you have a return statement prior to the end of main() is probably wrong....I have no idea what you were hoping to accomplish with that.

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