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.
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:
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.
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.
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' ;
}