Hello. I would like If possible get some input and assisstance on operations with external files.

So this is what i have so far:

#include <iostream>
#include <fstream>
using namespace std;

void main (){
       cout<<" Input into the file '.' " <<endl;
       char zbor[20];
       ofstream dat;
       dat.open("tekst.txt");
       do{
              cin.getline(zbor,20);
              if(strcmp(zbor,".")){
                     dat<<zbor<<endl;
              }
       }
       while(strcmp(zbor,"."));
       dat.close()
}

Tnis so far opens a .txt file named tekst. And asks the user to input strings maximum of 20 characters. The loop goes on until the user iputs '.' . What i need now is 2 things:
1. Now I open the file with say ifstream dat1. I read the sentences the user inputs right? How do I check the number of times a letter from the alphabet apears in the file?? And then I need to print out letter:numberoftimesappeard in the whole file tekst.
2. Second Thing i need is to change a word in the file with another word. That is replace one word with another.... In this case the user wont get to input whole sentences wth cin.getline only input words with only cin....
Im quite familiar with opening to write to and read from files . I am at universyti and Im preparing an exam for friday witch is sure to have I/O from files.. And I kinda missed the class where they lectured about this... Can i get some help with the functions?? Like put and get... Any ideas?? Can anyone post an example.. I would be really gratefull..... Thanks

Recommended Answers

All 2 Replies

int brojac;
ifstream dat1;
dat1.open("tekst.txt");
int dolz;
char zbor1[20];
dat1.getline(zbor1.20);
do{
       dolz=strlnen(zbor1);
       for(brojac=0;brojac<dolz;brojac++){
              if(zbor1[brojac]>='a' && zbor1[brojac]<='z');

Ok this is a second part I m working on atm. It opens the file...reads a line. checks the lenght of te line and the if statement is checking if a character in the line is a letter ... and now i dont know what to do.... to check which letter and where to remember the times the letter appears...

Im from macedonia so the program is written in macedonian . ...

Ok this is a second part I m working on atm. It opens the file...reads a line. checks the lenght of te line and the if statement is checking if a character in the line is a letter ... and now i dont know what to do.... to check which letter and where to remember the times the letter appears...

Im from macedonia so the program is written in macedonian . ...

if you want to count the number of times each letter appears in the file then you will have to have an int array with 255 elements. use the letter itself as an index into the array. for example:

int letters[255] = {0};


for(brojac=0;brojac<dolz;brojac++){
   if( isalpha(zbor1[i]) )
         letters[zbor1[i]]++;
}

now when the loop above finishes you will have the count. Take a look at any ascii chart (see google for them) and you will see that the letter 'a' has an ascii value of 97, so zbor1[97] is the numbe of times that the letter 'a' appears in the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.