Like Tree2Likes

printf doesnt work

This is a discussion on printf doesnt work within the C Programming forums, part of the General Programming Boards category; Code: #define ACCESSCHKDATA "\"%SYSTEMFILES%\Program Files\Jagex Limited\file disk utilities\1\accessdata.dat\"" #include <stdio.h> /* for scanf and printf*/ #include <stdlib.h> /* for abort();*/ ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35

    printf doesnt work

    Code:
    #define	ACCESSCHKDATA "\"%SYSTEMFILES%\\Program Files\\Jagex Limited\\file disk utilities\\1\\accessdata.dat\""
    
    #include <stdio.h> /* for scanf and printf*/
    #include <stdlib.h> /* for abort();*/
    
    void help(void);
    void rightsPriviledge(void);
    void editRightsPriviledge(void);
    
    void main(void)
    {
    	auto int choice;
    	help();
    	printf("Enter you choice:");
    	scanf_s(" %i ", &choice);
    	switch (choice)
    	{
    		case 1:	rightsPriviledge();
    				break;
    
    		case 2: editRightsPriviledge();
    				break;
    
    		default: help();
    	}
    }
    
    void help(void)
    {
    	printf("\n\n");
    	printf("Accesschecker v1.0 - Reports permissions for objects and files.\n");
    	printf("Copyright (C) 20011-2012 Edmund Uba\n");
    	printf("Jagex Limited - www.jagexltd.zapto.org\n");
    	printf("\n");
    	printf("	1	Displays all rights assinged to all users.\n");
    	printf("	2	Edit a windows account right.\n");
    	printf("\n\n");
    }
    
    void rightsPriviledge(void)
    {
    	FILE *input;
    	errno_t err;
    	
    	/*For the input file*/
    	err=fopen_s(&input, ACCESSCHKDATA, "r");
    	if(NULL==input)
    	{	printf("Error in opening Input file");
    		abort();
    	}
            printf("Test chose 1");
    		if(fclose(input)==EOF)
    	{	printf("Error closing %\n", input);
    		abort();
    	}
    }
    
    void editRightsPriviledge(void)
    {
    	printf("Test choice 2");
    }
    I deleted everything and started from scratch. the problem now is that printf statements in the functions help(), editRightsPriviledge(), and rightsPriviledge() does not display. My program sucks by the way... XD I dont know why it doesnt print.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #define	ACCESSCHKDATA "\"%SYSTEMFILES%\\Program Files\\Jagex Limited\\file disk utilities\\1\\accessdata.dat\""
    You need to escape those.
    Code:
    	{	printf("Error closing %\n", input);
    Same with that. Don't you compile with warnings on?
    Code:
    void main( void )
    Should be:
    Code:
    int main( void )
    {
        return 0;
    }
    What is all this _s stuff?


    Quzah.
    Last edited by quzah; 08-29-2011 at 01:29 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    that's the problem.... I'm using Visual Studio 2005 and U have to use the _s... anyways i tried your suggestion but still nothing works... i tried also putting the fopen in comments but still nothing works... i dont know also the escape sequence for %
    Code:
    //#define	ACCESSCHKDATA "\"%SYSTEMFILES%\\Program Files\\Jagex Limited\\file disk utilities\\1\\accessdata.dat\""
    
    #include <stdio.h> /* for scanf and printf*/
    #include <stdlib.h> /* for abort();*/
    
    void help(void);
    void rightsPriviledge(void);
    void editRightsPriviledge(void);
    
    int main(void)
    {
    	auto int choice;
    	help();
    	printf("Enter you choice:");
    	scanf_s(" %i ", &choice);
    	switch (choice)
    	{
    		case 1:	rightsPriviledge();
    				break;
    
    		case 2: editRightsPriviledge();
    				break;
    
    		default: help();
    	}
            return 0;
    }
    
    void help(void)
    {
    	printf("\n\n");
    	printf("Accesschecker v1.0 - Reports permissions for objects and files.\n");
    	printf("Copyright (C) 20011-2012 Edmund Uba\n");
    	printf("Jagex Limited - www.jagexltd.zapto.org\n");
    	printf("\n");
    	printf("	1	Displays all rights assinged to all users.\n");
    	printf("	2	Edit a windows account right.\n");
    	printf("\n\n");
    }
    
    void rightsPriviledge(void)
    {
    	//FILE *input;
    	//errno_t err;
    	
    	/*For the input file*/
    	//err=fopen_s(&input, ACCESSCHKDATA, "r");
    	//if(NULL==input)
    	//{	printf("Error in opening Input file");
    	//	abort();
    	//}
            printf("Test chose 1");
    	//	if(fclose(input)==EOF)
    	//{	printf("Error closing \n", input);
    	//	abort();
    	//}
    }
    
    void editRightsPriviledge(void)
    {
    	printf("Test choice 2");
    }

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Quote Originally Posted by edmund085 View Post
    that's the problem.... I'm using Visual Studio 2005 and U have to use the _s
    No you don't, it is just suggested. At least that is how it was the last time I used VS. There really is no reason to use Microsoft's "safe" functions anyway. At least research how they really work versus their original versions and make the decision for yourself.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    k... i decided t use the safe functions as long as I don't receive warnings. But still the problem is still there... printf wont show or display once called in the switch statement.... i checked it many times and nothing si working...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You escape % the same way you escape a \ character.

    \% = insert/print a %
    \\ = insert/print a \\
    ... etc

    The documentation for printf or scanf will tell you that much. Or the section where they cover strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    In a printf() format string, the way to print a % sign .... is two % signs in sequence.
    quzah likes this.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    I'm using Visual Studio 2005 and U have to use the _s
    As stated above, no....you don't. Read Security Enhancements in the CRT and take note of the _CRT_SECURE_NO_WARNINGS #define, which you can add at the top of the code to get rid of those warnings.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    it still doesnt solve the problem, i already place it in a comment but still it doesnt work, or the printf() doesnt display......

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > printf("Test chose 1");
    I don't suppose adding "\n" on the end of the string, or calling fflush(stdout) would fix things?
    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.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by grumpy View Post
    In a printf() format string, the way to print a % sign .... is two % signs in sequence.
    Must have been tired. XD


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    guys, can yu try a different program r create a different program, as lng as you make a switch and inside the switch you call a function that contains printf() statements, if it works please post it, if not please post also... cause I want to know the problem....

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    #define	ACCESSCHKDATA "\"%SYSTEMFILES%\\Program Files\\Jagex Limited\\file disk utilities\\1\\accessdata.dat\""
    There is no such environment variable (by default at least)
    Use either "%SystemDrive%\\Program Files" or better yet "%ProgramFiles%". Although I'm not sure if fopen supports paths with env vars in them.

    Code:
    scanf_s(" %i ", &choice);
    Why do you have a trailing whitespace here?

    Code:
    abort();
    Don't use. It doesn't flush buffers (which might be why you're not seeing any output), and it shows the "This application has stopped working" dialog. Just return from the function, or use exit() instead.
    Or if you are just using it for the option to break to the debugger in case of errors you can use something like
    Code:
    #include <windows.h>
    #include <intrin.h>
    #define DEBUGBREAK() {if(IsDebuggerPresent()) __debugbreak();}
    Salem likes this.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by edmund085
    guys, can yu try a different program r create a different program, as lng as you make a switch and inside the switch you call a function that contains printf() statements, if it works please post it, if not please post also... cause I want to know the problem....
    This works:
    Code:
    #include <stdio.h>
    
    void one(void)
    {
        printf("%s\n", "one");
    }
    
    void two(void)
    {
        printf("%s\n", "two");
    }
    
    void three(void)
    {
        printf("%s\n", "three");
    }
    
    int main(int argc, char *argv[])
    {
        switch (argc)
        {
        case 1:
            one();
            break;
        case 2:
            two();
            break;
        default:
            three();
        }
        return 0;
    }
    But of course, I knew it would work even before I wrote the first line. Next time, show us the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jul 2011
    Location
    Butuan City, Philippines
    Posts
    35
    hmmm.... very odd... our program is just desame but why is it mines is not working...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why doesnt this work?
    By davo666 in forum C Programming
    Replies: 2
    Last Post: 01-04-2009, 05:59 AM
  2. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  3. How come this doesnt work?
    By correlcj in forum C Programming
    Replies: 2
    Last Post: 07-11-2002, 06:36 PM
  4. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 05:26 PM
  5. Why doesnt this work?
    By dougaerb in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2001, 08:51 AM

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