Can you ignore lines in a txt file? ifstream

Is there a way i can "comment out" lines in a txt file that im reading data from? The actual prefix im looking for to decide wheather or not to ignore the line dosent really matter, just thought comment's would be the best example. So kinda like- (yes im sure this is wrong, its just an example)
Code
1
2
3
4
5
6
if(GetLine = "//"){
//ignore it
}

File >> line1;
File >> line2;

whatever.txt
1
2
3
1
//Dont read me
2

Results
1
2
3
line1 = 1;

line2 = 2;

Last edited on
no you can't. you first have to read the line in order to know it says: Dont read me

but you can decide after you read it, what you want to do with it
you can ignore .
use File.ignore(1,\'n') ignore next line
then read another line
Yes, you can ignore lines in a file (sorta)...

First, create a file in a directory of your choosing... I put the following data in the text file:
----------FILE-------------
;Database
Do this here

123
33
79
---------EOF----------------
The code...

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	const string aFile = "C:/YOUR_DIRECTORY/Example.txt";

	ifstream f(aFile);
	string line;


	while (!f.eof()) {
	getline(f,line);

	if (line.length() == 0 || line[0] == ';')
		cout << "IGNORE LINE\n";
	else
		cout << line << "\n";
	}
  system("pause");
	return 0;
}


The screen printout looked like this:

IGNORE LINE
Do this here
IGNORE LINE
123
33
79
Last edited on
cant getline read until it hits something? traditionally thats endl but can't it be the line?
Thx for that Hellfire!

I'm sure I will use this if (line.length() == 0)

I couldn't get yours to compile so changed it to this:

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

int main()
{
     string line;
     ifstream inputfile;
     inputfile.open("out2.txt");

     while (!inputfile.eof()) 
     {
     getline(inputfile,line);

     if (line.length() == 0 || line[0] == ';')
     {cout << "IGNORE LINE\n";}
     else
     {cout << line << "\n";}

     }// End While
     inputfile.close();
     return 0;
}
Topic archived. No new replies allowed.