Thread: Help, weird output

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    Talking Help, weird output

    Hi, first post. I'm trying to read numbers from a file with strtok and for some reason the last value in the file is printed twice.
    [Example] input: 1 2 5 word 4
    output: 1 2 5 4 4
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 100
    int isNumeric(char*);
    int main (int argc,char* argv[]){
    	if(argc!=2){
    		puts("Usage: ./HW1 <filename>.txt");
    		return 0;
    	}
    	FILE* cfPtr;
    	if((cfPtr=fopen(argv[1],"r"))==NULL){
    		puts("File could not be opened");
    		return 0;
    	}
    	double test;
    	char line[SIZE];
    	char* strPtr;
    	char* tokenPtr;
    	while(!feof(cfPtr)){
    		fgets(line,SIZE,cfPtr);
    		tokenPtr=strtok(line," \t\n");
    		while(tokenPtr!=NULL){
    			printf("tokenPtr:%s\n",tokenPtr);
    			if(isNumeric(tokenPtr)){
    				test=strtod(tokenPtr,&strPtr);
    				printf("%.0f\n", test);
    			}
    			tokenPtr = strtok(NULL," \t\n");
    		}
    	}
    	fclose(cfPtr);
    	return 0;
    }
    int isNumeric(char* token){
    	int i=0;
    	if(token[i]=='-'){
    		++i;
    	}
    	if(token[i]=='\0'){
    		return 0;
    	}
    	while(token[i]!='\0'){
    		if(isdigit(token[i])==0){
    			return 0;
    		}
    		++i;
    	}
    	return 1;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Isn't the extra character a 1 and not a 4?

    The problem is that you are not checking the return value of fgets(). From the linked page:

    If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged).
    You probably have a newline at the end of your input.
    fgets() reads the first line and later tries to read the second - which is empty - and returns NULL. Because it doesn't modify your string in this case, line still has the old contents (which is something like ['1', '\0', '2', '\0', ...], because of the operations done by strtok()). So the next call to strtok() reads the 1 from line and stops at the NULL character, but this gets printed to the screen as it's a valid number.
    Last edited by koplersky; 01-20-2015 at 05:38 PM.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    Wink

    Quote Originally Posted by koplersky View Post
    Isn't the extra character a 1 and not a 4?

    The problem is that you are not checking the return value of fgets(). From the linked page:



    You probably have a newline at the end of your input.
    fgets() reads the first line and later tries to read the second - which is empty - and returns NULL. Because it doesn't modify your string in this case, line still has the old contents (which is something like ['1', '\0', '2', '\0', ...], because of the operations done by strtok()). So the next call to strtok() reads the 1 from line and stops at the NULL character, but this gets printed to the screen as it's a valid number.
    Thank you, this fixed the problem. It's been a long winter break and I'm a bit rusty.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird output
    By kantze in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2006, 12:05 PM
  2. my output is weird
    By panfilero in forum C Programming
    Replies: 1
    Last Post: 11-10-2005, 02:11 PM
  3. float gives weird output
    By Xarr in forum C Programming
    Replies: 4
    Last Post: 05-25-2004, 07:44 PM
  4. weird code output
    By noob2c in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2003, 07:34 AM
  5. weird output using cos
    By Jan79 in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2003, 07:22 AM

Tags for this Thread