0

hi!

i'm trying to write the code to open the file and read the contents and write to another file with uppercase but the problem i'm having is path problem everything seems pretty fine but it says Steam != NULL , as long as i know its could'nt locating the file.

NB: i'm using Visual C++ 2008 as IDE.

Please have a look at it and correct me if i'm wrong.
Br.

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(void)
{
	
	int ch; 

	int NumAlpha = 0, NumDigit = 0, NumLower = 0, NumUpper = 0, Count = 0, Place = 0;
	char *my_word, *my_word2, *my_word3;
	
	my_word = "dog";
	my_word2 = "cat";
	my_word3 = "rat";

	long int pos;
	const char readerfilepath[500] = "D:\\TEMP\\readObject.txt";
	const char writerfilepath[500] = "D:\\TEMP\\writeObject.txt";
	
	
	FILE *ipfileptr;
	FILE *opfileptr;

	ipfileptr = fopen(readerfilepath, "r");
	if (ipfileptr == NULL )
	{ 
		printf("Reader File not opened!\n"); 
		exit(8);
	}
	else
	{
	printf("Reader File opened successfuly!\n");
	}

	opfileptr = fopen(writerfilepath, "w+");
	
	if (ipfileptr == NULL )
	{ 
		printf("Writer File not opened!\n"); 
		exit(8);
	}
	else
	{
	printf("Writer File opened successfuly!\n");		
	} 
			while ((ch = getc(ipfileptr)) != EOF){
			
		if (isalpha(ch))
			NumAlpha++;	/* add 1 to NumAlpha count */
		
			if (islower(ch))
				NumLower++;	 /* add 1 to NumLower count */
		
			else
				if (isupper(ch))
				NumUpper++;	 /*add 1 to NumAlpha count */
					
		else

			if (isdigit(ch))
				NumDigit++; /*add 1 to digit count*/
			
			
			//ch = putc(toupper(ch),opfileptr);
			
			if (ch == *my_word ||*my_word2 || *my_word3){
				Count++;

				if((pos = ftell(ipfileptr)) != EOF){
					Place++;
		}	
		
					
	}
		ch = putc(toupper(ch),opfileptr);
		 	
			
	}

	printf("\nNumAlpha = %d NumDigit = %d NumLower = %d NumUpper = %d\nCount = %d\nPosition = %d\n", NumAlpha, NumDigit, NumLower, NumUpper ,Count,Place);
//	getchar();


		fclose(ipfileptr);
		fclose(opfileptr);


	return 0;
	}
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by j_cart007
1

you can't do this:

if (ch == *my_word ||*my_word2 || *my_word3)

you have to specify what you're testing for each OR'ed condition.

if (ch == *my_word || ch == *my_word2 || ch == *my_word3)
1

Also you are not writing C++ code so all your variable definitions need to be at the top of your functions, before any executable statements.

And for us to follow your code, use proper formatting

0

can u have at it again pls and sorry about any mistake again.
Br.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(void)
{
	
	int ch; 
	long int pos;
	int NumAlpha = 0, NumDigit = 0, NumLower = 0, NumUpper = 0;
	int Count = 0, Place = 0;
	
	char *my_word, *my_word2, *my_word3;
		
	const char readerfilepath[500] = "D:\\TEMP\\readObject.txt";
	const char writerfilepath[500] = "D:\\TEMP\\writeObject.txt";
	
	my_word = "dog";
	my_word2 = "cat";
	my_word3 = "rat";	
	
	FILE *ipfileptr;
	FILE *opfileptr;

	ipfileptr = fopen(readerfilepath, "r");
		if (ipfileptr == NULL )
		{ 
			printf("Reader File not opened!\n"); 
			exit(8);
		}
	else
		{
			printf("Reader File opened successfuly!\n");
		}

	opfileptr = fopen(writerfilepath, "w+");
	
		if (ipfileptr == NULL )
		{ 
			printf("Writer File not opened!\n"); 
			exit(8);
		}
	else
		{
			printf("Writer File opened successfuly!\n");		
		} 
		

	while ((ch = getc(ipfileptr)) != EOF){

		if (isalpha(ch))
			NumAlpha++;	/* add 1 to NumAlpha count */
		
			if (islower(ch))
				NumLower++;	 /* add 1 to NumLower count */
			else
				if (isupper(ch))
				NumUpper++;	 /*add 1 to NumAlpha count */
					
		else

			if (isdigit(ch))
				NumDigit++; /*add 1 to digit count*/
			
			
			
			
			if (ch == *my_word ||ch == *my_word2 || ch == *my_word3)
			{
				Count++;

				if((pos = ftell(ipfileptr)) != EOF)
				{
					Place++;
				}	
		
				
			}

	ch = putc(toupper(ch),opfileptr);
 	
			
	}

	printf("\nNumAlpha = %d NumDigit = %d NumLower = %d NumUpper = %d\nCount = %d\nPosition = %d\n", NumAlpha, NumDigit, NumLower, NumUpper ,Count,Place);

	fclose(ipfileptr);
	fclose(opfileptr);


	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.