Deleting character from file at the current position of the file object - c++

Please look at this minimal working example:
#include <iostream>
#include <fostream>
using namespace std;
int main()
{
fstream file;
file.open("asd.txt", ios_base::out);
file << "this is a sentence!" << endl;
///is it possible at this point to delete the last character, the exclamation mark, from the file asd.txt using the object "file"?
file.close();
return 0;
}
I am writing a sentence to a file asd.txt using a file object file. Is it possible to delete the character ! from asd.txt using file?

You can use a std::ofstream with seekp and write to overwrite the ! with a space ( I tried just deleting the ! or replacing it by '\0' but i can't seem to get that to work )
#include
#include
int main()
{
std::ofstream file;
file.open("asd.txt");
///is it possible at this point to delete the last character, the exclamation mark, from the file asd.txt using the object "file"?
//Yes!
file.write("this is a sentence!", 19); // writes 19 chars to the file
long pos = file.tellp(); // gets the current position of the buffer ( in this case 19)
file.seekp(pos - 1); // subtracts one from the buffer position ( now 18 )
// writes a space that is one char at the current position of the file ( 18, which overwrites the '!' that is in pos 19)
file.write("", 1);
file.close();
return 0;
}
There are many alternatives though, you can always just close the file, reopen the file with std::ios_base::trunc which will clear all the content in the file, Then you can write the string again only up until before the !
file.write("this is a sentence!", 18);
You can also store the string in a std::string and call std::string::pop_back() to remove the last character and then write that into the file after clearing the file contents. Just store the stream into a std::ostringstream and get the string from the ostringstream and then pop_back() the !.
It really depends on your use case, if you want to give more detail I'd be free to help you out a little more.

Related

C++ Im trying to stream a file, and replace the first letter of every line streamed. It doesn't seem to be working as expected

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
void add1(std::fstream& files)
{
char c;
int i=0;
int j=0;
int k=0;
int con=0;
string word;
while(files.get(c)&&!files.eof())
{
i++;
j++;
if(c=='\n'||(con>=1&&isspace(c)))
{
con++;
if(con>=2)
{
break;
}
else
{
cout<<j<<"\/"<<i<<endl;
files.seekp(i-j,files.beg);
files.write("h",1);
files.seekg(i);
*seekg ends the loops I tried fstream::clear. I think it would work perfect if seekg worked.
+ without seekg it works but only for 3 lines then its off.
j=0;
word="";
}
}
else
{
con=0;
word=word+c;
}
}
}
*The goal is to be able stream the file, and replace the first letter of every line in the file while streaming.*
You seam to have a logical error and make thinks overcomplicated.
I do not knwow, what you want to do with your variable "word". It is consumed nowhere. So, I will ignore it.
Then you are playing with read and write pointers. That is not necessary. You only need to manipulate the write pointer.
Then, you want to "stream" something. This I do not fully understand. Maybe it means, that you want to write always something to the stream, even, if you do not replace anything. This would in my understanding only make sense, if you would have 2 streams. But in that case it would be brutally simple and no further thinking necessary.
If we use the same stream and do not want to replace a character, then this is already there, existing, and maybe not overwritten by the same character again.
So, if there is nothing to replace, then we will write nothing . . .
Also, and that is very important, we do no replacement operation, if we have an empty line, because then there is nothing to replace. There is now first character in an empty line.
And, most important, we cannot add characters to the same fstream. In that case we would have to shift the rest of the file one to the right. Therefore. 2 streams are always better. Then, this problem would not occur.
So, what's the logic.
Algorithm:
We always look at the previuosly read character. If that was a '\n' and the current character is not, then we are now in a new line and can replace the first character.
That is all.
It will take also into account, if a '\n' is encoded with 2 characters (for example \r\n). It will always work.
And, it is easy to implement. 10 lines of code.
Please see:
#include <iostream>
#include <fstream>
#include <string>
constexpr char ReplacementCharacter{ 'h' };
void replaceFirstCharacterOfLine(std::fstream& fileStream) {
// Here we stor the previously read character. In the beginning, a file always starts
// with a newline. Therefore we pretend that the last read character is a newline
char previouslyReadCharacter{'\n'};
// Here we store the current read character
char currentCharacter{};
// Get characters from file as lon as there are characters, so, until eof
while (fileStream.get(currentCharacter)) {
// No check, if a new line has started. We ignore empty lines!
if ((previouslyReadCharacter == '\n') && (currentCharacter != '\n')) {
// So last charcter was a newline and this is different. So, we are in a new, none empty line
// Set replacement character
currentCharacter = ReplacementCharacter;
// Go one back with the write pointer
fileStream.seekp(-1, std::ios_base::cur);
// Write (an with taht increment file pointer again)
fileStream.put(currentCharacter);
// Write to file
fileStream.flush();
}
else {
// Do not replace the first charcater. So nothing to be done here
}
// Now, set the previouslyReadCharacter to the just read currentCharacter
previouslyReadCharacter = currentCharacter;
}
}
int main() {
const std::string filename{"r:\\replace.txt"};
// Open file
std::fstream fileStream{ filename };
// Check, if file could be opened
if (fileStream)
replaceFirstCharacterOfLine(fileStream);
else
std::cerr << "\n\n*** Error: Could not open file '" << filename << "'\n\n";
return 0;
}

Read multiline file and using rdbuf()

I've a multiline file, every line a string.
Example of code.txt:
AAAAA
BB33A
C544W
I have to put some code contained in another file before every string. I'm using this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//the file to include before every string
ifstream one("1.txt");
//final output file
ofstream final;
final.open ("final.txt", ofstream::app);
//string file
string line;
ifstream file("code.txt");
while (getline(file,line))
{
final<<one.rdbuf();
final<<line;
}
}
Now, this doesn't work, it works only for the first line of code.txt. What is wrong?
final<<one.rdbuf() works for only the first line because once you stream out the rdbuf the first time, its read pointer is sitting at the end of the 1.txt file data. There is no data left for it to read on subsequent streaming. You would have to reset the one stream back to the beginning of its data on each loop iteration, eg:
while (getline(file,line))
{
final<<one.rdbuf();
final<<line;
one.seekp(0); // <-- add this
}
Otherwise, do as #Check suggests. Read the content of the 1.txt file into memory one time, and then stream that out on each loop iteration, instead of re-reading the file on each iteration.
I would change your final<<one.rdbuf();.
You can use this:
//the file to include before every string
ifstream one("1.txt");
std::string first;
if (one.is_open())
{
// file length and reserve the memory.
one.seekg(0, std::ios::end);
first.reserve(static_cast<unsigned int>(one.tellg()));
one.seekg(0, std::ios::beg);
first.assign((std::istreambuf_iterator<char>(one)),
(std::istreambuf_iterator<char>()));
one.close();
}
//final output file
ofstream final;
final.open ("final.txt", ofstream::app);
//string file
string line;
ifstream file("code.txt");
while (getline(file,line))
{
final<<first;
final<<line;
}
Maybe you want to check this https://stackoverflow.com/a/8737787/3065110

Decoding / Encloding Text File using Stack Library - Can't Encode Large Files C++

I am working on a program that can encode and then decode text in C++. I am using the stack library. The way the program works is that it first asks you for a cypher key, which you put in manually. It then asks for the file name, which is a text file. If it is a normal txt file, it encodes the message to a new file and adds a .iia files extension. If the text file already has a .iia file extension, then it decodes the message, as long as the cypher key is the same as the one used to encode it.
My program does encode and decode, but how many characters it decodes is determined by temp.size() % cypher.length() that is in the while loop in the readFileEncode() function. I think this is what is keeping the entire file from being encoded and then decoded correctly. Another words, the ending file after it has been decoded from say "example.txt.iia" back to "example.txt" is missing a large portion of the text from the original "example.txt" file. I tried just cypher.length() but of course that does not encode or decode anything then. The entire process is determined by that argument for the decoding and encoding.
I cannot seem to find out the exact logic for this to encode and decode all the characters in any size file. Here is the following code for the function that does the decoding and encoding:
EDIT: Using WhozCraig's code that he edited for me:
void readFileEncode(string fileName, stack<char> &text, string cypher)
{
std::ifstream file(fileName, std::ios::in|std::ios::binary);
stack<char> temp;
char ch;
while (file.get(ch))
temp.push(ch ^ cypher[temp.size() % cypher.length()]);
while (!temp.empty())
{
text.push(temp.top());
temp.pop();
}
}
EDIT: A stack is required. I am going to implement my own stack class, but I am trying to get this to work first with the stack library. Also, if there is a better way of implementing this, please let me know. Otherwise, I believe that there is not much wrong with this except to get it to go through the loop to encode and decode the entire file. I am just unsure as to why it stops at, say 20 characters sometimes, or ten characters. I know it has to do with how long the cypher is too, so I believe it is in the % (mod). Just not sure how to rewrite.
EDIT: Ok, tried WhozCraig's solution and I don't get the desired output, so the error now must be in my main. Here is my code for the main:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <stack>
using namespace std;
void readFileEncode(string fileName, stack<char> &text, string cypher);
int main()
{
stack<char> text; // allows me to use stack from standard library
string cypher;
string inputFileName;
string outputFileName;
int position;
cout << "Enter a cypher code" << endl;
cin >> cypher;
cout << "Enter the name of the input file" << endl;
cin >> inputFileName;
position = inputFileName.find(".iia");//checks to see if the input file has the iia extension
if (position > 1){
outputFileName = inputFileName;
outputFileName.erase(position, position + 3);// if input file has the .iia extension it is erased
}
else
//outputFileName.erase(position, position + 3);// remove the .txt extension and
outputFileName = inputFileName + ".iia";// add the .iia extension to file if it does not have it
cout << "Here is the new name of the inputfile " << outputFileName << endl; // shows you that it did actually put the .iia on or erase it depending on the situation
system("pause");
readFileEncode(inputFileName, text, cypher); //calls function
std::ofstream file(outputFileName); // calling function
while (text.size()){// goes through text file
file << text.top();
text.pop(); //clears pop
}
system("pause");
}
Basically, I am reading .txt file to encrypt and then put a .iia file extension on the filename. Then I go back through, enter the file back with the .iia extension to decode it back. When I decode it back it is gibberish after about the first ten words.
#WhozCraig Does it matter what white space, newlines, or punctuation is in the file? Maybe with the full solution here you can direct me at what is wrong.
just for information: never read file char by char it will take you hours to finish 100Mb.
read at least 512 byte(in my case i read directly 1 or 2Mb ==> store in char * and then process).
If I understand what you're trying to do correctly, you want the entire file rotationally XOR'd with the chars in the cipher key. If that is the case, you can probably address your immediate error by simply doing this:
void readFileEncode(string fileName, stack<char> &text, string cypher)
{
std::ifstream file(fileName, std::ios::in|std::ios::binary);
stack<char> temp;
char ch;
while (file.get(ch))
temp.push(ch ^ cypher[temp.size() % cypher.length()]);
while (!temp.empty())
{
text.push(temp.top());
temp.pop();
}
}
The most notable changes are
Opening the file in binary-mode using std::ios::in|std::ios::binary for the open-mode. this will eliminate the need to invoke the noskipws manipulator (which is usually a function call) for every character extracted.
Using file.get(ch) to extract the next character. The member will pull the next char form the file buffer directly if one is available, otherwise load the next buffer and try again.
Alternative
A character by character approach is going to be expensive any way you slice it. That this is going through a stack<>, which will be backed by a vector or deque isn't going to do you any favors. That it is going through two of them just compounds the agony. You may as well load the whole file in one shot, compute all the XOR's directly, then push them on to you stack via a reverse iterator:
void readFileEncode
(
const std::string& fileName,
std::stack<char> &text,
const std::string& cypher
)
{
std::ifstream file(fileName, std::ios::in|std::ios::binary);
// retrieve file size
file.seekg(0, std::ios::end);
std::istream::pos_type pos = file.tellg();
file.seekg(0, std::ios::beg);
// early exit on zero-length file.
if (pos == 0)
return;
// make space for a full read
std::vector<char> temp;
temp.resize(static_cast<size_t>(pos));
file.read(temp.data(), pos);
size_t c_len = cypher.length();
for (size_t i=0; i<pos; ++i)
temp[i] ^= cypher[i % c_len];
for (auto it=temp.rbegin(); it!=temp.rend(); ++it)
text.push(*it);
}
You still get your stack on the caller-side, but I think you'll be considerably happier with the performance.

