C :: Counting Number Of Lines In A File

Feb 2, 2015

it looks like a popular method for determining the total lines in a file is to read the entire file character by character in search of ' '. I have a file with 5 lines, but for some reason this code isn't finding any instances of ' '. Is this possible? Is there a better way to get the number of lines in a file?

The file looks like this:

Code:
NAME: John
FRIEND 1: Steve
FRIEND 2: Andrea
FRIEND 3: Ken
OCCUPATION: Programmer

Code:
FILE *f = fopen(currentFile, "r");
if (f == NULL) perror ("Error opening file.
");
int ch, lines = 0;
while(!feof(f)){

[Code] .....

View 2 Replies


ADVERTISEMENT

C++ :: Counting The Number Of Lines In A File?

Jun 3, 2013

I was trying to make a function that would return how many lines are in a certain file. I was looking at [URL] which talks about how to count how many characters are in a text file. I just want the number of lines with in a file.

View 2 Replies View Related

C++ :: Counting Number Of Lines Inputted By User (File Stream)

Feb 12, 2014

So I'm trying to count the number of lines in a text file that is inputted by the user. Also the code doesn't sum up the last number of the text file (exmp it calculates and print only 14 from 15 numbers). I'm a beginner in c++ programing.

View 3 Replies View Related

C++ :: Declaring Arrays - Counting Number Of Lines In Text File

Feb 9, 2013

I'm creating a program to read information about class schedules at my school, reformat the information, and allow the user to search for specific semesters. There are eight fields of information. I'm reading the info from a text file using eight parallel arrays, but I'm having trouble declaring the arrays. I can run this code in one compiler (Dev-C++) with no problems, but I get errors when trying to compile it using Visual Studio stating that arrays must be declared with a constant value. I have a loop to run through the text file, with a counter to increment with each subsequent line, then I create a constant int equal to the counter, and declare the arrays of size equal to the constant int. Here's the section of code in question:

// Counting the number of lines in the text file
inFileForLines.open("CIS225HW1DA.txt");
string countLine;
int numberOfLines = 0;
//Discarding the first line of the text file containing only column headings
getline(inFileForLines, countLine);

[Code] .....

View 7 Replies View Related

C++ :: Counting Lines From A File

Apr 23, 2013

How I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work.

void SpellCheck::checkValidWords(const string& inFileName) {
string eachword;
ifstream istream;
istream.open( inFileName.c_str() );
if ( !istream.is_open() ) {

[Code] .....

View 3 Replies View Related

C :: Working With File And Counting Characters / Words And Lines

Dec 17, 2013

I have to write a program (on linux) which will count character, words and lines like wc linux command. I'm trying to write this for last 3 days... First part of app I did and it works fine - command line options to choose. Then I've got a function read_file which I have to use to read a file. One of the options is to get the file name from user and if user will not type any name then the standard file is ubuntu dict file /usr/share/dict/words, this is not working as well...

Counting characters and lines is working fine but because I don't know how to get text from read_file wrote code to read file interior this functions. Words counting is working partly - everything is fine until there are two or more spaces, tabs one after another then counts extra words. Finally I need child processes in words and lines counting functions. Parent process should waits for all childs to finish and should be pipes to submit character counts back to parent process. How to do all this things with processes...

Code:

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
/*size of character buffer to read in a file. */
#define BUFFSIZE 1000000

[code]....

View 2 Replies View Related

C++ :: Counting The Number Of Characters In TXT File

Feb 22, 2015

I have recently hit a stump with C++ and have been getting pretty frustrated with this assignment. I just can't seem to find out how to start the assignment. Basically for the first part of the assignment, I need to find the number of characters in this .txt file my teacher provided for me. The only exception is that I can only count the actual letters themselves, not spaces or punctuation. How would I go about doing this? I have read about functions like isalpha but can't figure out on how to fit them into code to do the first part of this assignment.

View 2 Replies View Related

C++ :: Counting Characters / Words / Lines And Digits

Mar 20, 2013

I am writing a program for my class in C++. For this program we are required to use different fuctions and prototypes outside of main. We have to determine the number of characters, lines, sentences, digits, words, etc. in a particular function the user types in.

View 1 Replies View Related

C :: Counting Number Of Characters Copied To File

Jul 21, 2014

I'm trying to copy a file into another file and copy the number of characters copied but my while loop doesn't even enter into a loop indicating the file is already at the EOF file character. I've confirmed this by placing the printf() statement inside the while loop, which doesn't print anything and by keeping it out of the while loop and changing the chars_copied to something like 9, it prints 9 for number of chars_copied. I don't understand why the file is already at the EOF character, I've tried this with a few more files, it's the same result.

Code:
#include <stdio.h>
int main(void){
FILE *input_file, *output_file;
int c, chars_copied=0;

if((input_file=fopen("C:workmarks.txt", "r"))==NULL)
perror("input file open failed");

[Code] ....

View 13 Replies View Related

C++ :: Counting Number Of Occurrences Of A Word In Text File

Jul 27, 2014

I've been struggling with this for a while, I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

int WordCount :: countWords(string wrd) {
int counter=0;
string temp = "";
while (getline(*file,temp)) {
for (int i = 0; i < temp.length();i++) {

[Code] ....

This is what I have come up with, but I get an incorrect value. It was suggested to us to consider using the strcpy() or strstr() functions, but I don't know how to use them.

View 6 Replies View Related

C++ :: Counting Number Of Occurrences Of A Word In A Text File

Jul 27, 2014

I have to write a function that accepts a word (in my case the word is "the") and file pointer, and counts the number of times the given word appears (case insensitive) within the file associated with the pointer. This means the two words "the" and "THE" should both be counted.

Here is my code:

int WordCount :: countWords(string wrd)
{
string temp;
int counter;
while (!file->eof())
{
*file >> temp;
if (temp == wrd)

[Code]...

Unfortunately this does not work,

View 1 Replies View Related

C :: Counting Function - Prints All Input Lines That Are Longer Than 80 Characters

Jan 10, 2014

I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.

Here is my full code:

/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */

#include<stdio.h>
/* Declarations*/
#define MAX_STRING_LEN 1000
int count_characters(char S1[]);
int main() {

[Code] .....

So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:

Code:
#include <stdio.h>
/* counts character of a string*/
main() {
int nc = 0;
int c;
for (nc = 0; (c = getchar()) != '
'; ++nc);
printf("Number of characters = %d
", nc);
}

which works...

View 1 Replies View Related

C++ :: Reading Number In Lines In Text File

Feb 27, 2015

lets say this is our textfile

-----------------------------------
45(here is space)string and other shit is here
454(here is space)string and other shit is here
4121(here is space)string and other shit is here
77(here is space)string and other shit is here
45545(here is space)string and other shit is here
1122(here is space)string and other shit is here
-----------------------------------

how do i get exactly that number in every line start? and compare but i jus tneed to get them to variable so ?

View 3 Replies View Related

C++ :: Print Out Number Of Words And Lines In A File

Jan 29, 2012

Write a program that prompts the user for the name of a file. Then it opens the file, and counts the number of words and lines in the file, and prints out those counts.

I think I possibly could somehow use a counter to increment using getLIne() until getLine() returns NULL, but the problem is, I'm worried if I try that, a file that looks like this:

Bla bla bla bla bla lkfdljkfaklafdskjladsjkdfkjlkdfjdfshafdsjkjrerjkkjfaddjkfsafkjdjakdfsjkasfjkjkfdskjldfjkfjkdjfkdsakdjfkjfdkjdfskjfdsk
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjdfkerea
blkjadkjlfdskjldfkjlfdkjfdjkdfsjkldfskljfksfdljfd

Only register four lines and not get the one after the fourth line.

So I was thinking of something like

Code:
#include <iostream>
#include<ifstream>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
using namespace std;
iostream keyboard;
ifstream file;
iostream cin;

[Code] .....

Did I do that right? Will that count the number of words and number of lines correctly?

View 14 Replies View Related

C :: Program Count Number Of Lines Of Read Text File

Jan 25, 2013

I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). The pattern of the source text file is like this:

known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089

Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:

0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

View 2 Replies View Related

C :: Program To Count Number Of Lines In Text File And Reverse Contents

Jan 25, 2015

C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.

ex :
input
one
two
three

output:
three
two
one

View 6 Replies View Related

C++ ::  Number Counting Up To Infinite

Mar 1, 2014

#include <windows.h>
int main() {
int i;
int y=6;
int x=9;
gotoxy(x,y);
//gotoxy(x,y) must be the coordinate that the number lies in.

[Code] ...

How to make this program that the output is a number that counts up to infinite (or we say just like a timer but there's no minute just all whole number counting up ) using for loop ? and when the number changes it also change in color !
and the color of a number is according to the color attribute of console output.

View 2 Replies View Related

C++ :: Counting The Number Of Vowels

Feb 10, 2015

I am unsure how to write a function which modifies the content of the 1D character array and counts the number of the vowels. the following is the array that i have.

char text[MAX+1] = {'T', 'e', 's', 't', 'e', 'r', EOT};

the output that i am trying to produce is should look something like this

the number of vowels is 2, e, e.

I am unsure how to do this.

View 3 Replies View Related

C++ :: Counting The Repeated Number?

Jul 12, 2013

This function check a set of values from a text file and show it on the output.

void MatchNumber(int b){
vector<Rect> rects;
ifstream theFile("CheckNumber.txt");

[Code]......

I want to calculate how many times the number is repeated. So I have used freq[num] in that function. But I am getting the output like this-

The common number is = 5
The 5 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 4
The 4 repeated = 1 times

The common number is = 5
The 5 repeated = 1 times

The common number is = 5
The 5 repeated = 1 times

The common number is = 8
The 8 repeated = 1 times

The common number is = 9
The 9 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 6
The 6 repeated = 1 times

The common number is = 8
The 8 repeated = 1 times

So the value of freq[num] only shows 1 instead of calculating the number of repetition.

View 3 Replies View Related

C++ :: Counting Number Of Each Type Of Variable

Apr 23, 2013

I'm a basic C++ programmer, and I am stuck on this problem. You work for a company that collects a set of numbers. The numbers are located in a data file named "./Data_File". The data file contains two columns. how do you count a certain number on the left column.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define DATA_FILE1 "./Data_File"
#define SUCEESS 0
#define FAIL 1

[code].....

View 1 Replies View Related

C++ :: Counting Number Of Vowels In A Sentence?

Nov 29, 2014

I want to count all the vowels in a string (a, e, i , o, u) and display it as a text-based histogram for example:

[INPUT] The black cat sat up on the orange mat!

[OUTPUT]
A: *****
E: ***
I:
O: **
U: *

The asterisks are supposed to correspond to the number of vowels that are counted (using increments and the function setfill()).

#include <iostream>
#include <string>
#include <iomanip>

[Code]....

This is my output during compilation:

[OUTPUT]
The black cat sat up on the orange mat! 0 0 0 0 0
A:
E:
I:
O:
U:

View 1 Replies View Related

C/C++ :: Counting Number Of Open Lockers?

Nov 3, 2014

I'm trying to write a program and I'm stuck. In order to continue on I simply need to know if there is a way of counting how many times the remainder of some number n when divided by some number x is 0.

For example

int lockers, i;
cin>>lockers;
//how can I count the amount of occurrences where lockers%i==0 while i<=lockers

A bit hard for me to articulate so let me know if it's unclear.

View 9 Replies View Related

C/C++ :: Counting Number Of Distinct Values

Mar 5, 2014

I am trying to write a function that will count the number of distinct values in an array of N size and return that number. For example array a[n] = {1, 2, 3, 1, 2, 3, 1}. My code won't look exactly like this because I am using an array that can be modified by user input. it is part of a bigger program that will also has a function for finding the Equilibrium Index for that same array. This is what my Code looks like:

void findDistinctValues() {
int t = 0;
int j = 0;
int disValue = 0;
for( i = 0; i < arraySize; i++) {

[Code] .....

This function works but the problem is it returns 16, or the number of times that the number being compared does not equal the one it is being compared to. how do I get it to only return the number of distinct values? The data set mentioned above is what I am using for input data so it should only have 3 values.

View 9 Replies View Related

C :: Counting The Number Of Case Selections In A Menu

Sep 20, 2013

Code:
#include<stdio.h>
int main () {
int choice, opens, saves, news;

[Code]....

So what would I have to add in order for this code to display how many times the user has selected open, save or new?

View 2 Replies View Related

C++ :: Counting Total Number Of Each Topic Separately?

Feb 25, 2015

How can i count total number of each topic separately? For example total number of topic 1 between all nodes. To begin enter 10 and 7 to initial node number and number of topic.

#include <iostream>
#include <vector>
#include <list>
#include <stdlib.h>
#include<time.h>
#include<iomanip>
#include <iostream>
#include <string>

[Code] ....

View 1 Replies View Related

C/C++ :: Counting Specific Amount Of Times A Number Shows Up

Apr 11, 2015

Ok, so I am writing this program with 10 different functions, and one of those functions needs to count how many times 0 appears in a text file. I've done this before, but I am so stumped right now. Should I get the numbers from the 2d array I have, or should I just use the text file here? Here is what I have right now:

int toursMissed(int scores[][COLS]){
int counter;
for(counter=0;counter<=96;counter++){
if(scores==0){
counter++;
return counter;
}
}

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved