simple string program

This is a discussion on simple string program within the C Programming forums, part of the General Programming Boards category; hey there, i have a very trivial program which is given a text file as a parameter, and basically has ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    simple string program

    hey there,
    i have a very trivial program which is given a text file as a parameter, and basically has to print any of the characters in the file which are characters (i.e. not numbers etc) to the screen.

    i am using the isprint() method, which should return true only if the passed integer is of type char or space.

    the program, at current, returns everything -regardless of whether it is character or int. if anyone could please tell me where i have made my error i would be greaty appreciative.

    thanks in advance, twans

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    char *read_text(char *);
    int encode(char *);
    int decode(char *);
    
    int main(int argc, char **argv) {
    	char *text;
    
    	if (argc != 2) {
    		printf("USAGE: %s filename\n", argv[0]);
    		exit(1);
    	}
    
    	text=read_text(argv[1]);
    	
    	printf("Characters and Strings:\n");
    
    	isString(text);
    	printf("%s\n", text);
    
    	free(text);	
    	return 0;
    }
    
    
    
    char *read_text(char *filename) {
    	FILE 		*filedes;
    	static char	*text;
    	int			file_len, 
    				i;
    
    	if(!(filedes = fopen(filename, "r"))) {
    		perror("fopen()");	
    		exit(2);
    	}
    
    
    
    	/* One quick way to find out a file's size... */
    	fseek(filedes, 0L, SEEK_END);
    	file_len = ftell(filedes);
    	rewind(filedes);
    
    #ifdef DEBUG
    	printf("File: %s; Length: %d\n", filename, file_len);
    #endif
    
    	if(!(text = (char *)malloc((size_t) (file_len + 1)))) {
    		perror("malloc()");
    		exit(3);
    	}
    
    	for(i = 0; i < file_len; i++) 
    		text[i] = fgetc(filedes);		
    	text[file_len] = (char)NULL;						/* NULL-terminate the character array */
    
    #ifdef DEBUG
    	for(i = 0; i <= file_len; i++)
    		if (text[i] == (char)NULL)
    			printf("NULL\n");
    		else
    			fputc(text[i], stdout);
    	exit(0);
    #endif
    
    	fclose(filedes);
    	return text;
    }
    
    int isString(char *tempText)
    {
    	int x;
    	while((x = *tempText) != '\0')
    	{
    		if(isprint(x))
    			{
    			*(tempText) = (char) x ;
    			}
    		*(tempText++);
    	}
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    How about an if
    Code:
    	if ( isString(text) ) printf("%s\n", text);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    hi salem, thanks for your reply. if you look at my isString method, however, it will always return 0. i was trying to use a pointer to the 'text' variable to modify it as required. therefore your 'if' solution is not applicable without modifying the structure of the function.

    .. i think in the isString function itself i am overlooking something, maybe with the pointer?
    Code:
    int isString(char *tempText)
    {
    	int x;
    	while((x = *tempText) != '\0')
    	{
    		if(isprint(x))
    			{
    			*(tempText) = (char) x ;
    			}
    		else { *(tempText) = ' '; }
    		*(tempText++);
    	}
    	return 0;
    }

  4. #4
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    I was just going to mention that the isString function always returns zero, twans, but you beat me to it...

    How can the isString function do anything for you if it always returns zero? Or can the function manipulate the values passed to it because of the pointers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. I'm not ask for ENTIRE program, only 1 Question !
    By Th3-SeA in forum C Programming
    Replies: 10
    Last Post: 10-01-2003, 01:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21