Write/Read Char Count to/from File

  • Thread starter Thread starter k_phanichandra
  • Start date Start date
  • Tags Tags
    Count File
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
k_phanichandra
Messages
3
Reaction score
0
Total program to write number of characters to a file and to read those back.
Please help.
 
Physics news on Phys.org
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
 
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