Write/Read Char Count to/from File

  • Thread starter Thread starter k_phanichandra
  • Start date Start date
  • Tags Tags
    Count File
Click For Summary

Discussion Overview

The discussion revolves around writing a program to count the number of characters in a file and then reading that count back. The focus includes programming techniques, file handling in C, and the use of standard library functions.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests assistance with a complete program to write and read character counts from a file.
  • Another participant provides pseudocode, suggesting the use of fopen() for file operations but expresses uncertainty about the correct libraries and syntax.
  • A different participant clarifies that fopen() requires a string argument for the mode ("w" for write, "r" for read) and recommends using fputc()/fgetc() or fwrite()/fread() for character operations.
  • Another contribution outlines a simple method for counting characters using fgetc() in a loop until EOF is reached, listing necessary functions like fopen, fclose, and printf.

Areas of Agreement / Disagreement

Participants present various approaches and suggestions, but there is no consensus on a single correct method or complete solution. Uncertainty remains regarding the correct implementation details.

Contextual Notes

Some participants mention missing assumptions about file handling and syntax, as well as potential inefficiencies in the proposed methods.

k_phanichandra
Messages
3
Reaction score
0
Total program to write number of characters to a file and to read those back.
Please help.
 
Technology 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
 
fopen() opens a file, the second argument 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
 
Last edited:
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
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
10
Views
2K
Replies
35
Views
8K
  • · Replies 57 ·
2
Replies
57
Views
6K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K