Can someone help me, I am learning C Program on my own and 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”.
The project has to be in C Programming not C++. The web site you suggest is for C++
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”.
The project has to be in C Programming not C++. The web site you suggest is for C++
Too Easy! So I will write a different program
#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 );
}
main()
{
createfile( "xfile.txt", “Now is the time for all good men to come to the aid of their country.” );
printfile( "xfile.txt" );
}
#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 );
}
main()
{
createfile( "xfile.txt", “Now is the time for all good men to come to the aid of their country.” );
printfile( "xfile.txt" );
}
Try this website. It's got a lot of useful information about file streams and IO.
Too Easy! So I will write a different program
#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 );
}
main()
{
createfile( "xfile.txt", “Now is the time for all good men to come to the aid of their country.” );
printfile( "xfile.txt" );
}
#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 );
}
main()
{
createfile( "xfile.txt", “Now is the time for all good men to come to the aid of their country.” );
printfile( "xfile.txt" );
}
these r the basics..not a big deal..read low level books like introductory ones..which r for biggners...u will find it very easy..