Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,602 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 948 people online right now. Registration is fast and FREE... Join Now!




problem counting number of lines in a text file

 
Reply to this topicStart new topic

problem counting number of lines in a text file

S0n1C
post 14 Jul, 2008 - 11:02 AM
Post #1


New D.I.C Head

*
Joined: 15 Jun, 2007
Posts: 35


My Contributions


Hey, so i'm trying to count to number of lines in a text file, and this is what i have so far.

cpp

while(!feof(fp)) {
fscanf(fp, "%s\n", junk);
numOfElements++;
}


but the value of numOfElements, no matter how many lines i put in the file always is 21. I have no idea why this is happening. This isn't all of my program, but this is the part that isn't working. The main objective of what i'm doing is upon compile time I don't know how many different "Listings" i'm going to have. a Listing is a class that i've built. I'm using the new operator to create the number of "Listings" that i'm going to need. Each line in the file is going to be a new listing. So when I run my program I have 21 LIstings, but with the test data that i'm using I only need 5. This is the first time that i'm using new, but it's doing exactly what I want it to, the line is
cpp

x = new Listing[numOfElements];


but i can't get the number of lines in the file though. I would think that numOfElements increased each time is goes to the next line until the end of the file will give me the number of lines in the file, but it's not.

Could someone please point me in the right direction.

Thanks

S0n1C!
User is offlineProfile CardPM

Go to the top of the page

polymath
post 14 Jul, 2008 - 12:23 PM
Post #2


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Do something like this:
cpp

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
string str;
fin.open("doc.txt")
int count=0;
for(;getline(fin,str)!=eofbit;count++);
cout << "Size of file is " << count << " lines. Press any key then enter to exit."
cin >> str;
return 0;
}


Then you could use a vector instead of a single string along with the resize function to store each line if you wanted to. Or you could just append them all together on one giant string. Your choice.

This post has been edited by polymath: 14 Jul, 2008 - 12:25 PM
User is offlineProfile CardPM

Go to the top of the page

S0n1C
post 14 Jul, 2008 - 07:47 PM
Post #3


New D.I.C Head

*
Joined: 15 Jun, 2007
Posts: 35


My Contributions


I'm currently not using fstream. I'm using the C way to open the file, FILE* fp = fopen(...) So getline and such operators won't work, I will repost after switching everything over.

Thanks;

S0n1C!
User is offlineProfile CardPM

Go to the top of the page

S0n1C
post 14 Jul, 2008 - 08:55 PM
Post #4


New D.I.C Head

*
Joined: 15 Jun, 2007
Posts: 35


My Contributions


ok, so i've switched stuff over, but how do i input formatted text from a text file, my file looks like that
CODE

Szalwinski|Chris|34 Highland  |408-003-3333
Potter    |Harry|975 Kings X  |975-975-9999
Flintstone|Fred |2 Dinosaur Rd|897-999-9008
Simpson   |Marge|3 Springfield|654-678-9065
Adams     |Matt |1 Lucas Lane |444-444-4444


i'm currently using a fscanf statement(below) to grab the different parts. and ignore the |. how can I do that using fstream?

CODE

    fscanf(fp, "%[^|]|%[^|]|%[^|]|%s\n", givenName, surName, livingAddress, phoneNum) != EOF)


This post has been edited by S0n1C: 14 Jul, 2008 - 08:56 PM
User is offlineProfile CardPM

Go to the top of the page

S0n1C
post 14 Jul, 2008 - 11:14 PM
Post #5


New D.I.C Head

*
Joined: 15 Jun, 2007
Posts: 35


My Contributions


SOLUTION!
What I noticed about my for loop:
CODE

for(numOfElements = 0; fscanf(fp, "%s\n", junk) != EOF; numOfElements);

is that the value of numOfElements was the exact number of sections in the file before each space. Since I know that each part has a first name, last name, address, and phone number, what I did was take the value of numOfElements and devide it by 4, and now the numOfElements is the same number as people in the file.

Thanks for the help,

S0n1C!
User is offlineProfile CardPM

Go to the top of the page

polymath
post 15 Jul, 2008 - 06:57 PM
Post #6


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


to write formatted input to a text file with fstream, then read and output it:
cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ofstream fileoutput;
ifstream fileinput;
vector<string> strarray;
string str;
int count(0);
fileoutput.open("file.txt");
fileinput.open("file.txt");
cout << "Outputting data into file:";
fileoutput << "Hello World!\nThis is a test.";
cout << "\nClosing Output stream."
fileoutput.close();
cout << "\nGetting data from file...";
for (;getline(fileinput, str)!=eofbit;count++) {
strarray.push_back(str);
}
cout << "\nSize of file is " << count << " lines. Contents of file:\n";
for(int index=0;true;index++) {
try {
cout << '\t' << strarray.at(index) << endl;
}
catch (exception& e) {
cout << "End of file. Press any key then enter to exit. ";
cin >> str;
fileinput.close();
}
}
return 0;
}


Output should be:
CODE

Outputting data into file:
Closing output stream...
Getting data from file...
Size of file is 2 lines.  Contents of file:
   Hello World!
   This is a test.
End of file.  Press any key then enter to exit. z
[WINDOW_CLOSES]


Hope this helps. I'm kinda new to vectors and exception handling, if i'm wrong somewhere, correct me.

This post has been edited by polymath: 15 Jul, 2008 - 06:57 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month