Click here to Skip to main content
12,559,073 members (49,004 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
I am working on a project. I want to find how can find the total number of characters in file? (student.txt supposed).

I implemented the logic below:
char* File_storage[iFile_size+1];               //dynamic array declaration
It should store all the data from the file to char* pointer. But the size is not correct. after fetching all data(character by character) the pointer still have garbage values at the end e.g

after compiling, char* File_storage has value $Saad$14$Novemeber$!****** (* shows garbage value.
I want to finish till '!' character.
Posted 26-Nov-12 10:07am
Updated 26-Nov-12 10:10am
Snk Tay 26-Nov-12 16:13pm
Do you want to count white characters(white characters are SPACE, TAB, \n, ...)??
saad_lah 26-Nov-12 16:15pm
i want to count all characters like (alphabets, numbers and '$', '!')
Sergey Alexandrovich Kryukov 26-Nov-12 16:33pm
Where is the definition of keystroke. It it was of a one-byte type, you could do it...
saad_lah 26-Nov-12 16:42pm
char keystroke;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here[^] you find a complete example of doing that in the correct way.
saad_lah 26-Nov-12 16:31pm
Thnx so much, i tested your given code and problem solved
CPallini 26-Nov-12 16:46pm
You are welcome.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


#include <stdio.h>
#include <ctype.h>

char c;
long long int counter;

        myfile >> c;

I tested it!! it works
saad_lah 26-Nov-12 17:04pm
This will definitely run,
What i want was something different than this. I am editing data in file "student.txt" which so that i have to copy all data in a char* pointer and then edit the data in pointer and then by ofstream myfile(*****) again puts edited data in file again, the problem i was facing was , how can i find the size of dynamic char* array so that i can put it in char* file_storage; file_storage=new char[count+1];
by (!myfile.eof()) its was not working correctly, but by myfile.good() its accurately giving the length of all characters in file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161026.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100