is there is any library function for saving a program?

This is a discussion on is there is any library function for saving a program? within the C++ Programming forums, part of the General Programming Boards category; hi..... i want to know that is there is any library function to save the program output.for example i am ...

  1. #1
    Unregistered
    Guest

    Question is there is any library function for saving a program?

    hi.....
    i want to know that is there is any library function to save the program output.for example i am writing a program below

    #include <stdio.h>
    #include <conio.h>
    void main (void)
    {

    char string;
    printf ("ENTER STRING");
    gets (string);
    for (i=0;i <10;i++)
    {
    printf ("THE STRING IS "%s",string);
    }

    }

    ok then the user will input the string and it will appear as output ten times on the screen i want to ask that can i save that output by any library function or by any other means ? is it possible?


    please reply me

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    you could write your own function, example:

    Code:
    ostream file("output.txt");
    //..
    void Write(char *string)
    {
        file << string;
        cout << string;
    }
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    You should maybe re-post this on the C board, not C++.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #4
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    yeah it helped a lot if you want quick esponses.
    Email: Klinerr1@nc.rr.com || AIM: MisterSako || MSN: sakotheinsane@hotmail.com

    -the shroom has spoken

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You know it pains me to see how many people think cross posting and multiple posting are a good way of getting an answer. When I see two posts with similar titles I usually don't look at them. And I know I'm not alone on that!

    Anyways, see if this helps.

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    void main (void) 
    { 
        FILE *output = fopen("user_output.log", "a");
        char string; 
        printf ("ENTER STRING"); 
        gets (string); 
        for (i=0;i <10;i++)  { 
            printf ("THE STRING IS "%s",string);
            fprintf(output, "%s", string);
        } 
        fclose(output);
    }
    If this is more what you want you could also do this:

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    void main (void) 
    { 
        FILE *output = freopen("user_output.log", "a", stdout);
        char string; 
        printf ("ENTER STRING"); 
        gets (string); 
        for (i=0;i <10;i++)  { 
            printf ("THE STRING IS "%s",string);
        } 
        fclose(output);
    }
    The second one diverts output to the file.

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I don't know enough about C functions, I ignore them as soon as I see the code

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Master5001,

    You know it pains me to see how many people...
    No, you're not alone at all. I'm afraid that, with the new school year just around the corner, the floodgates may well open on this type of activity.

    Patience...lots of patience.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Or you could simply use some form of redirection from the command line.

    >Anyways, see if this helps.
    Code:
    #include <stdio.h>
    /* Nonstandard, are you sure you need it? */
    #include <conio.h> 
    /* Flat out wrong, void main is undefined */
    void main (void) 
    {
        /* Where do you test for a successful open? */
        FILE *output = fopen("user_output.log", "a");
        /* The identifier string is reserved for the implementation.
        ** And it should be either string[SIZE] or *string. As it is
        ** string is only one char when you treat it as a string.
        */
        char string; 
        printf ("ENTER STRING");
        /* gets is unsafe, it should never be used. string also 
        ** wasn't defined as a pointer or array, so this call is
        ** invalid. If string were a pointer then you would need
        ** to allocate memory as well. */
        gets (string);
        /* i hasn't been declared. */
        for (i=0;i <10;i++)  {
            /* You have one too many " in that call to printf. */
            printf ("THE STRING IS "%s",string);
            fprintf(output, "%s", string);
        } 
        fclose(output);
    }
    This would be a bit better:
    Code:
    #include <stdio.h>
    
    int main (void) 
    {
      int i;
      char str[BUFSIZ];
      FILE *output = fopen ( "user_output.log", "a" );
    
      if ( output != NULL ) {
        printf ( "ENTER STRING: " );
        if ( fgets ( str, sizeof str, stdin ) != NULL ) {
          for ( i = 0; i < 10; i++ )  { 
            printf ( "THE STRING IS %s", str );
            fprintf ( output, "%s", str );
          } 
          fclose ( output );
        }
      }
      return 0;
    }
    -Prelude
    Last edited by Prelude; 07-29-2002 at 09:59 AM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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