Write/Read Char Count to/from File

  • Thread starter k_phanichandra
  • Start date
  • Tags
    Count File
In summary, to write a program that counts the number of characters in a file and then reads them back, you will need to use the functions fopen, fclose, printf, and fgetc. You can open the file for either writing or reading using the appropriate argument ("w" or "r"). Then, you can use fgetc to read individual characters and count them using a counter variable. Finally, you can print the number of characters using printf.
  • #1
k_phanichandra
3
0
Total program to write number of characters to a file and to read those back.
Please help.
 
Technology news on Phys.org
  • #2
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
 
  • #3
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:
  • #4
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
 

Related to Write/Read Char Count to/from File

1. How do I write a character count to a file?

To write a character count to a file, you will need to use a programming language such as Java, C++, or Python. First, open the file for writing using the appropriate function. Then, use a loop to iterate through each character in the file and count them. Finally, write the character count to the file using a write function.

2. How can I read a character count from a file?

Reading a character count from a file is similar to writing one. First, open the file for reading. Then, use a loop to iterate through each character and count them. Finally, store the character count in a variable and use it as needed in your program.

3. Can I write a character count to a specific location in the file?

Yes, you can write a character count to a specific location in the file. This can be done by using a seek function to move the file pointer to the desired location before writing the character count. Just make sure to open the file in the correct mode to allow for writing at specific locations.

4. Do I need to close the file after writing a character count?

Yes, it is important to close the file after writing a character count. This ensures that all data is properly written to the file and that any resources used for writing are released. Failure to close the file can result in data loss or corruption.

5. Can I read a character count from a file that has already been closed?

No, once a file has been closed, it cannot be read from. This is because the file pointer has been moved to the end of the file and there is no way to reset it. If you need to read a character count from a file, make sure to keep the file open until you have finished reading from it.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
851
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
726
  • Programming and Computer Science
Replies
1
Views
363
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
9
Views
894
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top