From global to local variable

This is a discussion on From global to local variable within the C Programming forums, part of the General Programming Boards category; Hi, I need some help to convert global variables into local variables. How the hell do i do that when ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    From global to local variable

    Hi,

    I need some help to convert global variables into local variables. How the hell do i do that when im operating with .txt files.

    The following is a part of my program.

    Thanks

    Code:
    #define infilename "input.txt"
    #define outfilename "output.txt"
    
    
    int main(void)
    
    {
    
    	FILE *infile, *outfile;
    
    	if (!(infile = fopen(infilename, "r")) || !(outfile = fopen(outfilename, "w")))
    	{
    		puts("Fejl: kan ej åbne filer\n");
    		exit(EXIT_FAILURE);
    	}
    	CreatePwList(infile, outfile);
    	fclose(infile); fclose(outfile);
    	return 1;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    You don't have any truly global variables there. You have two #defines, which are substituted into the code during the preprocessing phase and aren't variables at all; the are, in fact, constants.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Well i want to eliminate them. Any idea how i can do that without ruining the code?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wellll... you could try this...
    Code:
    if (!(infile = fopen("input.txt", "r")) || !(outfile = fopen("output.txt", "w")))
    Which is how the compiler sees it.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Thanks for the help. What about this one, is this also a constant (or variable?), and how do i eliminate it?

    Code:
    #define MAXCHARS 100
    
    void CreatePwList(FILE *in, FILE *out)
    {
    	char templine[MAXCHARS], fornavn[MAXCHARS];
    	char efternavn[MAXCHARS], klasse[MAXCHARS];
    	int elevnr=0, i=0;
    
    	while(fgets(templine, MAXCHARS, in))
    	{
    		elevnr++;
    		i = GrabSubStr(fornavn, templine, 0);
    		i = GrabSubStr(efternavn, templine, i);
    		    GrabSubStr(klasse, templine, i);
    
    		sprintf(templine, "%c%c%d;%c%c%d%s;%s;%s;%s\n",
    				fornavn[0], efternavn[0], elevnr,
    				fornavn[0], efternavn[0], elevnr, "DTU",
    				klasse, fornavn, efternavn);
    		printf(templine);
    		fputs(templine, out);
    	}
    }

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Never mind, i just found out how to fix it

    This works for me:

    Code:
    void CreatePwList(FILE *in, FILE *out)
    {
    	char templine[100], fornavn[100];
    	char efternavn[100], klasse[100];
    	int elevnr=0, i=0;
    
    	while(fgets(templine, 100, in))
    	{
    		elevnr++;
    		i = GrabSubStr(fornavn, templine, 0);
    		i = GrabSubStr(efternavn, templine, i);
    		    GrabSubStr(klasse, templine, i);
    
    		sprintf(templine, "%c%c%d;%c%c%d%s;%s;%s;%s\n",
    				fornavn[0], efternavn[0], elevnr,
    				fornavn[0], efternavn[0], elevnr, "DTU",
    				klasse, fornavn, efternavn);
    		printf(templine);
    		fputs(templine, out);
    	}
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    In this case, it's a step backwards, because everytime you change the size, you have to change it in 5 places instead of one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    In this case, it's a step backwards, because everytime you change the size, you have to change it in 5 places instead of one.
    Yep, I was thinking the same thing.

    #define does have it's benefits.

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    Yea, i know it's a bit silly, but the thing is that we aren't allowed to use global variables in our assignment. Don't know why.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Those aren't global variables. They are not even variables, they are just bits of text that the compiler will replace directly in your code before it compiles it.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by urban1990 View Post
    Yea, i know it's a bit silly, but the thing is that we aren't allowed to use global variables in our assignment. Don't know why.
    First of all a #define is not a variable, it's a constant that can't be changed within the program.

    Secondly it's just plain unreasonable to think you can write programs any bigger than "tic tac toe" without at least a couple of variables that have to be shared. Global variables have their places... interprocess signalling, settings, behavioral flags, window handles etc. It might be fun for your assignment, but it ain't practical in a project of any real size.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    No, actually, it's a good thing that you aren't allowed to use global variables. Not that they do not have their places; they should be used sparingly, and carefully.
    Chances are that, in a small projects, you don't even need them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick: Alternative to using global variable
    By abrownin in forum C++ Programming
    Replies: 1
    Last Post: 05-02-2010, 04:25 AM
  2. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  3. global struct variable best use
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 05:08 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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