Thread: Problems with batch files

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    Problems with batch files

    I'm trying to write a program that makes a batch file that you just have to double to compile a program instead of using a big IDE that takes up CPU power or using the command line which gets clunky when you're debugging and you have to run the programming a bajillion times.

    here's my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //Don't walk in front of me, I may not follow.
    //Don't walk behind me, I may not lead.
    //Just walk beside me and be my friend.
    void main (void) {
    	FILE *compileThis;
    	char nameOfCFile[1000];
    	char commands[1000]; 
    	strcpy(commands, "gcc ");
    	strcat(commands, nameOfCFile);
    	strcat(commands, ".c \n");
    	strcat(commands, "pause");
    	printf("%s", commands);
    
    	//find out the name of the file to be compiled
    	printf("\n\n\nPlease enter the name of your program (not including extension):");
    	scanf("%s", &nameOfCFile);
    	
    	
    	
    	compileThis=fopen("compileYourProgram.bat", "w");
    	fprintf(compileThis, commands);
    	
    }
    But for whatever reason say when I typed in someCode when it asks me for the name of my program but the batch file doesn't work. When I checked the batch file, it had the following code in it:
    Code:
    gcc 2‘|.c 
    pause
    and the batch file wouldn't recognize this stuff. What going wrong?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    fprintf(compileThis, "%s", commands);
    is more better.

    Maybe you should do this:
    Code:
    strcat(commands, nameOfCFile);
    strcat(commands, ".c \n");
    strcat(commands, "pause");
    later in the program...like after "nameOfCFile" could be something meaningful
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Quote Originally Posted by MK27 View Post
    [code]

    later in the program...like after "nameOfCFile" could be something meaningful
    What do you mean?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In the Original Post, you execute that before the scanf call to input "nameOfCFile", which is to say the value of "nameOfCFile" at that time is the undefined garbage you found in your output file.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Worked! Thanks a bunch!

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could do the same thing with a simple VB Script also.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems saving audio files
    By JoeJTaylor in forum C++ Programming
    Replies: 0
    Last Post: 04-23-2006, 03:10 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  3. Replies: 6
    Last Post: 08-13-2003, 04:35 AM
  4. Problems openning huge files
    By acoelho74 in forum C Programming
    Replies: 4
    Last Post: 02-11-2003, 07:02 PM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM