9 Replies - 37728 Views - Last Post: 07 October 2007 - 10:49 PM Rate Topic: -----

#1 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Counting lines in a file

Post icon  Posted 03 October 2007 - 07:50 AM

I'm writing a program that needs to count how many lines are in a file. What operator could I use to do this?
Is This A Good Question/Topic? 0
  • +

Replies To: Counting lines in a file

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: Counting lines in a file

Posted 03 October 2007 - 08:00 AM

  #include <stdio.h>

  int main(void) {
	  int c=0;
	  char ch='\0';

	  fp=fopen();

	  while(ch!=EOF) {
		ch=fgetc(fp);
		if(ch=='\n')  c++;
	  }
	  return 0;
	}



Something similar to this, is how I would do it.

Granted, if you are on a linux os,
cat $file | wc -l


This post has been edited by no2pencil: 03 October 2007 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#3 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Counting lines in a file

Posted 03 October 2007 - 08:04 AM

I was actually thinking of something that could count the lines and then store it in a variable. I guess I could use what you're saying and just modifying a little, unless someone has something better?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: Counting lines in a file

Posted 03 October 2007 - 08:08 AM

View PostCaptain M, on 3 Oct, 2007 - 08:04 AM, said:

I was actually thinking of something that could count the lines and then store it in a variable. I guess I could use what you're saying and just modifying a little, unless someone has something better?

if(ch=='\n')  c++;
printf("%d",c);  // <-- variable?


Was This Post Helpful? 0
  • +
  • -

#5 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Counting lines in a file

Posted 03 October 2007 - 08:19 AM

Sorry, I'm still new to this. I'm used to using cout, not printf. Could you convert it so it could be used with using namespace std?

I'm actually trying to write a hangman game. The program will have a file that has a hundreds of words in it, and it will grab one word and use it for the game. I'm just trying to have a variable with the file size in it to use it with srand. Here is my code so far:
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
using namespace std;

int main (int argc, char * const argv[]) {
	ifstream input;
	input.open ("input.txt");
	int yelp = 700;
	string line;
	srand ( time(NULL) );
	cout << rand() % yelp;
	getline (input,line);
	cout << line;
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Counting lines in a file

Posted 03 October 2007 - 08:32 AM

Oops. I forgot to mention that I'm using c++.
Was This Post Helpful? 0
  • +
  • -

#7 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Counting lines in a file

Posted 05 October 2007 - 07:34 AM

I figured out how to count the lines. Here it is:
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
using namespace std;

int main (int argc, char * const argv[]) {
	ifstream input;
	input.open ("input.txt");
	int c=0;
	string line;
	
	while(!input.eof()) {
		getline (input, line);
		c++;
		}
	
	srand ( time(NULL) );
	cout << rand() % c;
	
	
	
	return 0;
}



How could I use getline to get a specific line in the file?
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Counting lines in a file

Posted 05 October 2007 - 07:38 AM

You would have to iterate to that line, then use the getline function.
Was This Post Helpful? 0
  • +
  • -

#9 Captain M   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 103
  • Joined: 21-January 07

Re: Counting lines in a file

Posted 05 October 2007 - 08:00 AM

Thanks Amadeus. I'm now trying to have a string with one '_' for every character there is in the word being used for the game. I'm trying to make a loop that does just that. How could I make it so it will add a '_' to the string without erasing the previous contents of the string? Here is what I have as of now:

#include <iostream>
#include <fstream>
#include <time.h>
#include <string>
using namespace std;

int main (int argc, char * const argv[]) {
	ifstream input;
	input.open ("input.txt");
	int c=0, d=0, l=0, p=0;
	string line, line2, mystery;
	
	while(!input.eof()) {
		getline (input, line);
		c++;
		}
	
	srand ( time(NULL) );
	int r = rand() % c;
	
	input.close();
	input.open ("input.txt");
	
	while(d <= r){
		getline (input, line2);
		d++;
		}
	
	cout << line2;
	
	l = line2.length();
	
	while(p != l){
		line2[p];
		p++
		}
		
	while(p >= 0){
		
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 jjhaag   User is offline

  • me editor am smartastic
  • member icon

Reputation: 48
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Counting lines in a file

Posted 07 October 2007 - 10:49 PM

not sure if i understand your post correctly, correct me if this is wrong. you have a string that stores a word, and you want another string that contains a single "_" for every character in that word?

if this is the case, there are a couple of options. let's call the original string source, and the string you want to have the underscores in destination. if destination already exists, the following will do the trick:
testString.assign(source.size(),'_');

if it doesn't exist already and you want to create it and intialize it to contain the underscores, try:
string testString(source.size(),'_');


hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1