Thread: * functions *

  1. #1
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    * functions *

    I've got a question about functions.....here is the current code that I have...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main()
    {
    	int tall,wide,position,x=0,y=0,a=0;
    	char line[50],common[50];
    	FILE *ifp;
    
    
    	printf("\n\n  Opening OBJECT.DAT ");
    	
    	if ((ifp=fopen("object.dat", "r")) == NULL)
    	{
    		printf("\n  * error opening file object.dat *");
    	}
    
    	fgets(line,100,ifp);
    	position=(strlen(line)-1);
    
    	while(x<position)
    	{
    		
    		common[a]=line[x];	
    	
    		if (line[x]='\0')
    		{     a++; break;	}
    		x++; a++; position--;
    		
    	}
    
    	tall=atoi(common);  common==0; a=0;
    
    	while(a<position)
    	{
    		common[a]=line[x];
    
    		if (line[x]='\0')
    		{     x++; break;	}
    		x++; a++; position--;
    	}
    
    	wide=atoi(common);
    
    	printf("\n\n\tHeighth: %i",tall);
    	printf("\n\tWidth:   %i\n\n\t",wide);
    	
    	
    }
    This code is made to read in 2 numbers on the first line of the objects.dat file...these 2 numbers will be the Heighth and the Width of our giant rectangle...I know there is a much better way to write that code to not involve 2 giant while statements...Can anyone suggest a method?

    Also...we will be needing to do a very similar thing with that code for leading lines 2 up to lines 40 or so.....but these lines contain 3 numbers seperated by spaces....since I will be calling it over and over again i figured it would be best to make that entire thing as a function which we can pass things too and have constantly reading from the file when we need to call it...but how far can variables in a function go? If we call a function we make...and it reads the file...seperates the 2 items...and stores them in a variable....will the scope and range of the variable allow us to use that variable in the rest of our main() function? anyone suggest a way to write that code into a nice compact function? Thanks.

  2. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    also...what exactly does the:

    fgets(line,100,ifp);

    line do....i understand the concept..it stores the input in variable line....it the second parameter, 100, the amount of characters to read in, and i know the last param is the file pointer....how can you make fgets goto the next line?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by SavesTheDay
    also...what exactly does the:

    fgets(line,100,ifp);

    line do....i understand the concept..it stores the input in variable line....it the second parameter, 100, the amount of characters to read in, and i know the last param is the file pointer....how can you make fgets goto the next line?
    A file pointer points to the current position in the file. Thus, if you've just called 'fgets', which reads a line of text, you're automaticly on the beginning of the second line once the call is done.

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

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Hey man, wasup!!!!

    I incremented the string instead of using an array:

    >common[a] = *line;

    then increment line

    >line++;

    By doing this you do not need the second loop, just do this

    >wide = atoi(line);

    since line is just pointing to the second number at that point.


    ---------

    fgets() will automatically go to the next line each time you call it.

    To get the rest of the numbers, I had to use fgets() and assign it to a temp string, the pass the string(char pointer) and a int array(pointer also) to a function I made. In the function I broke the string down and assigned it to numbers [0], [1], and [2] in the int array.

    You need to do this in a loop and check the return value of fgets() to know when to break.

    PHP Code:
    while(1)
    {
            if ( 
    fgets(str100ifp) == NULL )
                      break;
            else
                      
    getvalues(strnum);

    where getvalues() is a function that break the string into ints and store them in the array 'num'.

    Hope this helps some.
    Last edited by drharv; 04-23-2002 at 06:45 PM.

  5. #5
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    hey dude! whats going on....of course you know me just now starting to work on this! hope Discrete ended up ok...did WAR III work for you? tell Neil im sorry i havent made a copy for him yet...ill try to get around to that...im taking Trig and Discrete this summer...im still in the WWW/Foundations class next Spring and in the DigiTech Class though....email me sometime! thanks...

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Lightbulb

    remember to use the "int" in front of main. It may not have any real affect to the program but the compiler and processor will appreciate it.
    Reason: if you look at the memory assigned in assembly after it is broken into assembly you will see that with int the memory is in the top of the stack, rather than if it were void and also, int allows main to return a 0 or 1
    1 being the error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM