C/C++ :: Counting Character Occurrences In A File
May 26, 2012How can I count the occurrences of each character in a text file and then organize them by most occurring?
View 1 RepliesHow can I count the occurrences of each character in a text file and then organize them by most occurring?
View 1 RepliesI'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.
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,
I have the following code that does not work. It prints out 1 when it should print out 5 and 3 respectively. I know that the code is not the pretties due to ( *arr == value) but i am just learning C++.
Spoiler
template <typename T>
int countOccurrencesRecursively(T* arr, int size, T value){
//the base case is when the the array is empty
if(size == 0) return 0;
else return *arr == value + countOccurrencesRecursively(++arr, --size, value);
[Code] .....
Code:
#include <stdio.h>
int main(){
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f
", nc);
}
It works well and I am using ctrl-D as my EOF. Now I understand that C is not really a language for dealing with text, which is why I am learning it more in depth, my degree was in robotics, and I think C is definitely the best place to be for me. So this may just be a quirk, but if I type more than 10 characters, I get the right answer from the program, however, if I type less than 10, for the sake of argument I type Hello and then return, if I at that point do ctrl-D, I get the answer 6D?
The 6 is right 5 letters and a return character, but what is the D?
Problem i have is that is keep saying "error: file". I have test.txt. Byway i am using xcode 6 and I using C.
#include <stdio.h>
/* counts words in the file */
int main(void) /* C89 ANSI */
{
FILE *fp;
int r; /* a variable for result of a function, returning int */
size_t n; /* the words counter */
[Code] ......
Double values are stored in text file. 23.5 36.8 34.2 ... My teacher told me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi(). I have to convert it into double. but i dont know how to do this....
View 5 Replies View RelatedI have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below
rhhrkkj$hghjhdf$ddfkrjt
While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.
Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.
How do I write an a program that will read an input file character by character?
View 1 Replies View RelatedIn my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is
void typeset (int maxWidth, istream& documentIn)
I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file.
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] .....
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int num;
int count = 0;
[code]...
The last number in the numbers.txt file is: -1, and I am not allowed to include it in the calculation of total, average, etc.
I tried to make an if statement to not include -1 but, I can't figure it out. This compiles with or without my num == -1 statement.
I want do delete all occurrences of a char in a given string, but I really don't see my error in the code. Here is how I did it:
Code:
char *input = "example_string";
int len = strlen(input);
char *new_line[len];
char bad = 'e'; //the one to delete
int i;
int j = 0;
[Code]...
I get segmentation or bus errors.. And I also don#t understand the warning I get. new_line is an array of chars, and I am trying to assign to it a certain char.
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 Relatedit 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] .....
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 RelatedI need to read from a text file and search for a string that appears in the file and then count how many times it appears. My count just prints zero though. Do you see what I did wrong in my code for this to happen?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
[Code]....
I have the following program
#include "stdafx.h"
#include "string.h"
#include "ctype.h"
/*selection sort*/
void swap(int A[], int j, int k) {
int p = A[k];
[Code] ....
Theoretically it should cound the number of apperances of each character in a string, but:
1) It is counting characters from letter "j" upwards thus all asci characters below "j" are not counted, why is that?
2) I want also to make this code calculate the number of apperances of each character in a txt file. How to do that, i know i have to open a file in read mode, that's all (I am new to c programming)?
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] ....
The user will first enter the file name to be processed. The program can do the following 5 tasks:
1. Count the number of words
2. Count the number of alphabets (without punctuation marks)
3. Count number of sentences.
4. Count the frequency of each vowel.
5. Count frequency of the following three words individually: FAST, computer and engineering
There should be a proper menu through which the user can select the desired task. The program should continue until the user asks to terminate it.
Here is what i have done so far:
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(void)
{
int opt=0; //option number
ifstream fin;
string filename;
[Code]...
There are a few problems.
1- When I enter option 'q', an infinite loop starts.
2- The first time, the program gives the correct value. After that, each time it gives answer ZERO.
3- The last part is not working. I experimented something which failed. I don't know how to do the last part.
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]....
I am supposed to read(scan) data from a folder with multiple(21578) text files, and file names are numbered from 1 to 21578,and read each word that occurs in a text file, and count the number of times it occurs in the entire folder,i.e; in all the files how do i go about it? I've tried this so far..but it isn't working..
#include <iostream>
#include <map>
#include <string>
using namespace std;
void incrementString(map<string, int> &theMap, string &theString) {
if(theMap.count(theString)) {
[Code] ....
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 RelatedI'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] .....
I have this code for a computer project... (store management) but the character strings are not copied on text file..
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Store {
public:
char *item_name[5];
store()
[Code] .....
Now when i run the program, it gives a error :::
ERROR
address 0x0
How can i write these strings to the text file?
I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)
_______lastname_______firstname__90__________lname __________fname_100___________last___________first __60
When printed out, the names are formatted as follows:
lastname, firstname: 90
lname, fname: 100
last, first: 60
However, when I print them out this is what I get:
lastname, firstname: 90
lname, fname: 100$
last, first: 60H
For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.
Code: /*
- Prints a single record stored in the file pointed to by ifp.
*/
void print_record(FILE *ifp, int record) {
[Code]......