Text file excersize.need help.
Jun 8, 2013 at 6:31am UTC
I have this code,i need to count how much there are letters in each word in a text file and cout thouse who consist of only 2 letters,can you help me with it?
i kwno i can use char and then i will count how much letters there are. but i think in such situanion string type is better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
struct rd
{
string ka;
};
rd gg[22];
int a=0;
ifstream fd("duom.txt" );
while (fd.good())
{
a++;
fd>>gg[a].ka;
}
fd.close();
for (int i=0;i<a;i++)
{
cout<<gg[i].ka<<" " ;
}
cout<<endl;
system("PAUSE" );
return 0;
}
Jun 8, 2013 at 9:54am UTC
my solution using chars, not strings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char * buffer=new char [1048576];
int filesize=0;
ifstream file(your filename);
while ((buffer[filesize]=file.get())){filesize++;}
int wordlenght=0;
char word[64];
for (int i=0;i<filesize;i++)
{
if (buffer[i]!=' ' ){word[wordlenght]=buffer[i];wordlenght++;}
if (buffer[i]==' ' )
{
if (wordlenght==2){cout<<word<<endl;}
wordlenght=0;
}
}
}
something like that?
i havent tried this code jet, im sure it will not compile(ifstream file(your filename))
Jun 8, 2013 at 11:03am UTC
Yes it doesnt compile:) any othet answers maybe? :)
Topic archived. No new replies allowed.