I have a text file which looks like this (Example):
123456789 18-5-2014
985665547 23-12-2016
I've a read/write function in a while(!file.atEnd) construction like this:
while (!file.atEnd())
{
if (date-currentdate<42) {
ui->label->setText(number); //number is the number before the date in the text file
//Here I want the function to delete the current line
}
}
What I want is to delete the line that is just used in the if statement from my text file. By line (as example) I mean 123456789 15-5-2014.But how do I do this?
If the file can be large:
Create a temporary file.
While reading, write into the temp file all the lines except the one that you want to delete.
Delete the original file.
Rename the temporary file as the original file.
If you know that the file is always small, you can optionally do as follows:
Read in memory all the lines, except the one to be deleted.
Rewrite the file using the strings that you now have in memory.
Related
I have a json file that I want to read in as a string, the problem I'm having is the read() function reads the first line like so:
json file contents:
{
"structure": [
{
"thing1": "1441",
"thing2": 1234,
"thing3": "2200.685715",
"thing4": "1793.190430",
}
}
what gets read is just the leading { (the first record/line), so when I print out I get this at the beginning and a whole bunch of blank spaces. How do I read in all the records/lines as a single pre-allocated string?
my code:
open (unit=11, status='scratch', access='stream', form='formatted')
call jsonHandler%print(outputJson, 11)
rewind(11)
inquire(11, SIZE=file_size)
! allocate the output based on the size of the file
allocate(character(len=file_size)::output)
read(11, *) output
close(11)
notes on the code:
I am using Jacob Williams' json-fortran library and I am aware that there is a serialize() function, however this is a very slow algorithm, because of the appends, this is the solution I came up with to have a pre-allocated string, and therefore will be fast
I am writing to a temporary file so normally this file object would not show up on the file system, the goal of this is to use a file objects powerful functionality without writing to the file system
This is more of a generalization of how to read multi-record data into a string, I am simply using json as an example
Do not use form="formatted". You do not want the formatting in the file to be interpretted in any way when reading the file content.
Here is the code I'm having a trouble with, I have a .txt file that contains a list of users and their passwords using this format: user;password.
I need to search for a user in the file and then delete the line which contains this user.
void deleteauser()
{
string user;
cout<<"Which user do you wish to delete?";
cin>>user;
string line;
string delimiter=";";
string token,token1;
ifstream infile;
infile.open("users.txt",ios::in);
while (getline(infile,line,'\n'))
{
token = line.substr(0, line.find(delimiter));
token1=line.substr(token.length(), line.find('\n'));
if(token==user)
{
//here i need to delete the line of the user that has been found
}
}
infile.close();
}
Read the input file, line by line, writing to a temporary file. When you find lines you don't want then just don't write them to the temporary file. When done rename the temporary file as the real file.
To edit a file you have 2 options:
Read in every line and write out those you want to keep
Seek to the part of the file you want deleted and replace the text with spaces (or similar)
You have the first half pretty much done - just write out what you read to a temporary file and delete/rename to make it the original.
For the second option, you can write to the input file at that point if you use an iofstream (be aware of buffering issues). The better option is to use seekp or seekg to get to the right point before overwriting the file.
I'm writing a program in C++ which writes lines to a text file. In certain circumstances I want it to delete a bunch of recently added lines. The code looks like this:
file << "Some line." << endl; // *
// lots of lines might be written to file here
if (condition2)
// delete all the lines written to file since * including line "Some line."
How do I do it?
Have a look at the seekp and tellp methods of ostream.
I assume your file is some sort of fstream.
This is just a pseudo code, since you too didn't give enough info
if (condition2)
{
/*
1. Read back the file lines till "Some line" marker
in say, std::vector<std::string>
2. Discard other lines after that.
3. Write the contents of std::vector into the file
*/
}
I was looking for an easy way to write something into the first line of an already existing textfile. I tried using ofstream like this:
ofstream textFileWriter("Data/...txt");
if (textFileWriter.is_open())
{
textFileWriter << "HEADER: stuffstuff";
}
But it would delete everything which used to be in that file, even though the ofstream wasn't constructed with std::ofstream::trunc. I cannot use std::ofstream::app, since it is important to write into the first line.
Copying the whole textfile into a vector which has the line already and then writing it back would be my last option, but something I would really like to avoid, since the textfiles are quite large.
You can't simply "append" to the beginning of a file.
The common solution is to open a new (temporary) file, write your new header, write the rest of the original file to the temporary file, and then "rename" (using the OS system calls) the temporary file as the original file.
Or as you say in your question, read the original file into an in-memory buffer (e.g. a vector) and do the modification in that buffer, and then write the buffer to the file.
How can i write some text to a file's particular line using <fstream> header? Is there any function to do that? Thank you.
You can't really do that because the line you write might be longer than then one that exists. So you would clobber a line or have to rewrite the whole file.
If the lines are all exactly the same length, you could do binary writing.
[Edit: the following line was mistakenly added, it's for .NET only]
If you can, use File.ReadAllLines and File.WriteAllLines.
if you want to insert text in line 5 :
1- copy the content of the line 5 to the end of the file on a new file of to a buffer.
2- then write your line. (ater putting cursor in beinnin of line 5)
3- then copy back the lines from the other file.
or, more complicated (not using buffer): (same algorithm as insertion in an array)
you can move all lines atfer the line you want to overwrite to get the eact spae oryour line. then write your line.
for example, you want to write 20 char in line 5.
start by writing 21 char at the end of the file. (if there is a functionthe move charsby 21 characers,i would be easier and perfect).
then put a loop which replace each char with the char in position -21. until un arrive to line 5.
then write your line in line 5.
is that ok ?
The code will look like this:
InputFile.open();
tmpFile.open();
while(InputFile.readline())
{
if (this is where you want the new line)
{
tmpFile.write(newLine);
if(Want to keep the original line)
{
tmpFile.writeLine(oldLine);
}
}
else
{
tmpFile.writeLine(oldLine);
}
}
InputFile.close();
tmpFile.close();
unlink(InputFile);
move tmpFile to InputFile.