scanf function

This is a discussion on scanf function within the C Programming forums, part of the General Programming Boards category; I am developing windows console based application. And I need to read characters (entered by kbd) on console. I am ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    scanf function

    I am developing windows console based application. And I need to read characters (entered by kbd) on console. I am using scanf function. Is there any wrong in using C library in Windows application?

    Please let me know.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Using scanf is ok in console (that is what you meant, right? Not windows API?) though I suggest you use fgets instead. Scanf is easily crashed.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    There is nothing wrong with using C standard libraries in a console application. That is how they are written.

    As long as you stick with the standard C libraries, you will be able to take that code and compile it on any machine, regardless of OS.

    Where you get into trouble is when you start using platform specific libraries and header files.
    IE:
    #include <windows.h> // Windows
    #include <sys/*.h> // Unix & Linux

    Now, as far as scanf goes, there are better ways:

    Instead of doing this:
    Code:
     scanf("%d", &val);
    Use fgets, and sscanf if they are numerical values (integers, floats, etc).
    Or use straight fgets to read in strings:
    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* fgetstring -- function that uses fgets, but removes the new-line character, and places a null terminating character at the end of the string */
    char* fgetstring(char* s, size_t size, FILE* stream)
    {
    	char* status;
    
    	status = fgets(s, (int)size, stream);
    	if(status != NULL)
    	{
    		size_t strlength = strlen(s);
    		
    		if(s[strlength - 1] == '\n')
    		{
    			s[strlength - 1] = '\0';
    		}
    	}
    	
    	return status;
    }
    
    int main(void)
    {
    	int iVal;
    	float fVal;
    	char myString[20];
    	char tempString[1024];
    
    
    	printf("Enter an integer value\n");
    	fgetstring(tempString, sizeof(tempString), stdin);
    	sscanf(tempString, "%d", &iVal);
    
    	printf("Enter a Floating point value\n");
    	fgetstring(tempString, sizeof(tempString), stdin);
    	sscanf(tempString, "%f", &fVal);
    
    	printf("Enter a string of characters\n");
    	fgetstring(myString, sizeof(myString), stdin);
    	/* Use this if you want to store the string in temporary
                        memory first
                    fgetstring(tempString, sizeof(tempString), stdin);
                    memcpy(myString, tempString, sizeof(myString));*/
    
    	printf("iVal = %d\n", iVal);
    	printf("fVal = %.2f\n", fVal);
    	printf("myString = %s\n", myString);
    
    	return 0;
    }

    Hope that helps.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Thanks for reply.

    But after building the code in VC++ 6.0. I got following error.

    \microsoft visual studio\vc98\include\stdlib.h(298) : error C2040: 'malloc' : 'void *(unsigned int )' differs in levels of indirection from 'void *(unsigned int )'

    c:\program files\microsoft visual studio\vc98\include\string.h(105) : error C2040: 'strcpy' : 'char *(char *,const char *)' differs in levels of indirection from 'char *(char *,const char *)'

    So is there any restriction on the code.

    Thanks,

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    you're compiling your code as C? (*.c) or as C++? (*.cpp)

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Compiling code as C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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