how to replace a line with another/ c++ code

I am working on ubuntu. I have a file called test.txt. I would like to replace the second line with another line. How can I do that? I don't want to create a new file and delete the first one.
I would like to specify that the lenght of the new line is the same with the length of the ond one
Try something like:
#include <fstream>
#include <string>
int main() {
const int lineToReplace = 14;
std::fstream file("myfile.txt", std::ios::in | std::ios::out);
char line[255];
for (int i=0; i<lineToReplace-1; ++i)
file.getline(line, 255); // This already skips the newline
file.tellg();
file << "Your contents here" << std::endl;
file.close();
return 0;
}
Note that line can hold up to 254 bytes (plus the null terminator), so if your line takes more than that, adjust accordingly.
If the file is small enough you can read it into memory, do whatever modifications you want on the in-memory copy, and the write if back out.
Edit Code as requested:
// A vector to store all lines
std::vector<std::string> lines;
// The input file
std::ifstream is("test.txt")
// Get all lines into the vector
std::string line;
while (std::getline(is, line))
lines.push_back(line);
// Close the input file
is.close();
// All of the file is now in memory, each line a single entry in the vector
// "lines". The items in the vector can now be modified as you please.
// Replace the second line with something else
lines[1] = "Something else";
// Open output file
std::ofstream os("test.txt");
// Write all lines to the file
for(const auto& l : lines)
os << l << '\n';
// All done, close output file
os.close();
This is Python, but it's significantly more readable and terse for this purpose:
f = open('text.txt', 'w+') # open for read/write
g = tempfile.TemporaryFile('w+') # temp file to build replacement data
g.write(next(f)) # add the first line
next(f) # discard the second line
g.write(second_line) # add this one instead
g.writelines(f) # copy the rest of the file
f.seek(0) # go back to the start
g.seek(0) # start of the tempfile
f.writelines(g) # copy the file data over
f.truncate() # drop the rest of the file
You could also use shutil.copyfileobj instead of writelines to do block copying between the files.
Here's how I would do it, without a hard limit on the line length:
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file("test.txt",std::ios::in|std::ios::out);
string line;
string line_new="LINE2";
// Skip the first line, file pointer points to beginning of second line now.
getline(file,line);
fstream::pos_type pos=file.tellg();
// Read the second line.
getline(file,line);
if (line.length()==line_new.length()) {
// Go back to start of second line and replace it.
file.seekp(pos);
file << line_new;
}
return 0;
}

