Arrange of txt files under one condition

Hello, My name is Carlos and this is my problem

I'm trying to read one txt file, and pick up the 46 character of the first line, then if this character is a "B", I need to start copying in another txt files the next lines until this character (46), to " " and angain, I made this begginers code to check the 46 character and is a B, but when I try to use If or while, it doesnt work.

This is my code

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <limits>

using namespace std;

int main(int argc, char *argv[])
{

ifstream file("Maxwell.txt");//string asignado a archivo maxwell
ofstream Id("IDEAL.txt");//string asignado a archivo IDEAL
ofstream Ck("ck.txt");
ofstream imput;



file.ignore(46);
char check[2];
file.get(check,2);
imput.open("ck.txt");
Ck<<check<<endl;
cout<<check<<endl;
if (check=="B")
cout<<"good"<<endl;
else
cout<<"very bad"<<endl;

Hope to find some one to help me

Best regards

file.ignore(46);

The next character you extract will be the 47th character in the file.


if (check=="B")

Compares the address of check to that of "B" and that will never evaluate to true.


1
2
3
#include <cstring>

if ( 0 == std::strcmp(check, "B") )


Or, alternatively, extract the character to check as a char instead of a c-string and compare to the character constant 'B'.

Hello, cire, it works perfect!! but now the question is, how do I ignore, all the lines with the previos condition(check="B") to make a second file with all the other lines, like this:

#include <limits>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{

ifstream file("Maxwell.txt");//string asignado a archivo maxwell

ofstream Id("IDEAL.txt");//string asignado a archivo IDEAL
ofstream imput;
char line1[256];
string line;

while(getline(file,line))

{
file.get(line1,48);
imput.open("IDEAL.txt");
Id<<line1<<" "<<endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}


Thanks in advance

Carlos

If I understand correctly, you want to read in each line from the source file and output it to the destination file on the condition that the 46th character in the line is not a 'B'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Open your file for input.
// Open your file for output.

string line ;

while ( getline(input,line) )
{
     // Check if the 46th character in line is 'B'
     // Advisable to check line.length() first.
     // if it isn't, write the line to the output file
     // and follow it with a '\n' since that won't be
     // in the line extracted.
}

// we're done. 


Fairly close. You're getting bogged down in the details.
Pretty much is what I want to do, but, I want to extract the line if the 46th character
is a 'B', this is What I've got, but What is happening is that the program after the if, is
still ignoring the 46 characters, so the output file is wrong.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{

ifstream file("Maxwell.txt");//string asignado a archivo maxwell

ofstream Id("IDEAL.txt");//string asignado a archivo IDEAL
ofstream imput;
char line1[256];
string line;


while(getline(file,line))

{
file.ignore(46);
char check[2];
file.get(check,2);

if ( 0 == std::strcmp(check, "B") )
{
file.get(line1,48);
imput.open("IDEAL.txt");
Id<<line1<<" "<<endl;
}


}

system("PAUSE");
return EXIT_SUCCESS;
}

Best Regars
cire wrote:
file.ignore(46);

The next character you extract will be the 47th character in the file.


CarlosSalguero wrote:
I want to extract the line if the 46th character
is a 'B'
Last edited on
Hello. It was my mistake, I want to check the 47th, character, if it's a 'B', copy the line, and 6 spaces, the code that I hace and I send to you before what it does is, copy the lines with B in the 47th character, but it ignores the first 46 charactes on the line.

Best Regars

Basically what you are currently doing is: Read in a line. If the 47th character of the next line is 'B' then you get the rest of the next line. Then you try to open another file stream object that you never use except to open multiple times without closing. Then you output the rest of the next line. Every other line of the file is completely discarded.

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

int main()
{
	std::ifstream in("Maxwell.txt");//string asignado a archivo maxwell
	std::ofstream out("IDEAL.txt");//string asignado a archivo IDEAL
	std::string line;

	while(std::getline(in,line))
		if ( line.length() >= 48 ) // if length were 47, the line ends with 'B' -- nothing to output
			if ( line[46] == 'B' ) // 47 character in line.
				out << line.substr(47) << '\n' ;
}
Topic archived. No new replies allowed.