print values of function to file

This is a discussion on print values of function to file within the C Programming forums, part of the General Programming Boards category; hello you. Code: /* an array will be printed in the desired file. fputs doesnt work with count() function. why? ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    print values of function to file

    hello you.

    Code:
    /*	an array will be printed in the desired file.
      	fputs doesnt work with count() function. why?
    */
    
    #include<stdio.h>
    #include<conio.h>
    
    int count();
    
    main ()
    {
       FILE *datei;                             //filename declared
       int hello[100];                          //this is going to be printed
       int i, stop;
       char filename[20];
    
       int count();
    
    
       printf("enter filename: ");
       scanf("%s", &filename);
       datei = fopen (filename, "w+");
    
    
    
       if (datei == NULL)
       	printf("an error occured\n");
    
       else
       	printf("an one dimensional array is going to be printed in your file\n\n");
          fputs(count(), datei);             //this line is supposed to write
                                             //the return values of int count()
                                             // to the file called datei
    
    
    	scanf("%d", &stop);                   //this line only exists to stop
                                             //the program, no deeper sense
    
       return 0;
    }
    int count()
    {
       int i, hello[100] ;
    	for(i=0;i<=100;i=i++)
    		hello[i] = 10;
    	for(i=0;i<=100;i++)
    		printf("%d\n", hello[i]);
    
       return(hello[i]);
    }
    this code is a little documented, so you can easier understand the purpose of this program.

    the fputs line doesnt work with the function.
    i typed: fputs(count(), datei);
    my compiler doesnt seem to like it.

    how do i print a whole function to a file? this time the count() function.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, declare your prototype correctly like this:
    >>int count(void);
    ... this is different from the way you have it, and has a different meaning. The same goes for main():
    >>int main(void)
    [edit]Just noticed you have 2 prototype for count, you only need the one.

    This is wrong:
    >>scanf("%s", &filename);
    it should be:
    >>scanf("%s", filename);

    I have restructured this section of code, not the location of the braces:
    Code:
        if (datei == NULL)
        {
            printf("an error occured\n");
        }
        else
        {
            printf("an one dimensional array is going to be printed in your file\n\n");
            fputs(count(), datei); /* This is still wrong, as discussed below */
            fclose(datei);
        }
    You need to fclose() your file too.

    >>for(i=0;i<=100;i=i++)
    This is exceeds the array bounds, remember it goes from 0 to 99. (Same for the return statement). And you've goofed with the part I've bolded.

    Now, to fputs(), you are using the function incorrectly, its first arg is a char array, not an int like count() returns. Try this instead:

    >>fprintf(datei, "%d", count());

    This will only print out 1 element of the array. If you want all of them, you need to loop through it, outputing each one individually, like you have already done with the printf() statement in the count() function.

    [edit]Darn, beat
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    first of all thanks for you help

    i changed all the wrong parts and my program seems to run
    smoothly, but why is the number written to the file random, and not 0 or 99?

    ill figure out the loop later

    threadhead

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by threahdead
    ... but why is the number written to the file random, and not 0 or 99?
    Are you still doing this at the end of count():
    >return(hello[i]);

    If so, i is out of the array bounds, so you'll be picking up junk memory. You must return (hello[i-1]); to get the last element of the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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