Write/Read Char Count to/from File

  • Thread starter Thread starter k_phanichandra
  • Start date Start date
  • Tags Tags
    Count File
AI Thread Summary
The discussion focuses on creating a program to write a specified number of characters to a file and then read them back. The initial code snippet includes opening a file for writing with `fopen`, using `fprintf` to write the character 'a' ten times, and then closing the file. For reading, the code suggests using `fopen` again with the "r" mode but lacks complete implementation details. Key functions highlighted for file operations include `fopen`, `fclose`, `fgetc`, and `printf`. To count characters in the file, a loop with `fgetc` can be employed until EOF is reached, incrementing a counter. The discussion also references a tutorial for further guidance on file I/O in C programming, emphasizing the importance of understanding the correct usage of these standard library functions.
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top