File Managment, skiping certain lines from files

Good Evening All,
Im supposed to write a program that reads lines from files and see if the first
character is an odd number, if so it will out put the remaining charecters to output file, else itll seek to the beginning of the next line,
this is what i came up with but its not working as it should, im not sure if its the version im using or the program I wrote, the commented part below was another algorithem I was trying to use.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<fstream>
#include<iostream>
using namespace std;

int main()
{
	char c;
	int i =0;
	int k = 0;
	int seek=0;
	ifstream infile;
	ofstream outfile;
	infile.open("input.txt",ios::in);
	outfile.open("output.txt",ios::beg);

	
	while (! infile.fail())
	{
		

		infile.read(&c,(ios::cur));
		infile>>c;
		if( c=='1')
			k = 1;
		else if(c=='3')
			k = 3;
		else if(c=='5')
			k = 5;
		else if (c=='7')
			k = 7;
		i = 0;
	
	if(k%2==1)
	{
		cout<<c;
				//cout<<k<<endl;
				//seek+=k;
				//cout<<seek; to check seek values which turned to be right

				outfile<<c;
				
				infile.seekg(1, ios::cur);
				//i++;
				//seek+=i;
				
			
			
	}
	else
	{
		seek+=k;
		//cout<<k;
		infile.seekg(seek,ios::cur);
	}

		
	}


	infile.close();
	outfile.close();
	cout<<endl<<"be happy ran successfully "<<endl;

	return 0;
}

/*
int main(){
char c;
	ifstream infile;
	ofstream outfile;
	infile.open("input.txt",ios::in);
	outfile.open("output.txt",ios::beg);
 infile.unsetf(ios::skipws) ; //to read spaces and EOL
 infile.read(&c,1);

while (!infile.fail()){
 outfile << c;
 infile >> c;
 }
}
*/

	/*infile.close();
	infile.open("input.txt",ios::in);
	///////////////
		while (! infile.fail())
	{
		infile>>c;
		k[n]=static_cast<int>(c);
		n++;
		infile.seekg(k[n]);
	}
	*/
		
		///////////////////// 


my text file was as follows:
input.txt
3Ali
4Sami
6Rashid
3Aya

output was un expected , although some was relatively write but not as I wanted

thank you for your time
closed account (jyU4izwU)
it seams like your spaming the page you must first define the variables that conducts the equasion.
Is seeking, literally, a requirement? Generally, seekg()/seekp() are only guaranteed to work on the files opened in binary mode, while the concept of a "line" implies text mode.

How about you read a character and the rest of the line on every iteration? This makes for a very small program:

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

bool is_odd(char c)
{
    return c == '1' || c == '3' || c == '5' || c == '7' || c == '9';
}

int main()
{
    std::ifstream infile("input.txt");
    std::ofstream outfile("output.txt");

    char c;
    std::string line;
    while(infile.get(c) && getline(infile, line))
        if(is_odd(c))
            outfile << line << '\n';
}

output file, given your input, contains:
Ali
Aya
Last edited on
Loved your answer Cubbi, sadly the seeking is the requirement .
thats the thing annoying me with this program..

thx for the help though :)
Topic archived. No new replies allowed.