Need Help with reading file

This is a discussion on Need Help with reading file within the C++ Programming forums, part of the General Programming Boards category; OK before anyone says that this is homework, let me say that this is homework. I understand the question and ...

  1. #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 04:50 AM. Reason: Forgot to say thanks

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    I'm guessing your input is not working, probably because the file is not being opened. You're not checking if it worked.

  3. #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

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Someone please help me out.
    I have to submit this tomorow.

    *BUMP*

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Whatz the problem can u paste the latest code ??

  6. #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

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    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

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    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.

  9. #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

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Seems to work OK for me, in that it is not exhibiting the behaviour you've described. Compiling using g++ on BSD.

  11. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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