View Full Version : C program help
k_phanichandra
Dec13-07, 10:10 PM
Total program to write number of characters to a file and to read those back.
Please help.
topherfox
Dec13-07, 11:36 PM
this is sudo code, not completely sure but most of it should work, provided you include the right libraries
#include <file.h> ??? maybe
file f = fopen ("text.txt", w); //opens for write
for(int i = 1; i <=10; ++i;)
fprintf(f,"a"); //prints a 10 times
f.close;
file f = fopen("text.txt", r); //opens for read
//forget
Should get you started
mgb_phys
Dec14-07, 09:14 AM
fopen() opens a file, the second arguement should be a string, either "w" or "r" for write/read
reading/writing individual characters you should look at fputc()/fgetc() or better fwrite()/fread().
Take a look here http://www.cprogramming.com/tutorial/cfileio.html
jim mcnamara
Dec14-07, 09:20 AM
simplest but not the most efficient way:
1. open the file for read only - explained above, get a file stream to read.
2 set an integer variable as a counter = 0.
3. while (fgetc(<file stream variable>)!=EOF) counter++;
4. close the file
5 display the number of characters (use the counter varaible) with printf
The standard library functions you need are: fopen, fclose, printf, fgetc
So you can read your documentation on how to call each of those
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.