how to print it to a file

This is a discussion on how to print it to a file within the C Programming forums, part of the General Programming Boards category; i have created a simple dumb err what ever you want to call it... but anyways i did this for ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    how to print it to a file

    i have created a simple dumb err what ever you want to call it... but anyways i did this for practice structures but i want to know how can i make the info print to a file... im using linux btw..

    Code:
    struct data{
          char fname[25];
          char lname[25];
          char fnumb[10];
       }ph;
    
    
    int main()
     {
        printf("\nType in the first and last one of the person\n");
        printf("please add a space when typing the last name\n\n");
        scanf("%s %s",&ph.fname, &ph.lname);
    
        printf("\n Please type in the persons phone number\n");
        scanf("%s",&ph.fnumb);
    
        printf("Name: %s  %s\n",ph.fname, ph.lname);
        printf("Phone#:  %s\n",ph.fnumb);
    
        return 0;
    
        }
    thanx

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    1. #include <stdio.h>

    2. Declare file variable: FILE *myfile;


    3. Open file :: myfile = fopen("filename","w");


    4. Use fprintf instead of printf.

    5. close file fclose(myfile);



    Mr. C.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One slight change:

    3a. Add the following lines below step 3:
    Code:
    if( myfile == NULL )
    {
        printf("Fatal Error. Could not open file for writing.\n");
        exit( EXIT_FAILURE );
    }
    Alternately, you can use 'exit(0)' instead of 'EXIT_SUCCESS' or EXIT_FAILURE.

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

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    ok this is how i did it.... i was using fprintf and iwas getting lots of errors.. but i did it this way and all it did was just creat the file info... check it out..

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct data{
          char fname[25];
          char lname[25];
          char fnumb[10];
       }ph;
    
    
    int main()
     {
    
      FILE *info;
    
        printf("\nType in the first and last one of the person\n");
        printf("please add a space when typing the last name\n");
        scanf("%s %s",&ph.fname, &ph.lname);
        puts("\n\n");
    
        printf("Please type in the persons phone number\n\n");
        scanf("%s",&ph.fnumb);
        puts("\n");
    
        printf("Name: %s  %s\n",ph.fname, ph.lname);
        printf("Phone#:  %s\n\n",ph.fnumb);
    
        info = fopen("info","w");
    
        if( info == NULL )
    {
        printf("Fatal Error. Could not open file for writing.\n");
        exit( EXIT_FAILURE );
    }
    
     fclose(info);
    
       return 0;
     }
    Just in case this is the error that i get when i add fprintf


    ph.c: In function `main':
    ph.c:25: warning: passing arg 1 of `fprintf' from incompatible pointer type
    ph.c:25: too few arguments to function `fprintf'
    ph.c:26: warning: passing arg 1 of `fprintf' from incompatible pointer type
    ph.c:26: too few arguments to function `fprintf'
    ph.c:30: warning: passing arg 1 of `fprintf' from incompatible pointer type
    ph.c:30: too few arguments to function `fprintf'
    Last edited by xlordt; 10-04-2002 at 01:08 AM.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    The order in which your program is wrong and where is fprintf- you need it.

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Print a File on Multiple Processors
    By Cell in forum Linux Programming
    Replies: 6
    Last Post: 03-25-2009, 09:39 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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