-
C++ Files
Hey guys, I was trying to make code that would count how many times the word hello appears in a text files but the code below doesn't compile so I was just wondering where I when wrong?
Thanks.
Code:
char ch [10000]
int v_word;
ifstream file("c://test.txt");
if (file.is_open())
{
while (! file.eof() )
{
while (file.getline(ch))
{
if(ch=="hello")
v_word++
}
}
}
file.close();
-
You cannot use == with character arrays. You should use C++ strings instead, or, if you must use C style strings, use strcmp.
The forward slash does not need to be escaped, so your file path should be "c:/test.txt" or "c:\\test.txt".
You shouldn't use eof() to control the while loop. Simply use the call to getline as the while control. You already do this in the inner loop, so the outer eof() loop is redundant and should be removed. If you don't remove it and the input fails for some reason, the code will loop forever.
-
Code:
char ch[10000];
int v_word = 0;
ifstream file("c:/test.txt");
if (file.is_open()) {
while (file.getline(ch, 1000))
if(strcmp(ch, "hello") == 0)
v_word++;
}
file.close();
-
Hey guys thanks for the replies, I was wondering what following code does?
Code:
while (file.getline(ch, 1000))
I was also wondering if I could then use the code below to search for more words by just adding more if statements?
Thanks again for your help and sorry for the newbie questions.
Code:
char ch[10000];
int v_word = 0;
ifstream file("c:/test.txt");
if (file.is_open()) {
while (file.getline(ch, 1000))
if(strcmp(ch, "hello") == 0)
v_word++;
}
file.close();
-
that's exactly (well almost exactly) what i asked two posts below yours!!!! grrr
-
Ah lol sorry I did not see your post, what kind of thing are you trying to do?
-
yes by adding if statements you could test for other words. Uhm, do you actually know how to program in for example C or visual basic? Because those are quite basic questions.
Code:
while (file.getline(ch, 1000))
does (grosso modo):
as long as the file hasn't been fully read, do "file.getline(ch, 1000)". Now, that command means, get a line from the file and store it in the char array. A line means a row of characters untill a line break. The thousand stands for, if the line is longer than 1000 characters, it won't fit in your array "ch", so to avoid errors, we only read untill 1000 chars. In the next pass, the rest of the line would be read.
-
Hey, thanks again for your reply as you can probably tell I am brand new to programming so I am finding it a bit hard to get my head round is there anyway that a whole file could be read without having to make a huge array? Thanks again.
-
Note that you cannot simply add if statements to djbe's example code, since the while would not include any extra statements.
Also, you really should consider using C++ strings over C style strings if you are programming in C++.
-
I have made a Css (Cascading Style Sheet) editor with C++. It was done early in my learning so it runs on MS DOS, but I think I did a good job on it.
Code:
//CSS EDITOR BASIC beta V.1 by Andrew Benenati (c) 2006
#include <iostream>
using namespace std;
int main()
{
int h1; {
cout<<"Please enter your background color number here for header 1: ";
cin>> h1;
cin.ignore();
if ( h1 == 1 ) {
cout<<"h1{ background: Red;\n";
}
else if ( h1 == 2 ) {
cout<<"h1{ background: Orange;/n";
}
else if ( h1 == 3 ) {
cout<<"h1{ background: Yellow;/n";
}
else if ( h1 == 4 ) {
cout<<"h1{ background: Green;/n";
}
else if ( h1 == 5 ) {
cout<<"h1{ background: Blue;/n";
}
else if ( h1 == 6 ) {
cout<<"h1{ background: Purple;/n";
}
else if ( h1 == 7 ) {
cout<<"h1{ background: Black;/n";
}
else if ( h1 == 8 ) {
cout<<"h1{ background: Gray;/n";
}
cin.get(); // Put this after every "int" direction
}
int h2; {
cout<<"Please enter your background color number here for header 2: ";
cin>> h2;
cin.ignore();
if ( h2 == 1 ) {
cout<<"h2{ background: Red;\n";
}
else if ( h2 == 2 ) {
cout<<"h2{ background: Orange;/n";
}
else if ( h2 == 3 ) {
cout<<"h2{ background: Yellow;/n";
}
else if ( h2 == 4 ) {
cout<<"h2{ background: Green;/n";
}
else if ( h2 == 5 ) {
cout<<"h2{ background: Blue;/n";
}
else if ( h2 == 6 ) {
cout<<"h2{ background: Purple;/n";
}
else if ( h2 == 7 ) {
cout<<"h2{ background: Black;/n";
}
else if ( h2 == 8 ) {
cout<<"h2{ background: Gray;/n";
}
cin.get();
}
int h3; {
cout<<"Please enter your background color number here for header 3: ";
cin>> h3;
cin.ignore();
if ( h3 == 1 ) {
cout<<"h3{ background: Red;\n";
}
else if ( h3 == 2 ) {
cout<<"h3{ background: Orange;/n";
}
else if ( h3 == 3 ) {
cout<<"h3{ background: Yellow;/n";
}
else if ( h3 == 4 ) {
cout<<"h3{ background: Green;/n";
}
else if ( h3 == 5 ) {
cout<<"h3{ background: Blue;/n";
}
else if ( h3 == 6 ) {
cout<<"h3{ background: Purple;/n";
}
else if ( h3 == 7 ) {
cout<<"h3{ background: Black;/n";
}
else if ( h3 == 8 ) {
cout<<"h3{ background: Gray;/n";
}
cin.get();
}
int h12; {
cout<<"Please enter your font number here for header 1: ";
cin>> h12;
cin.ignore();
if ( h12 == 1 ) {
cout<<"h1{ font: Courier New;\n";
}
else if ( h12 == 2 ) {
cout<<"h1{ font: Arial;/n";
}
else if ( h12 == 3 ) {
cout<<"h1{ font: Times New Roman;/n";
}
else if ( h12 == 4 ) {
cout<<"h1{ font: Jokeman ;/n";
}
else if ( h12 == 5 ) {
cout<<"h1{ font: Comic Sans MS;/n";
}
else if ( h12 == 6 ) {
cout<<"h1{ font: Edda;/n";
}
else if ( h12 == 7 ) {
cout<<"h1{ font: Lucida Console;/n";
}
else if ( h12 == 8 ) {
cout<<"h1{ font: Lucida Handwriting;/n";
}
cin.get(); // Put this after every "int" direction
}
int h22; {
cout<<"Please enter your font number here for header 2: ";
cin>> h22;
cin.ignore();
if ( h22 == 1 ) {
cout<<"h2{ font: Courier New;\n";
}
else if ( h22 == 2 ) {
cout<<"h2{ font: Arial ;/n";
}
else if ( h22 == 3 ) {
cout<<"h2{ font: Times New Roman;/n";
}
else if ( h22 == 4 ) {
cout<<"h2{ font: Jokeman;/n";
}
else if ( h22 == 5 ) {
cout<<"h2{ font: Comic Sans MS;/n";
}
else if ( h22 == 6 ) {
cout<<"h2{ font: Edda;/n";
}
else if ( h22 == 7 ) {
cout<<"h2{ font: Lucida Console;/n";
}
else if ( h22 == 8 ) {
cout<<"h2{ font: Lucida Handwriting;/n";
}
cin.get();
}
int h32; {
cout<<"Please enter your font number here for header 3: ";
cin>> h32;
cin.ignore();
if ( h32 == 1 ) {
cout<<"h3{ font: Courier New;\n";
}
else if ( h32 == 2 ) {
cout<<"h3{ font: Arial;/n";
}
else if ( h32 == 3 ) {
cout<<"h3{ font: Times New Roman;/n";
}
else if ( h32 == 4 ) {
cout<<"h3{ font: Jokeman;/n";
}
else if ( h32 == 5 ) {
cout<<"h3{ font: Comic Sans MS;/n";
}
else if ( h32 == 6 ) {
cout<<"h3{ font: Edda;/n";
}
else if ( h32 == 7 ) {
cout<<"h3{ font: Lucida Console;/n";
}
else if ( h32 == 8 ) {
cout<<"h3{ font: Lucida Handwriting;/n";
}
cin.get();
}
int h13; {
cout<<"Please enter your font style number here for header 1: ";
cin>> h13;
cin.ignore();
if ( h13 == 1 ) {
cout<<"h1{ text-decoration: Underline;\n";
}
else if ( h13 == 2 ) {
cout<<"h1{ text-decoration: Bold;/n";
}
else if ( h13 == 3 ) {
cout<<"h1{ text-decoration: Italic;/n";
}
cin.get(); // Put this after every "int" direction
}
int h23; {
cout<<"Please enter your font style number here for header 2: ";
cin>> h23;
cin.ignore();
if ( h23 == 1 ) {
cout<<"h2{ text-decoration: Underline;\n";
}
else if ( h23 == 2 ) {
cout<<"h2{ text-decoration: Bold ;/n";
}
else if ( h23 == 3 ) {
cout<<"h2{ text-decoration: Italics;/n";
}
cin.get();
}
int h33; {
cout<<"Please enter your font style here for header 3: ";
cin>> h33;
cin.ignore();
if ( h33 == 1 ) {
cout<<"h3{ text-decoration: Underline;\n";
}
else if ( h33 == 2 ) {
cout<<"h3{ text-decoration: Bold;/n";
}
else if ( h33 == 3 ) {
cout<<"h3{ text-decoration: Italic;/n";
}
cin.get();
}
int p; {
cout<<"Please enter your background color number here for your paragraphs: ";
cin>> p;
cin.ignore();
if ( p == 1 ) {
cout<<"p{ background: Red;\n";
}
else if ( p == 2 ) {
cout<<"p{ background: Orange;/n";
}
else if ( p == 3 ) {
cout<<"p{ background: Yellow;/n";
}
else if ( p == 4 ) {
cout<<"p{ background: Green;/n";
}
else if ( p == 5 ) {
cout<<"p{ background: Blue;/n";
}
else if ( p == 6 ) {
cout<<"p{ background: Purple;/n";
}
else if ( p == 7 ) {
cout<<"p{ background: Black;/n";
}
else if ( p == 8 ) {
cout<<"p{ background: Gray;/n";
}
cin.get(); // Put this after every "int" direction
}
int p2; {
cout<<"Please enter your font number here for your paragraphs: ";
cin>> p2;
cin.ignore();
if ( p2 == 1 ) {
cout<<"p{ font: Courier New;\n";
}
else if ( p2 == 2 ) {
cout<<"p{ font: Arial;/n";
}
else if ( p2 == 3 ) {
cout<<"p{ font: Times New Roman;/n";
}
else if ( p2 == 4 ) {
cout<<"p{ font: Jokeman ;/n";
}
else if ( p2 == 5 ) {
cout<<"p{ font: Comic Sans MS;/n";
}
else if ( p2 == 6 ) {
cout<<"p{ font: Edda;/n";
}
else if ( p2 == 7 ) {
cout<<"p{ font: Lucida Console;/n";
}
else if ( p2 == 8 ) {
cout<<"p{ font: Lucida Handwriting;/n";
}
cin.get(); // Put this after every "int" direction
}
int p3; {
cout<<"Please enter your font style number here for your paragraphs: ";
cin>> p3;
cin.ignore();
if ( p3 == 1 ) {
cout<<"p{ text-decoration: Underline;\n";
}
else if ( p3 == 2 ) {
cout<<"p{ text-decoration: Bold;/n";
}
else if ( p3 == 3 ) {
cout<<"p{ text-decoration: Italic;/n";
}
cin.get(); // Put this after every "int" direction
}
}
-
How do you attach files, do you need an ftp server?
-
Oh and when my program asks for a "color number" it means a chart I made with the understood colors for each number. When I find out how to attach files I will give you my program with everything that comes with it.