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
ADVERTISEMENT
Dec 20, 2013
I have written below program to count number of words and lines and print the all the words.
#include <iostream>
using namespace std;
#include<fstream>
#include<string.h>
int main() {
ofstream outfile;
[Code] .....
Its compiling fine but when executed its displaying I infinite times...
View 3 Replies
View Related
Mar 13, 2014
So I need to make a program that counts the number of words, lines, and vowels in a program. Punctuation and white spaces are not counted in any of the counts. I have this so far and the words, and lines work and are correct but I can't seem to get the vowel count to work.
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <string>
[Code]....
View 1 Replies
View Related
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
Apr 20, 2015
Im trying to figure out how to print a random number of asterisks on two separate lines at the same time. So every time you press a key it prints a different amount of random number of integers between1 and 10 until one of the lines reaches 70. I have the code to do one line but can't figure out how to do two at once.
#include <stdio.h>
#include <time.h>
#define MINR 1
#define MAXR 70
#define MINM 1
#define MAXM 10
int main (void)
[Code]...
View 2 Replies
View Related
Feb 21, 2014
I have program that is supposed to read in a story from an input file and separate the words and output the lines on which the word occurs. It needs to read in another input file that has a list of words (1 per line) to ignore. i.e. skip them when running through the story. This is what I have so far, I've changed multiple things trying to get it running....
#include<iostream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include"split.h"
[Code] .....
View 1 Replies
View Related
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
View Related
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
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
Mar 27, 2014
I have a program I have to do that counts the number of words in a text file. I have tried the code on 2 computers now since my programming teacher told me the code was fine. Here is my code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("tj.text" , ios::in);
[Code] .....
View 4 Replies
View Related
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
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
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
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
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
Mar 6, 2015
I am creating a trie (not a tree but a trie). I have the following structure:
Code:
struct tnode {
bool is_word;
list *children;
};
struct trie {
int length;
struct tnode *first;
};
list *children is a linked list that contain pointers to tnodes. For example: I have a trie with the words cup and cut:
c
u
/
p t
Given the example above you have the following linked lists:
1. [c] that contains a pointer to linked list 2.
2. [u] that contains a pointer to linked list 3.
3. [p, t]
Now I want to print the trie like:
cut
cup
View 2 Replies
View Related
Dec 4, 2013
I want to print the words of a sentence, given as a string..But I have a problem with the end of the sentence, and cannot find the bug....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char str[80]="This is a sentence";
int main(){
}
[code].....
View 7 Replies
View Related
Nov 12, 2014
I'm taking an operating systems class and I'm trying to write a script that would execute a program in Simple OS.I"m not sure if this is the right place for this because although the entire OS is written in C, "asm" are assembly language code. Anyway when I run the OS in virutal box and try to run a program, I get a "Fatal Execption" in a loop until the entire OS crashes. So then I try to debug my code BUT when I add printf statements and try to run the code again I don't get to the "fatal execption" and the entire OS just crashes. 1) why can't I add print lines? 2) what's going on with the fatal execption?
////////////////////////////////////////////////////////
// Loads program and executes it in user mode
//
#include "kernel_only.h"
[Code]....
View 3 Replies
View Related
Mar 19, 2015
I wrote this program to scan a number and a string until EOF then print them to a file named "data.list". the problem is that the program duplicates last line of input in the output file. so for example if the input is :
1 test
2 dream
3 code
then output (in data.list file) would be:
1 test
2 dream
3 code
3 code
I also changed the program code so that it reads from data.list file. even here it duplicates last line!
so when program reads the info above saved in data.list it would be:
1 test
2 dream
3 code
3 code
3 code
here's the code for writing:
#include <stdio.h>
int main( void )
{
int num;
char str[80];
FILE *fPTR;
fPTR = fopen( "data.list", "w" ); // opens a file named "data.list", create if doesn't exist
while(!feof(stdin)) // loop until End-Of-File is pressed. Ctrl-Z in Windows, Ctrl-D in Unix, Linux, Mac OS X
[Code]...
and the one for reading from file:
#include <stdio.h>
#include <conio.h>
int main( void )
{
int num;
char str[80];
FILE *fPTR;
[Code]...
How do I fix this behavior??
View 3 Replies
View Related
Mar 4, 2014
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
See [URL] ....
I'm trying to do this exercise from K&R on my own (with my own code), but I'm receiving a signal (Illegal instruction (Core dumped)) when the input is too large.
#include <stdio.h>
#define MAXWORDLENGTH 10
int main(void) {
int c; /* Character read */
long length[MAXWORDLENGTH + 1];
int reading_word = 0;
int word_size = 0;
[Code] ....
Where the problem might be occurring. I tried debugging with GDB but found no useful information.
Program received signal SIGILL, Illegal instruction.
0x00007ffff7a3b76e in __libc_start_main (main=0x4005d4 <main>, argc=1,
ubp_av=0x7fffffffe2a9, init=<optimized out>, fini=<optimized out>,
rtld_fini=<optimized out>, stack_end=0x7fffffffe298) at libc-start.c:258
258libc-start.c: No such file or directory.
The program output is also wrong when tested with the code provided at the link given above.
View 7 Replies
View Related
Sep 1, 2014
is there a to use printf to print certain number of blank spaces, where the number is derived from the output of a function?
for(m=low; m<=high;m++)
{
printf("t=%2d %'f(m)-1's %3c", m, ' ',*);
}
This is suppose to output
t=-3 *
t=-2 *
t=-1
.
.
.
View 2 Replies
View Related
Feb 3, 2014
I am looking for a way to correctly count the lines between two specified characters/strings in a file. Here's the part I need work on:
getline( file, lines );
do {
if(lines.find("character")
{
++counter;
}
} while( !lines.find("story") );
I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."
Right now, I am only getting a counter value of 1.
View 2 Replies
View Related
Feb 4, 2013
Below is the response XML my programmer is getting from business service -
<Data>
<Maintain>
<AssetList>
<AssetDetails>
<AssetDescriptor>
<ns1:ParentProductCat category="ABC">ABC</ns1:ParentProductCat
[code].....
In above XMl if you check the ParentProductCat is prefixed with ns1:,ns2:,ns3:....
My XSLT code only checkes for the ParentProductCat but since it is prefixed with "ns*:" (* is integer) it fails to find it. So that in C# code replace function is used to replace ns*: a for loop is used for this and it goes until 1000
But now my response crossed the 1000 mark and this time the XML contains total 14500 lines.So I want to replace all ns:*
For this I want to count the number of lines present in the XML so that the for loop will run until that line number. how to do that?
View 2 Replies
View Related
Feb 5, 2015
I trying to write a program able to read a number up to 3 digits and display it in words. What to do first.
View 1 Replies
View Related
Dec 10, 2013
I am trying to write a code that counts the number of words in a string that end with ng.
I started with this but this just checks the end of the string. I need it to check everyword in the string and also count them up.
int main() {
string s;
cout << " enter string ";
getline(cin, s);
string end;
end = s.substr(s.length()-2, 2);
cout << end;
cout << endl;
return 0;
}
View 19 Replies
View Related
Apr 28, 2015
#include <fstream>
#include <iostream>
#include <string>
[Code].....
When I try to run my program I get:
1>LINK : error LNK2001: unresolved external symbol _mainCRTStartup
1>C:UsersDocumentsVisual Studio 2010ProjectsCS 111Lab10Lab10DebugLab10.exe : fatal error LNK1120: 1 unresolved externals
What I'm trying to do is get my program to count the number of words in an inputted data file. I've attached the assignment directions
Attached File(s) : lab10_data_.zip (9.27K)
View 4 Replies
View Related