C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2009, 05:49 AM   #1
Registered User
 
Join Date: Mar 2009
Posts: 15
Post Need Help with reading file

OK before anyone says that this is homework, let me say that this is homework.
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 skip the whole inputting text part, the program shows me the number of words in the file correctly.

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   Reply With Quote
Old 11-11-2009, 08:51 AM   #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   Reply With Quote
Old 11-11-2009, 07:05 PM   #3
Registered User
 
Join Date: Mar 2009
Posts: 15
Unhappy

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
If the input text part is present, the output is.

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   Reply With Quote
Old 11-12-2009, 06:15 AM   #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   Reply With Quote
Old 11-12-2009, 06:21 AM   #5
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Whatz the problem can u paste the latest code ??
RockyMarrone is offline   Reply With Quote
Old 11-12-2009, 06:34 AM   #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();
}
If at runtime i enter the words

Code:
Hello Hello
the word counter outputs only 2 and not all the words from the file, even though the file contains many more words.
I check
Code:
if(inp)
   {
   	cout<<"Working";
   }
But "working" is never output. However when I try

Code:
if(inp.good())
   {
   	cout<<"Working";
   }
The word "working" is displayed on the screen.
Please help
Thanks
aditya_t90 is offline   Reply With Quote
Old 11-12-2009, 07:04 AM   #7
Registered User
 
rogster001's Avatar
 
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   Reply With Quote
Old 11-12-2009, 08:05 AM   #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   Reply With Quote
Old 11-12-2009, 08:14 AM   #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   Reply With Quote
Old 11-12-2009, 09:22 AM   #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   Reply With Quote
Old 11-12-2009, 09:40 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22