How to delete all lines and words from a text file up until a certain point - c++

The problem I have is that I have a text file which contains a whole load of rubbish and just at the bottom are a few lines which holds the information I need. I want to be able to delete all the rubbish and just keep the useful information.
Iam hoping i can attach the text file which can explain it better.
There is a specific post which is relevant to this post that I am doing, however I have followed it and it hasn’t solved the problem I have. I have tried arrays and many other things, it just hasn’t worked.
it has information like this in it
|83Ïú¯–ŸªÖ!
just at the bottom of the text file it has this
000---------
i want to keep everything after the 000 and the rest of it i want to remove.

Open the original file for reading.
std::ifstream fin(orig_filename, std::ios::binary);
Open a new temporary file for writing in the same directory
std::ofstream fout(temp_filename, std::ios::binary);
Search for the marker pattern in the original file:
std::string marker = "000---------";
auto it =
std::search(std::istreambuf_iterator<char>(fin),
std::istreambuf_iterator<char>(), marker.begin(), marker.end());
Copy the rest from fin to fout (if the pattern was found):
if(it == std::istreambuf_iterator<char>()) {
std::cout << "pattern " << marker << " not found\n";
} else {
// the iterator "it" is now at the last char in the marker pattern,
// step over it and copy the rest to fout
std::copy(++it, std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(fout));
}
Rename the temporary file to the same name as the original file:
std::rename(temp_filename, orig_filename);
Note: Opening the files in std::ios::binary mode is afaik only needed on Windows.

Related

Read image on iPhone from C++

I am working on a C++ library that shall do image processing. My approach now is to pass two strings to C++ from swift. One string is the path to the image and the second is the output directory.
All the time I get that the file does not exist. How can I get the correct path the asset? The image lies in a directory I have created on my own and is called "test.jpg". I also have a "test2.jpg" in Assets. Have not managed to find that either.
Swift Code:
func getGrayImage() -> Image {
if var resourcePath = Bundle.main.url(forResource: "test", withExtension: "jpg") {
let dir = resourcePath.deletingLastPathComponent()
VideoProcessingWrapper().rgb2gray(resourcePath.absoluteString, dir.absoluteString)
}
return Image("test2")
}
C++ Code:
void VideoProcessing::rgb2gray(const std::string& image_path, const std::string& dir) {
std::cout << "C++: " << std::endl;
std::cout << image_path << std::endl;
std::cout << std::endl;
std::cout << dir << std::endl;
std::cout << "In directory: " <<std::endl;
for (const auto& entry : std::filesystem::directory_iterator(dir)) {
std::cout << entry.path() << std::endl;
}
if (std::filesystem::exists(image_path)) {
std::cout << "File exists" << std::endl;
} else {
std::cout << "File does NOT exist" << std::endl;
return;
}
cv::imread(image_path, cv::IMREAD_COLOR);
}
It even crashes when trying to display all files in the directory, stating that the directory does not exist. But what have I then been given from Bundle.main.url-call?
This is the printout from the C++ function using a simulator:
C++:
file:///Users/name/Library/Developer/CoreSimulator/Devices/7F438661-8BF5-4A60-B41F-1D4B7FEC6A8E/data/Containers/Bundle/Application/AB3EFF3E-79A4-469C-A3BF-ABDD31A09E61/TestVideoProcess.app/test.jpg
file:///Users/name/Library/Developer/CoreSimulator/Devices/7F438661-8BF5-4A60-B41F-1D4B7FEC6A8E/data/Containers/Bundle/Application/AB3EFF3E-79A4-469C-A3BF-ABDD31A09E61/TestVideoProcess.app/
I get the same behaviour when running on a real iPhone.
No matter what you do, the bundle directory is inside your application and you have no write access to it.
The problem here is that Foundation framework represents paths with URI's, while C++ standard library filesystem relies on more file-path-specific format which may consist of only the following components:
root-name(optional): identifies the root on a filesystem with multiple roots (such as "C:" or "//myserver"). In case of
ambiguity, the longest sequence of characters that forms a valid
root-name is treated as the root-name. The standard library may define
additional root-names besides the ones understood by the OS API.
root-directory(optional): a directory separator that, if present, marks this path as absolute. If it is missing (and the first
element other than the root name is a file name), then the path is
relative and requires another path as the starting location to resolve
to a file name.
Zero or more of the following:
file-name: sequence of characters that aren't directory separators or preferred directory separators (additional limitations
may be imposed by the OS or file system). This name may identify a
file, a hard link, a symbolic link, or a directory. Two special
file-names are recognized:
dot: the file name consisting of a single dot character . is a directory name that refers to the current directory dot-dot: the file
name consisting of two dot characters .. is a directory name that
refers to the parent directory.
directory-separators: the forward slash character / or the alternative character provided as path::preferred_separator. If this
character is repeated, it is treated as a single directory separator:
/usr///////lib is the same as /usr/lib
A URI consists of quite a lot of different parts:
In this case you need the path part only. One trick you can employ here is to build the path from pathComponents of the URL instance:
if let resourcePath = Bundle.main.url(forResource: "test", withExtension: "jpg") {
let dir = resourcePath.deletingLastPathComponent()
VideoProcessingWrapper().rgb2gray(resourcePath.pathComponents.joined(separator: "/"),
dir.pathComponents.joined(separator: "/"))
}
Alternatively, as suggested by mani in the comments to your question, you can use the path property of the same class, however the documentation says that it also may include parameter string and for some reason it's now deprecated.