calculate size of file

i have following program to calculate size of file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string line;
ifstream myfile ("C:\\Users\\7\\Desktop\\example\\text.txt",ios::in | ios::out |ios::binary);
if (!myfile){
cout<<"cannot open file";
exit (1);
}
while (!myfile.eof()){
getline(myfile,line);
cout<<line<<endl;
}
long l,m;
l=myfile.tellg();
myfile.seekg(0,ios::end);
m=myfile.tellg();
cout<<"size of text file is:";
cout<<(m-l)<<"bytes"<<endl;
myfile.close();
return 0;
}
for make more clarify in text.txt file i have wrote some copy of information from this site http://en.wikipedia.org/wiki/List_of_algorithms
but it shows me 0 bytes and why? what is wrong?
You are subtracting the current-file-position (l) from the end-of-file position (m) to get the size. This will work as you expect if the current-file-position is at the start of the file, but as you have just read the entire contents of the file, (l) is "starting" at the end of the file.
Just use the value of (m) rather than (m-l), as files always start at 0.
(Alternatively, before using ftell to get (l), use fseek to move to the start of the file)
#include <stdio.h>
int main(int argc, char** argv) {
FILE *f = fopen("x.txt", "r");
fseek(f, 0, SEEK_END);
printf("%ld\n", ftell(f));
fclose(f);
return 0;
}
while (!myfile.eof()){
getline(myfile,line);
cout<<line<<endl;
}
Reads the whole file, so the get pointer is already at the end of the file. myfile.seekg(0,ios::end) will not move it, so m-l will return 0.
Ok, another dopey question, why not use FileInfo('file name') and use the length value stored?
It appears your while loop reads the file completely. Then you capture the position in l. Then you seek to m, which is also the position of l. Then you print their difference.
Did I miss something here??
Once you read to the end of the file, its fail bit gets set, and until you reset that, nothing else you do with the file will really accomplish much. Your loop for copying the file is also wrong (like virtually all that start with while (!file.eof())).
I'd try something like this:
std::string line;
while (getline(myfile, line))
std::cout << line << "\n";
// allow further use of the stream object to work:
myfile.clear();
// since we already read to the end, the current position is the length:
length = myfile.tellg();
Just a warning - be aware that some OS's provide for sparse files. If you open a file, write a byte, seek to start-of-file + 1,000,000,000, write another byte, then close the file, the intermediate bytes may not actually be written to disk. So, which is the size of the file? The two blocks actually allocated on disk, or the 1,000,000,000 bytes that is the offset of the final byte? Either could be the correct answer, depending upon what you are using the result for,