Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
C++
while(!student.eof())
	{
		student.get(keystroke);
			iFile_size++;
	}
char* File_storage[iFile_size+1];               //dynamic array declaration
while(!myfile.eof())
    {
        myfile.get(keystroke);
            File_storage[file_count]=keystroke;
            file_count++;
    }

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
Updated 26-Nov-12 10:10am
v2
Comments
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...
--SA
saad_lah 26-Nov-12 16:42pm    
char keystroke;

Here[^] you find a complete example of doing that in the correct way.
 
Share this answer
 
Comments
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.
;)
C++
#include <stdio.h>
#include <ctype.h>
//......

char c;
long long int counter;

//........

while(!myfile.eof())
    {
        myfile >> c;
        if(!isspace(c))
        {
            counter++;
        }

    }

</ctype.h></stdio.h>


I tested it!! it works
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900