work with text file

Pages: 12
Hello!I have to check one txt file for simetrical words(like 1221 or abccba) and put these simetrical words into other file.End of word is ().,!? or space

I have figured out how to find out if string is simetrical.

int main() {
char s[100];
gets(s);
int len=strlen(s);
bool b=true;
for (int i=0;i<len;i++)
if (s[i]!=s[len-i-1]) b=false;
if (b) cout << "is!\n"; else cout << "is not\n";
system("Pause");
return 0;

But i have no idea how f.open f.close works together with that.And how can i make neew second file with data of the first file.
Most of what you need is given on:

http://www.cplusplus.com/doc/tutorial/files/

Iterate over all the words in the file (reading them using 'getline'), and run your code (I suggest putting it in a separate method) on each one. If b is true, write the word to an output file using an ofstream.

Hope that helps.

Also question-how to make read symbols one by one,but stop it ,when !?(),._ is in file?
if i got your question then;
1
2
3
4
5
while(file)//here we tell the compiler that while the file txt is not finshed do whats in the loop .when the compiler recognize thats its the end of file?its when it sees \n which is by nature at the end of each txt file
{
     getline(inData,symbols);//the get line here takes what ever it will read and save it as a string even if there were spacess
}


also see :http://www.cplusplus.com/reference/iostream/istream/getline/
hopefully it helps you ^-^
OK,that helps.But how to make stop loop when !?.,() is the symbol?
i have a feeling it might not quite work with the char array situation but you could probably find a way to adapt

1
2
3
4
if ( s == '!' || s == '?' || s == '.' || s == ',' || s == '(' || s == ')' )
{
     i = len;
}


if you don't know what the '||' things are, look at here: http://www.cplusplus.com/doc/boolean/

also, if you're actually trying to make it end when that entire thing is loaded into the array,

1
2
3
4
if ( s == "!?.,()" )
{
     i = len;
}


EDIT: Ignore this, it's not what you need :P
Last edited on
I have done this far.Know there is full of mistakes :D Dont know how write word in new file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <fstream>


using namespace std;
bool b=true;
int arr[i];
char c;
int main() {

ifstream inFile;
inFile.open("example.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1);
}

while (!fin.eof()) {
     get.fin(ch)
if ('!' == ch || '?' == ch || ',' == ch || ','==ch || '('=ch || ')'==ch)
break;
else
{ch=arr[i]
i++;}
}

int len=i;
bool b=true;
for (int i=0;i<len;i++)
if (s[i]!=s[len-i-1]) b=false;}
}

inFile.close();
system("pause");
return 0;
}
Ooh, even better now.

Alrighty, just a change here:

1
2
3
4
5
6
7
8
while (!fin.eof()) {
     get.fin(ch)
if (('!' == ch || '?' == ch || ',' == ch || ','==ch || '('=ch || ')'==ch)
break;
else
{ch=arr[i]
i++;}
}


change it to:

1
2
3
4
5
6
7
8
9
while (!fin.eof() && ch != '‰') {
     get.fin(ch)
if ((ch != '!' && ch != '?' && ch != ',' && ch != '(' && ch != ')' )
     {ch=arr[i]
     i++;}
else
     ch = '‰'; // the odd character ‰ is a character that I highly doubt would be put into
               // anything.
}


As far as I've learned, breaks shouldn't be used for while loops in any situation, I don't have a compiler on this computer so I can't remember if it makes an error or what. I just know it isn't good XD. Note to anyone who finds mistakes in my advice, please correct me if i'm wrong, i'm not claiming to know everything perfectly.
Last edited on
So some questions now:

1)how to get '‰' on keyboard?:D and what it does?

2)I get word as array-how to check if array is simetrical.I can do it only with string.

3)How to write into second file simetric word?
1) to get ‰ on the keyboard, hold down the alt key and press the numbers 0-1-3-7 on the numpad.

2) unfortunately i don't know :/ i'll do some experimenting later to try to figure that one out

3) i don't quite get what you're trying to ask there... explain please?
Last edited on
about #3

I have to read data from file1 and put them into file2
Final version.compiler doesnt give errrors ,but program dont work:( where is mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>


using namespace std;

int main() {
    int i;
int arr[i];


ifstream fin;
fin.open("example.txt");
if (!fin) {
cout << "Unable to open file";
exit(1);
}
char ch;
while (!fin.eof() && ch != '‰') 
{
     fin.get(ch);
if (ch != '!' && ch != '?' && ch != ',' && ch != '(' && ch != ')' )
     {arr[i]=ch;
     i++;}
else
     ch = '‰';
}

int len=i;
bool b=true;
for (int i=0;i<len;i++)
if (arr[i]!=arr[len-i-1]) b=false;
else
{
ofstream myfile ("example1.txt"); 
if (myfile.is_open()) 
{
myfile << arr[i]; 

myfile.close(); 
}
}
fin.close();
system("pause");
return 0;
}
My compiler gave errors :). On line 9 you're trying to statically declare an array with a non-constant expression. If you want to declare the array like that, you have to put in a constant expression in the square brackets (e.g. '5' or some constant integer). You should think a bit about what you're doing with this array though...do you want to declare it statically?

Once you fix that it should compile, but that DEFINITELY DOES NOT MEAN YOUR PROGRAM WORKS! On line 19 you're trying to use the character variable ch before it is initialised. This will give you a run-time failure.

There's other things wrong, but you'll learn more if you sweat over trying to find them. So yeah, hardly the 'final version' in my opinion...

Feel free to post your next attempt when you get stuck...
Oh, and by the way, 'symmetrical words' are called 'palindromes'.
almost working..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>


using namespace std;

int main() {
int gar;
bool sim=true;
char simb;
char arr[40];
ifstream f1;
ofstream f2;
f1.open("example.txt");
if (!f1) {
cout << "Unable to open file";
exit(1);
}
f2.open("example2.txt");
if (!f2) {
cout << "Unable to open file";
exit(1);
}

while (!f1.eof()) 
{
     f1.get(simb);
if (simb != '!' && simb != '?' && simb != ',' && simb != '(' && simb != ')' )
     
     {
     arr[gar]=simb;
     gar++;}
else
     simb = '‰';

for (int i=0;i<gar;i++)
if (arr[i]!=arr[gar-i-1]) sim=false;
else sim=true;
}
if (sim)
{
ofstream f2 ("example1.txt"); 
if (f2.is_open()) 
{for(int i=0;i<gar;i++)
f2 << arr[i]<<endl; 

 
}
}
f2.close();
f1.close();
system("pause");
return 0;
}
To fix the uninitialized variable on line 18 just add the "= something" or you can consider a do-while loop.
Do-while loops always run at least once, check some condition, and then repeat if true or quit if false. Sammy speaks the truth. Many beginners fall into the snare of just trying to get their program to just compile without errors, but the does not imply automatically that the program does its job correctly.
Does your program read the numbers in correctly yet? I would suggest having some cout statements just to check if all the numbers are being read correctly. If they are, and only if they are, move on to the next part of your program that depends on that.
If you do get your program to compile error free, please post it again and if you would be so kind to post some amount of the input file, so we can test it with the data you use.
that's all.But i dont know why example2.txt stilll remains empty after this..

To kevinkjt2000:

in file example.txt i have put random text like:

anna anasana gvbmmf mhvfhgcd hgvjygkhgk 1221

Words in bold have to be written in example2.txt


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>


using namespace std;

int main() {
int gar=0;
bool sim=true;
char simb;
char arr[40];
ifstream f1;
ofstream f2;
f1.open("example.txt");
if (!f1) {
cout << "Unable to open file";
exit(1);
}
f2.open("example2.txt");
if (!f2) {
cout << "Unable to open file";
exit(1);
}

while (!f1.eof()) 
{
     f1.get(simb);
if (simb != '!' && simb != '?' && simb != ',' && simb != '(' && simb != ')' )
     
     {
     arr[gar]=simb;
     gar++;}
else
     simb = '‰';

for (int i=0;i<gar;i++)
if (arr[i]!=arr[gar-i-1]) sim=false;
else sim=true;
}
if (sim)
{
ofstream f2 ("example2.txt"); 
if (f2.is_open()) 
{for(int i=0;i<gar;i++)
f2 << arr[i]<<endl; 
f2.close();
 
}
}

f1.close();
system("pause");
return 0;
}
On line 37 (when i == 0) you're attempting to access the element of arr at index -1! That will corrupt the stack around this variable...not a good idea :(.

Think about this. Let's say you have a word 'sayak'. If I'm not mistaken, in the 'for' loop, you will first check the outer characters 's' and 'k' and decide that it's NOT a palindrome. Then, on the next pass through the 'for' loop, you'll see 'a' and 'a' and say that it IS a palindrome! Do you see where I'm going with this? Follow kevin's advice too...get the basics right first before you get bogged down with the more complex details...
I give up..
This is good...you're annoyed and confused. This is the turning point and typically where most of the learning happens (in my opinion at least).

At this point you can either take a break, clear your head and come back with a tenacious spirit, or you can really give up. It's your decision, but if you choose the former, you'll be proud of yourself when you eventually get the solution out...
Pages: 12