| | |
File array question
![]() |
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
Hey guys,
I have the following code, but for some reason it does not work with bigger txt files is this because of the size of the array?
And if so is there a simple way to solve the problem?
Thanks in advance for any help.
I have the following code, but for some reason it does not work with bigger txt files is this because of the size of the array?
And if so is there a simple way to solve the problem?
Thanks in advance for any help.
C++ Syntax (Toggle Plain Text)
char ch[1000]; int v_word = 0; int c_word = 0; ifstream file("c:/test.txt"); if (file.is_open()) { while (file.getline(ch, 1000)) if(strcmp(ch, "int") == 0) v_word++; } cout<<v_word<<endl; cout<<c_word<<endl; file.close();
it should work with text files of any size. Are any lines longer than 1,000 bytes? What is the purpose of checking if one of the lines = "int" ? Is there anything else following "int" on that line?
This is a c++ program -- why don't you use std::string instead of c-style arrays?
This is a c++ program -- why don't you use std::string instead of c-style arrays?
C++ Syntax (Toggle Plain Text)
#include <string> ... std::string line; int v_word = 0; int c_word = 0; int spot; ifstream file("c:/test.txt"); if (file.is_open()) { while (getline(fin,line) ) if( (spot = line.find("int")) >= 0) v_word++; }
what do you mean by "it doesn't work"? what doesn't work? strcmp() compares the entire line with the text you put in literals, in your example it is "int". So if the line is "int some more stuff here", then strcmp() will fail. If you only want the first three characters, the use strncmp(), or if "int" can appear anywhere in the line use strstr(). Also you have to be careful with the comparison because strstr() will also find "intabc" is the same as "abcintabc" and " int ". So if strstr() finds the text "int" the program should check for space character immediately before and after. If there is no white space before and after, then strstr() did not find a word.
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
Hi thanks for your reply. I have tried to use strstr() but I keep getting the wrong word count.
For example all the txt file contains is "hello james" and it gives the count 0 for both words.
For example all the txt file contains is "hello james" and it gives the count 0 for both words.
C++ Syntax (Toggle Plain Text)
ifstream file("c:\\test.txt"); if (file.is_open()) { while (file.getline(ch, 10000)) { if(strstr(ch, "hello") == 0) v_word++; if(strstr(ch, "james") == 0) c_word++; } }
C++ Syntax (Toggle Plain Text)
if(strstr(ch, "hello") == 0)
strstr is a pointer to the match or a NULL pointer when no match was found. Here you are checking for a NULL pointer, meaning when no match was found, so you would be incrementing your counter at the wrong time.Try this to find at least one occurence per line.
C++ Syntax (Toggle Plain Text)
if(strstr(ch, "hello") != 0)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
That what I thought but when I was tesing it if I used the code:
I got the wrong number of times the word occured.
but if I used the following code for strcmp()
it works. But with strcmp() shouldn't it have worked with the first piece of code?
C++ Syntax (Toggle Plain Text)
if(strcmp(ch, "hello") == 0)
I got the wrong number of times the word occured.
but if I used the following code for strcmp()
C++ Syntax (Toggle Plain Text)
if(strcmp(ch, "hello") != 0)
it works. But with strcmp() shouldn't it have worked with the first piece of code?
If the whole line is just "hello", then doing a
Even with
strcmp against "hello" will be 0. Otherwise it won't be ("hello james" is not the same as "hello").Even with
strstr, you may want to move down the string after a match. Consider an input file like this: C++ Syntax (Toggle Plain Text)
hello james hello james hello hello hello james james hello james
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { char ch[1000]; int v_word = 0; int c_word = 0; ifstream file("test.txt"); if ( file.is_open() ) { while ( file.getline(ch, 10000) ) { char *match = ch; cout << "ch : " << ch << '\n'; while ( (match = strstr(match, "hello")) != 0 ) { cout << "<<hello>> " << match << '\n'; v_word++; match += 5; /* the length of "hello" */ } match = ch; while ( (match = strstr(match, "james")) != 0 ) { cout << "<<james>> " << match << '\n'; c_word++; match += 5; /* the length of "james" */ } } } cout<<v_word<<endl; cout<<c_word<<endl; file.close(); return 0; } /* my output ch : hello james hello james hello hello hello james james hello james <<hello>> hello james hello james hello hello hello james james hello james <<hello>> hello james hello hello hello james james hello james <<hello>> hello hello hello james james hello james <<hello>> hello hello james james hello james <<hello>> hello james james hello james <<hello>> hello james <<james>> james hello james hello hello hello james james hello james <<james>> james hello hello hello james james hello james <<james>> james james hello james <<james>> james hello james <<james>> james 6 5 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- reading txt file into array (C++)
- input data from a file into an Array (C)
- reading from file into array (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: Win32 - Controls on main window
- Next Thread: vector<string> - way to find longest string?
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