How to modify a vts file using another vts file! / C++

I am new to the community here, and I know that my question could be stupid :(
The problem: I want to open a “file1.vts” file and modify the “vtkPoints” using another “file2.vts” file. After that, I want to update “file1.vts” and save it.
my approach: I tried to use the documentation as follow
std::string inputFilename1 = "file1.vts";
std::string inputFilename2 = "file2.vts";
// Read the file 1
vtkNew<vtkXMLStructuredGridReader> reader1;
reader1->SetFileName(inputFilename1.c_str());
reader1->Update();
vtkNew<vtkStructuredGridGeometryFilter> geometryFilter1;
geometryFilter1->SetInputConnection(reader1->GetOutputPort());
geometryFilter1->Update();
// Read the file 2
vtkNew<vtkXMLStructuredGridReader> reader2;
reader2->SetFileName(inputFilename2.c_str());
reader2->Update();
vtkNew<vtkStructuredGridGeometryFilter> geometryFilter2;
geometryFilter2->SetInputConnection(reader2->GetOutputPort());
geometryFilter2->Update();
vtkPolyData* polydata1 = geometryFilter1->GetOutput();
vtkPolyData* polydata2 = geometryFilter2->GetOutput();
polydata1->DeepCopy(polydata2);
The question: What is missing, what is next? How to update file1 only points. Other data should be the same. Only I am taking the points (coordinates).

how to add data in existing array file.i need to read data from text file then ask user whether user want to add data and save it in the same file

first,i read the text file
ifstream inFile; /*open file*/
inFile.open("data.txt");
while(!inFile.eof())
{
if(inFile.eof()==true)
break;
else
{
inFile>>n[count]>>i[count]>>g[count]>>s[count];
count++;
}
}
then i need to ask user whether he want to add more data.
if yes, i user enter the data which is name,ic,gender and state.
then,store it in the same file.
please help me.

How to open/close file at a specific entry in Qt5

I'm making a program that needs to be able to edit files at specific points, specific lines won't do since the line count will change over time. I plan to do this with qt's textEdit menu element.
So for example lets say I have a file that has a comment somewhere in it like so:
#qtread
lots
of
stuff
#qtend
Is there any way I can make a Qt text edit open the file and edit only whatever is between the #qtread and #qtend entries?
(Also I am a total beginner to Qt, so an idiotproof answer would be superb, thanks!)
You can read your file line by line like this. Create a bool indicating whether to read e.g. readEnabled. After you've read the line, check for your tokens:
QFile inputFile(fileName);
QString outputText, startToken = "#qtread", endToken = "#qtend";
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
bool readEnabled = false;
while (!in.atEnd())
{
QString line = in.readLine();
if(line == endToken) // the whole line has to match your comment
readEnabled = false;
if(readEnabled)
outputText.append(line + "\n");
if(line == startToken)
readEnabled = true;
}
inputFile.close();
}
You might want to refine that. In case you want to start reading right after the comment or accept the comment with trailing whitespaces, see QString::left, QString::mid, QString::right and try to put something together.
Edit:
Sorry, I overlooked that you also wanted to save it back. Writing the file is actually a lot harder in this case and I'd do it with the C++ standard library. I encourage you to search for that, post a new question without qt flag.

Replace single quote in QFile file name

I want open a file which contains single quote but I can't open it.
File name example : QFile file("my'file.example")
I've tried with file.fileName().replace("\'", "\\\'") but it's the same result.
You are trying to replace "\'" but it is not on the original string so it will not work. Furthermore, QFile::filename return a copy of the filename property, and any modification (like replace) will be made on the copy. To play with the filename (before open), use
file.setFilename(file.fileName().myModificationOperation())
Have you tried with QFile file("my\'file.example")?
to test your parameter use the static call:
QString filename = "my\'file.example";
bool okay = QFile::exists(filename);