By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

No of lies in a text file.

P: 13
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:

Expand|Select|Wrap|Line Numbers
  1. filesize=0;
  2. fstream file1;                    
  3. file1.open("graph.txt",ios::in);
  4. {
  5.     while(!file1.eof())
  6.     {
  7.         filesize++;
  8.     }
  9.  
  10. }
  11. 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.
Feb 21 '07 #1
Share this Question
Share on Google+
24 Replies


Expert 100+
P: 1,510
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>
http://www.cppreference.com/cppstring/getline.html
Feb 21 '07 #2

P: 13
Ok, I think I follow.
I'll have to go through it slowly.
Thanks
Feb 21 '07 #3

P: 13
Sorry I am still having no luck, I have tried this:

Expand|Select|Wrap|Line Numbers
  1. filesize=0;
  2. fstream file1;                    
  3. string s;
  4. file1.open("graph.txt",ios::in);
  5. {
  6.     while(!file1.eof())
  7.     {
  8.         getline(cin,s);
  9.         filesize++;
  10.     }
  11.  
  12. }
  13. file1.close();
Is this wrong? I am getting undeclared identifier erros for s,string and getline.
I have #include <string.h>
I'm stumped!
Feb 21 '07 #4

DeMan
100+
P: 1,806
I think String has a capital S (but that could be a lie )
Feb 21 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
I think String has a capital S (but that could be a lie )
Wrong language, DeMan - you're thinking Java!
Feb 21 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Sorry I am still having no luck, I have tried this:

Expand|Select|Wrap|Line Numbers
  1. filesize=0;
  2. fstream file1;                    
  3. string s;
  4. file1.open("graph.txt",ios::in);
  5. {
  6.     while(!file1.eof())
  7.     {
  8.         getline(cin,s);
  9.         filesize++;
  10.     }
  11.  
  12. }
  13. 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++?)
Feb 21 '07 #7

P: 13
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.
Feb 21 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
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)
Feb 21 '07 #9

P: 13
Yeah it compiles ok now but it's still getting stuck in my while loop!
Feb 21 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
meatstu-

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

Expand|Select|Wrap|Line Numbers
  1. filesize=0;
  2. fstream file1;                    
  3. string s;
  4. file1.open("graph.txt",ios::in);
  5. {
  6.     while(!file1.eof())
  7.     {
  8.         getline(cin,s);
  9.         filesize++;
  10.     }
  11.  
  12. }
  13. file1.close();
Feb 21 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
meatstu-

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

Expand|Select|Wrap|Line Numbers
  1. filesize=0;
  2. fstream file1;                    
  3. string s;
  4. file1.open("graph.txt",ios::in);
  5. {
  6.     while(!file1.eof())
  7.     {
  8.         getline(cin,s);
  9.         filesize++;
  10.     }
  11.  
  12. }
  13. file1.close();
It has to do with your getline(). You want to read from the file - but you are getline'ing from "cin".
Feb 21 '07 #12

P: 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.
Feb 21 '07 #13

sicarie
Expert Mod 2.5K+
P: 4,677
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.
Expand|Select|Wrap|Line Numbers
  1. file1.open("graph.txt",ios::in);
  2. {
  3.     while(!file1.eof())
  4.     {
  5.         getline(cin,s);
  6.         filesize++;
  7.     }
  8.  
  9. }
  10.  
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.
Feb 21 '07 #14

P: 13
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.
Feb 21 '07 #15

sicarie
Expert Mod 2.5K+
P: 4,677
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.
No problem - feel free to post again if you get stuck. (And mine does not corrupt, though I have random gibberish input - I don't know the quirks of getline, if there are any...)
Feb 21 '07 #16

sicarie
Expert Mod 2.5K+
P: 4,677
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)

And I was just proven wrong - you can incorporate such libraries into C++ programs as <ctype.h>
Feb 21 '07 #17

Ganon11
Expert 2.5K+
P: 3,652
And I was just proven wrong - you can incorporate such libraries into C++ programs as <ctype.h>
I'm fairly certain that it's compiler dependent. I use Dev C++, which uses #include <iostream>, but I remember working with Borland and using #include <iostream.h>
Feb 22 '07 #18

AdrianH
Expert 100+
P: 1,251
I'm fairly certain that it's compiler dependent. I use Dev C++, which uses #include <iostream>, but I remember working with Borland and using #include <iostream.h>
There are two types of include:
  1. #include "header.h"
  2. #include <header.h>
The first looks for the header in the current directory first, then if not found starts looking for it in the include folders.
The second only looks in the include folders.


Adrian
Feb 22 '07 #19

AdrianH
Expert 100+
P: 1,251
Oh and the includes that don't include a .h are newer C++ includes, to differentiate them from the older ones.


Adrian
Feb 22 '07 #20

Banfa
Expert Mod 5K+
P: 8,916
Also the ones without the .h tend to have a namespace that needs specifying in the source file where as the ones with a .h tend not to.
Feb 22 '07 #21

P: 3
I am of the right idea that you should input something from the file before you do anything. pliz try____ file variable>> {insert declared variable of whatever is in the file.};____,

I guess this will help.
Feb 22 '07 #22

P: 13
I am of the right idea that you should input something from the file before you do anything. pliz try____ file variable>> {insert declared variable of whatever is in the file.};____,

I guess this will help.
Yes I am doing this later in the code, I just copied out what I thought was needed for my post, thanks tho.

With regards to the .h issue, I also found that when I removed it I needed to specify a namespace.
Stu.
Feb 22 '07 #23

AdrianH
Expert 100+
P: 1,251
Yes I am doing this later in the code, I just copied out what I thought was needed for my post, thanks tho.

With regards to the .h issue, I also found that when I removed it I needed to specify a namespace.
Stu.
Do you mean using namespace std;? Well that is because that is the newest standard. Using the header file with .h in it may have some peculiarities as this is an old standard and should only be used on compilers that are really old.

The namespace keyword is to carve up the space where user classes, function and variable names are stored. That way someone can implement a class called iostream in their namespace without having to worry about it clashing with the one in another.

Trust me; use the new header with the using keyword. Otherwise you will eventually find something not working correctly or name clashes occurring, and will not have any recourse to correct the situation cleanly.


Adrian
Feb 23 '07 #24

P: 13
Do you mean using namespace std;? Well that is because that is the newest standard. Using the header file with .h in it may have some peculiarities as this is an old standard and should only be used on compilers that are really old.

The namespace keyword is to carve up the space where user classes, function and variable names are stored. That way someone can implement a class called iostream in their namespace without having to worry about it clashing with the one in another.

Trust me; use the new header with the using keyword. Otherwise you will eventually find something not working correctly or name clashes occurring, and will not have any recourse to correct the situation cleanly.


Adrian
Yes Adrian I do, I guess using the old standard was the root of my problems. I'm only doing simple programmes at the moment which is probably why I have not encountered any problems, I will take your advice, and thank you for the explanation.
Stu
Feb 23 '07 #25

Post your reply

Sign in to post your reply or Sign up for a free account.