![]() |
| | #1 |
| Registered User Join Date: Mar 2009
Posts: 15
| I understand the question and have even written the code. The question is. Write A Program to write to a file, read from that file and get the number of lines and the number of words. The number of lines works every time, but the number of words works only for the last line inputted. We have not learnt vectors or using namespace std; yet so they are out of the question. Code: #include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
//Inputting text part
char text[50];
ofstream out;
out.open("c:/country.txt",ios::app);
cout<<"\nEnter text\n";
cin.getline(text,50);
out<<text<<"\n";
out.close();
//Outputting and calc text part
ifstream inp;
const int n=80;
int nol=0,now=0; //nol is number of lines and now is number of words
char line[n],x[n];
inp.open("c:/country.txt");
cout<<"\nDisplay all records\n\n";
while(inp)
{
inp.getline(line,80);
cout<<line<<endl;
nol++;
strcpy(x,line);
for(int i=0;i<80;i++)
{
if(x[i]==' '||x[i]=='\n')
{
now+=1;
}
}
}
inp.close();
cout<<"\nTotal number of lines = "<<nol-1<<"\n";
cout<<"\nTotal number of words = "<<now-1<<endl;
getch();
}
If I dont skip the inputting text part, the program shows me the number of words in the last inputted line only. Please tell me what I am doing wrong. EDIT: Forgot to say thanks in advance Last edited by aditya_t90; 11-11-2009 at 05:50 AM. Reason: Forgot to say thanks |
| aditya_t90 is offline | |
| | #2 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| I'm guessing your input is not working, probably because the file is not being opened. You're not checking if it worked. |
| rags_to_riches is offline | |
| | #3 |
| Registered User Join Date: Mar 2009
Posts: 15
| I did check to see what happens. The file is being opened. When i run it without the input text part(i.e. I comment it), the output is Code: Display all records All the text from the file. Total number of lines = 2 Total number of words = 6 Code: Enter text I am entering text now. Display all records All the text from the file. I am entering text now. Total number of lines = 3 Total number of words = 5 |
| aditya_t90 is offline | |
| | #4 |
| Registered User Join Date: Mar 2009
Posts: 15
| Someone please help me out. I have to submit this tomorow. *BUMP* |
| aditya_t90 is offline | |
| | #5 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Whatz the problem can u paste the latest code ?? |
| RockyMarrone is offline | |
| | #6 |
| Registered User Join Date: Mar 2009
Posts: 15
| The problem is that only the last entered line words are counted. The other words are not counted Code: #include<iostream>
#include<fstream>
#include<conio.h>
void main()
{
char text[50];
ofstream out;
out.open("c:/country.txt",ios::app);
cout<<"\nEnter text\n";
cin.getline(text,50);
out<<text<<"\n";
out.close();
ifstream inp;
const int n=80;
int nol=0,now=0;
char line[n],x[n];
inp.open("c:/country.txt");
cout<<"\nDisplay all records\n\n";
if(inp.good())
{
cout<<"Working";
}
while(inp)
{
inp.getline(line,80);
cout<<line<<endl;
nol++;
strcpy(x,line);
for(int i=0;i<80;i++)
{
if(x[i]==' '||x[i]=='\n')
{
now+=1;
}
}
}
inp.close();
cout<<"\nTotal number of lines = "<<nol-1<<"\n";
cout<<"\nTotal number of words = "<<now-1<<endl;
getch();
}
Code: Hello Hello I check Code: if(inp)
{
cout<<"Working";
}
Code: if(inp.good())
{
cout<<"Working";
}
Please help Thanks |
| aditya_t90 is offline | |
| | #7 |
| Registered User Join Date: Aug 2006 Location: Liverpool UK
Posts: 241
| i am not that familiar with this but i am guessing that 'inp' is an object with 'good' as a function returning a boolean, also the while loop would then need to contain inp.good() to properly test the condition |
| rogster001 is offline | |
| | #8 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| I was not clear when I responded. What I meant was that the input you were trying to add to the file was not working because the file was not being opened for writing. As in...because it's in the root directory (or for other reasons) the file is marked as read-only. |
| rags_to_riches is offline | |
| | #9 |
| Registered User Join Date: Mar 2009
Posts: 15
| Writing to the file works perfectly. ![]() the file is not read only. I checked and double checked` Its only reading the total number of words in the whole file that does not work |
| aditya_t90 is offline | |
| | #10 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| Seems to work OK for me, in that it is not exhibiting the behaviour you've described. Compiling using g++ on BSD. |
| rags_to_riches is offline | |
| | #11 |
| Registered User Join Date: Mar 2009
Posts: 15
| Maybe Borland c++ 5.02 on vista must be the issue. @rags_to_riches Are you sure that it counts the number of lines and the number of words? If thats the case then can someone suggest a good enough compiler/ide that will work on vista for c++. Dont suggest visual studio c++ express 'coz I have had major problems with it. Thanks Everyone |
| aditya_t90 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| opening empty file causes access violation | trevordunstan | C Programming | 10 | 10-21-2008 11:19 PM |
| Formatting the contents of a text file | dagorsul | C++ Programming | 2 | 04-29-2008 12:36 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |
| Possible circular definition with singleton objects | techrolla | C++ Programming | 3 | 12-26-2004 10:46 AM |
| System | drdroid | C++ Programming | 3 | 06-28-2002 10:12 PM |