Thread: Writing a Program in C Program

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Question Writing a Program in C Program

    Can someone help me, I am learning C Program and I missed class and my teacher is not a very good teacher, (he keeps forgetting what he is talking about during class). Anyway, I am trying to write a program that does the following, that includes fopen(), fclose(), and fprintf(). I can not seem to getting working right. Please help

    1. Create a text file containing the following text. “Now is the time for all good men to come to the aid of their country.”

    2. Write a C program to open this file, read the contents, and print it to the screen. Incorporate a test to print out the message “No such file found” if the file does not exist.

    3. Submit the source file and an executable named “readfile”.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) Take a text editor and do it.

    2) So how far did you get? If your teacher is not good, take your textbook instead, or even an online tutorial. It's pretty basic stuff, and you even know the functions you need.

    3) You know how to compile, right? So compile, then submit the source file and the resulting executable using whatever submission process your school uses.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you could post your best effort to date, just so you don't sound like someone looking for free homework.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    This is what I have

    I am at work now so I can not see if it works.

    #include "stdio.h"

    void createfile( char * fname, char *s)
    {
    FILE *f = fopen( fname, "wt");
    if( f )
    {
    fprintf(f,"%s", s );
    fclose( f );
    }
    }

    void printfile( char *fname )
    {
    int ch;
    FILE *f = fopen( fname, "wt");
    if( f )
    {
    ch = 0;
    while( ch != EOF )
    {
    ch = fgetc(f );
    if( ch != EOF ) printf("%c", ch );
    }
    fclose( f );
    }
    else
    print "Can't open file %s\n", fname );

    }

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    In the printfile function you're opening the file for writing instead of reading. You also forgot using a code-tag which is impairing my vision.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's also a convention to use <> for system headers:

    #include <stdio.h>

    And a few syntax errors here and there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Thanks

    Thank you to all of you, I will try my changes at school

  8. #8
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Code tags please.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was going to reply something along the lines of "Use code tags, or I'll hunt you all down and kill you.", but I see you've already had a psycopath tell you to use them.


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

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Talking Thanks

    Thank you to all of you, I did make the changes and I got it to work

    Big thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM