Strings -missing something

Hello All,
Definite newbie here! I want this code to read a file and take each line that does not begin with "." and put it in another text file. It appears the "if" statement is only ran once. I wonder if I am missing dealing with the new line character. Any help would be great :)

Sample input file:
.
D41D8CD98F00B204E9800998ECF8427E .
./.DS_Store
194577A7E20BDCC7AFBB718F502C134C ./.DS_Store
./check.txt
5E8F1ED0F0F32C52DA0C7B682E159C46 ./check.txt
./CheckDuplications.sh
BE7AEE039D106ED86FE06B6A0C2C6CDC ./CheckDuplications.sh
./ChecksumErrorts.txt
D41D8CD98F00B204E9800998ECF8427E ./ChecksumErrorts.txt
./ChecksumOutput.txt
2001C2250DBA22E6BFF2271448C83E15 ./ChecksumOutput.txt
./dups.txt
D41D8CD98F00B204E9800998ECF8427E ./dups.txt
./test copy 2.sh
997A4E239A6DF944A34BC840E7E02B4E ./test copy 2.sh
./test copy.sh
997A4E239A6DF944A34BC840E7E02B4E ./test copy.sh
./test.sh
997A4E239A6DF944A34BC840E7E02B4E ./test.sh


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
55
56
57
58
59
60
61
62
63
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>



using namespace std;

int main(){

	string string1;

	string string2 = ".";

	string string3;

	string string4;

	ifstream fileIn;

	ifstream fileIn2;

  	ofstream fileOut;

		fileIn.open("Check.txt");

		if ( ! fileIn ) {

			cout << "Error: Can't open the file named Check.txt.\n";

		} 



	getline( fileIn, string1);

	while( true) {

		getline( fileIn, string3);	

			cout << "\nThis is string3: " << string3;

			string4	= string3.substr( 0, 1 );

			cout << "\nstring4 " << string4;

			if ( string4.compare(string2) != 0){

				fileOut.open("test.txt");

				fileOut << string3;

				string3.clear();

				string4.clear();

			}

		if ( fileIn.eof()) {

			break;


}

}

return 0;

}
eof() is faulty sometimes with ASCII text. it is really only reliable for binary because sometimes eof() will think that a space or a new line character is the end of your file (or so ive heard when i learned (which wasnt too long ago, last week).

try

 
if(fileIn.fail() = 1)


instead. fail() will return a value of 1 if it can no longer read your file (when it reaches the end of the file it wont be able to read it anymore)
Hi Fragile Cat,

Let me see. I think you have some extra code. I'll try to put it the minimum size to perform your task.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(){
      string temp;
      ifstream inputFile("Chack.txt");
      ofstream outputFile("test.txt");
      while( !inputFile.eof() ){
            getline(inputFile, temp);
            if( temp[0] != '.' )
                 outputFile << temp << endl;
      }
      return 0;
}


Tell me how will this work..
Last edited on
Hi again,

I have also checked your code. The main problem that causes your code to look as if it is doing the if statement once is that you are opening the fileOut inside the loop. Whenever you open the file that way, it will restart the file and write to it as an empty file. Therefore you will only get the last line, because everytime you are writing to an empty file. So to fix that just put the line:
 
fileOut.open("test.txt");

Anywhere before the while loop and you're done.

Yet there are two more fixes needed for your code:
1. Make the line that will write on the output file look like this:
 
fileOut << string3 << endl;


2. Remove these unnecessary clear lines:
1
2
string3.clear();
string4.clear();


Please tell me how will things go.
Last edited on
Hi ShantDashjian,
Thanks for replying (everyone ;)). I will post my results back here when I have finished. The last two lines I put in to see what they would do, if you don't play you don't know what you can do. Once again thank you.

Fragile Cat
Topic archived. No new replies allowed.