Reading specific lines

Hi. I have a program that I'm trying to read through a file, but the tricky part is that I'm only allowed to read some lines and store those line in c-string. This is what I have so far:

Sample.txt:
" I want to read this line1
I want to read this line2
I want to read this line3
I DO NOT want to read this line4
I want to read this line5
I want to read this line6
I want to read this line7
I DO NOT want to read this line8 "

This is my code so far:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int main(){
char letter;
int i,j;
j= O;
String file;

ifstream reader("sample.txt");
if (! reader ){
Cout<< " Error " << endl;
Return -1;
}
For (i=0; ! reader.eof(); i++){
while(! reader.eof()){
getline(reader, file)
Cout<< file << endl;
j++;
if ( j < = ( i + 2 )) {
break
}
}
}
Return 0;
}

Any help?? Please
First off, you haven't described the rule for what lines you should or should not read.
It looks like you need to skip every fourth line, but that is only a guess.

Second, what you posted won't compile. It is full of capitalization errors. C++ is case sensitive.

Third, do not loop on !eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (cin >> var) 
  {  //  Good cin or istream operation
  }


Fourth, PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{   int j = 0;
    string file;

    ifstream reader("sample.txt");
    if (! reader )
    {   cout<< " Error " << endl;
        return -1;
    }
    while(getline (reader, file)) 
    {   if (j % 4 != 3) //  Use modulo to skip evert 4th record
            cout<< file << endl;
        j++;
    }
    return 0;
}


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
thank you for your reply. I'm sorry for the lack of explanation. I will try your example. Here is in more detail.

Hi. I have a program that I'm trying to read through a file, but the tricky part is that I'm only allowed to read some lines and store those line in c-string. This is what I have so far:

Sample.txt:
" I want to read this line(1)
I want to read this line(2)
I want to read this line(3)
I DO NOT want to read this line(4)
I want to read this line(5)
I want to read this line(6)
I want to read this line(7)
I DO NOT want to read this line(8)

1- I want the program to read 3 lines then skip the 4th, leaving the 4th as an empty line, then store as a string file.
2- I want the program to use the character ' ( ' as a delim for the getline() function.

The ideal string resulting from the program it would be:

" I want to read this line
 I want to read this line
I want to read this line

I want to read this line
I want to read this line
I want to read this line"

This is my code so far:

< #[code][code]include<iostream>
#include<fstream>
#include<string>

using namespace std;
int main(){
char letter;
int i,j;
j= O;
string file;

ifstream reader("sample.txt");
if (! reader ){
cout<< " Error " << endl;
return -1;
}
for (i=0; ! reader.eof(); i++){
while(! reader.eof()){
getline(reader, file)
cout<< file << endl;
j++;
if ( j < = ( i + 2 )) {
break
}
}
}
retur 
n 0;
}[/code] >

I did you <> between my code

Any help?? Please
Last edited on
Your program still has compile errors.

Line 38. Should be 0, not O.

Lines 51-52 are not going to work to find every 4th line.

Line 52 is missing a ;

You've ignored my advice about when to test eof(). See my sample code for the proper way to test for eof.

Please place code tags only around your code, not around your entire message.

If you only want to read up to the (, I suggest using a stringstream.
Read each line into a string using getline. Create a stringstream from the string just read.
Do a delimited getline from the stringstream to get the data up to the (.
Last edited on
I didn't ignore your advise, I just edited my previous post.

Is this correct:

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>
#include<fstream>

using namespace std;
int main(){
int j = 0;
string file, newstr;

ifstream reader(" sample.txt");
if (! reader )
{

1
2
3
4
5
6
7
8
cout << " Error " << endl;
return -1;
}
while (getline ( reader, file))
{ if (j % 4 !=3)
cout << file << endl;
j++;
}

1
2
3
4
5
6
7
stringstream stream;
stream << file;
stream >> newstr;
getline ( file, newstr, ' ( ' );
cout << newstr << endl;
return 0;
}
Any help anyone??
Topic archived. No new replies allowed.