No of lies in a text file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meatstu
    New Member
    • Feb 2007
    • 13

    No of lies in a text file.

    Hello everyone I was hoping someone could point me in the right direction with a problem I have reading from a text file.
    I am trying to find out how many lines of data are in a text file,the file will contain a list of names in this sort of format:
    john
    dave
    bob
    etc.
    I have written the following code so far:

    Code:
    filesize=0;
    fstream file1;					
    file1.open("graph.txt",ios::in);
    {
    	while(!file1.eof())
    	{
    		filesize++;
    	}
    		
    }
    file1.close();
    The program runs and gets stuck in the while loop, Is this due to the fact nothing is being done with the data in the file whilst in the loop?
    I dont want to read the data (names) in from file untill I know how many lines are in there.
    Anybody got any tips?
    cheers
    Stu.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    it gets stuck in the while loop because you never read from the file so it never reaches EOF. You need to read the text, e.g. use getline() in <string>
    Last edited by AdrianH; Feb 22 '07, 03:40 AM. Reason: Added url tag

    Comment

    • meatstu
      New Member
      • Feb 2007
      • 13

      #3
      Ok, I think I follow.
      I'll have to go through it slowly.
      Thanks

      Comment

      • meatstu
        New Member
        • Feb 2007
        • 13

        #4
        Sorry I am still having no luck, I have tried this:

        Code:
        filesize=0;
        fstream file1;					
        string s;
        file1.open("graph.txt",ios::in);
        {
        	while(!file1.eof())
        	{
        		getline(cin,s);
        		filesize++;
        	}
        		
        }
        file1.close();
        Is this wrong? I am getting undeclared identifier erros for s,string and getline.
        I have #include <string.h>
        I'm stumped!

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          I think String has a capital S (but that could be a lie )

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by DeMan
            I think String has a capital S (but that could be a lie )
            Wrong language, DeMan - you're thinking Java!

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by meatstu
              Sorry I am still having no luck, I have tried this:

              Code:
              filesize=0;
              fstream file1;					
              string s;
              file1.open("graph.txt",ios::in);
              {
              	while(!file1.eof())
              	{
              		getline(cin,s);
              		filesize++;
              	}
              		
              }
              file1.close();
              Is this wrong? I am getting undeclared identifier erros for s,string and getline.
              I have #include <string.h>
              I'm stumped!
              try removing the .h (is this in C or C++?)

              Comment

              • meatstu
                New Member
                • Feb 2007
                • 13

                #8
                It's C++, It's in a method within a class.
                I am using MS Visual C++ 6.0.
                This is all pretty new to me.

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by meatstu
                  It's C++, It's in a method within a class.
                  I am using MS Visual C++ 6.0.
                  This is all pretty new to me.
                  Yeah, get rid of the '.h'. C libs have those - C++ no longer uses them. (though some use #include "library.h" - generally if it's in quotes (to my experience), it will be a .h)

                  Comment

                  • meatstu
                    New Member
                    • Feb 2007
                    • 13

                    #10
                    Yeah it compiles ok now but it's still getting stuck in my while loop!

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      meatstu-

                      Just out of curiousity, why do you have the '{'s under the file1.open(.... line?

                      Code:
                      filesize=0;
                      fstream file1;					
                      string s;
                      file1.open("graph.txt",ios::in);
                      {
                      	while(!file1.eof())
                      	{
                      		getline(cin,s);
                      		filesize++;
                      	}
                      		
                      }
                      file1.close();

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by sicarie
                        meatstu-

                        Just out of curiousity, why do you have the '{'s under the file1.open(.... line?

                        Code:
                        filesize=0;
                        fstream file1;					
                        string s;
                        file1.open("graph.txt",ios::in);
                        {
                        	while(!file1.eof())
                        	{
                        		getline(cin,s);
                        		filesize++;
                        	}
                        		
                        }
                        file1.close();
                        It has to do with your getline(). You want to read from the file - but you are getline'ing from "cin".

                        Comment

                        • meatstu
                          New Member
                          • Feb 2007
                          • 13

                          #13
                          I dont quite follow, if you mean why do I put my '{' under the line rather than than at the end of the file1.open line, this was the style I was first shown when I first started learning programming.

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by meatstu
                            I dont quite follow, if you mean why do I put my '{' under the line rather than than at the end of the file1.open line, this was the style I was first shown when I first started learning programming.
                            Code:
                            file1.open("graph.txt",ios::in);
                            {
                            	while(!file1.eof())
                            	{
                            		getline(cin,s);
                            		filesize++;
                            	}
                            		
                            }
                            The brackets - not the ones for the while loop, but under the file1.open line. That's not a loop, it's just a statement - there's no need for those.

                            Comment

                            • meatstu
                              New Member
                              • Feb 2007
                              • 13

                              #15
                              Ah right thats the way I've done file read in the past, I was told to put the brackets there.
                              It's not getting stuck in the loop now, I've changed 'cin' to 'file1'. However it now seems to be corrupting the data in the file.
                              I will try and work throught his myself, but many thanks for your help, it's much appreciated.
                              Stu.

                              Comment

                              Working...