replace and write to file c++ - c++

I want write code to find words in a file and replace words.
I open file, next I find word. I have a problem with replace words.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string contain_of_file,a="car";
string::size_type position;
ifstream NewFile;
NewFile.open("plik1.txt");
while(NewFile.good())
{
getline(NewFile, contain_of_file);
position=contain_of_file.find("Zuzia");
if(position!=string::npos)
{
NewFile<<contain_of_file.replace(position,5, a );
}
}
NewFile.close();
cin.get();
return 0;
}
How can I improve my code?

lose the using namespace std;
don't declare the variables before needed;
I think the English word you were looking for was content -- but I am not an English-native speaker;
getline already returns NewFile.good() in boolean context;
No need to close NewFile explicitly;
I would change the casing on the NewFile variable;
I don't think you can write to an ifstream, and you ought to manage how you are going to replace the contents of the file...
My version would be like:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
int main() {
std::rename("plik1.txt", "plik1.txt~");
std::ifstream old_file("plik1.txt~");
std::ofstream new_file("plik1.txt");
for( std::string contents_of_file; std::getline(old_file, contents_of_file); ) {
std::string::size_type position = contents_of_file.find("Zuzia");
if( position != std::string::npos )
contents_of_file = contents_of_file.replace(position, 5, "car");
new_file << contents_of_file << '\n';
}
return 0;
}

There are at least two issues with your code:
1. Overwriting text in a file.
2. Writing to an ifstream (the i is for input, not output).
The File object
Imagine a file as many little boxes that contain characters. The boxes are glued front to back in an endless line.
You can take letters out of boxes and put into other boxes, but since they are glued, you can't put new boxes between existing boxes.
Replacing Text
You can replace text in a file as long as the replacement text is the same length as the original text. If the text is too long, you overwrite existing text. If the replacement text is shorter, you have residual text in the file. Not good in either method.
To replace (overwrite) the text, open the file as fstream and use the ios::in and ios::out modes.
Input versus Output
The common technique for replacing text is to open the original file for *i*nput and a new file as *o*utput.
Copy the existing data, up to your target text, to the new file.
Copy the replacement text to the new file.
Copy any remaining text to the new file.
Close all files.

Related

how to select a sentence from a text with c++

i have a question, how to separate one file .txt into 3 files based on the keywords using c++. so each keyword has it's own sentence. so each new sub file contains keywords with their respective sentences. i have tried to show it on console, and it works, but i can't separate the text by it's keywords.
so i have a file. every sentence in this file
so I have a file. There are many sentences here. So, every sentence starts with the words error, warning, and information. how to separate each sentence starting with each of these words, and make them 3 separate files
can you help me please?
i've tried this code, and its failed.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream myFile;
string data,output,buffer, line;
bool isData = false;
myFile.open("try.txt");
while(getline(myFile, buffer)){
if (buffer == "Error"){
getline(myFile,buffer);
cout<< buffer <<endl;
}
}
cin.get();
return 0;
}

How to write from file to string

I am new to C++ and I'm having trouble understanding how to import text from a file. I have a .txt file that I am inputting from and I want to put all of the text from that file into a string. To read the text file I am using the following code:
ifstream textFile("information.txt");
Which is just reading a text file name information. I made a string named text and initialized it to "". My problem is with the following code which I am trying to use to put the text from the .txt file onto the string:
while (textFile >> text)
text += textFile;
I am clearly doing something wrong, although I'm not sure what it is.
while (textFile >> text) won't preserve spaces. If you want to keep the spaces in your string you should use other functions like textFile.get()
Example:
#include <iostream>
#include <string>
#include <fstream>
int main(){
std::ifstream textFile("information.txt");
std::string text,tmp;
while(true){
tmp=textFile.get();
if(textFile.eof()){ break;}
text+=tmp;
}
std::cout<<text;
return(0);}
while (textFile >> text) text += textFile;
You're trying to add the file to a string, which I assume will be a compiler error.
If you want to do it your way, you'll need two strings, e.g.
string text;
string tmp;
while(textFile >> tmp) text += tmp;
Note that this may omit spaces, so you may need to manually re-add them.

Read from text file C++ (fname, lname, class, seat number), store and verify

I have a text file of the classlook like this:
FName LName Class SeatNum
FName2 LName2 Class2 SeatNum2
...
and the list goes on.
How to read lines of strings and store them into different variables?
How to combine Class & SeatNum to be an ID (3D-20)?
How to verify for every input name and ID has to be matched?
For example, input > FName LName Class2-SeatNum2 is wrong, please try again.
Your help is greatly appreciated.
Thanks!
Just a note for next time - because you didn't detail the problem, it was hard to figure out what you mean. Anyhow:
in order to do what you asked you need to:
a) read the data from the file
b) split the data based on the character which is between the cells.
In C++, The split string algorithm is in boost - if you dont know what that is, make sure you take a look in here: http://www.boost.org/
Soltion:
I`m modifying various cPlusPlus guides here to fit your purpouse:
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
using namespace std;
vector<string> getData (string filePath) {
vector<string> Cells; // In the end, here we will store each cell's content.
stringstream fileContent(""); // This is a string stream, which will store the database as a string.
ofstream myfile; // the file which the database is in
myfile.open (filePath); // Opening the file
while ( getline (myfile,line) ) // Reading it until it's over
{
fileContent << line; // adding each line to the string
}
split(Cells, fileContent.str(), is_any_of(" "));// Here, insert the char which seperates the cells from each other.
myfile.close()
return Cells; // returning the split string.
}
Hope i helped :)

C++ Fstream to replace specific line?

okay i'm stumped on how to do this. I managed to get to the line I want to replace but i don't know how to replace it.
say a file called file.txt containts this:
1
2
3
4
5
and I want to replace line 3 so that it says 4 instead of 3. How can I do this?
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
fstream file;
string line;
int main(){
file.open("file.txt");
for(int i=0;i<2;i++){
getline(file,line);
}
getline(file,line);
//how can i replace?
}
Assuming you have opened a file in read/write mode you can switch between reading and writing by seeking, including seeking to the current position. Note, however, that written characters overwrite the existing characters, i.e., the don't insert new characters. For example, this could look like this:
std::string line;
while (std::getline(file, line) && line != end) {
}
file. seekp(-std::ios::off_type(line.size()) - 1, std::ios_base::cur);
file << 'x';
Even if you are at the right location seeking is needed to put the stream into an unbound state. Trying to switch between reading and writing without seeking causes undefined behavior.
The usual approach is to read from one file while writing to another. That way you can replace whatever you want, without having to worry about whether it's the same size as the data it's replacing.

New to <dirent.h>, trying to access data in a directory

I've never used dirent.h before. I was using istringstream to read through text files (singular), but have needed to try to revise the program to read in multiple text files in a directory. This is where I tried implementing dirent, but it's not working.
Maybe I can't use it with the stringstream? Please advise.
I've taken out the fluffy stuff that I'm doing with the words for readability. This was working perfectly for one file, until I added the dirent.h stuff.
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream> // for istringstream
#include <fstream>
#include <stdio.h>
#include <dirent.h>
void main(){
string fileName;
istringstream strLine;
const string Punctuation = "-,.;:?\"'!##$%^&*[]{}|";
const char *commonWords[] = {"AND","IS","OR","ARE","THE","A","AN",""};
string line, word;
int currentLine = 0;
int hashValue = 0;
//// these variables were added to new code //////
struct dirent *pent = NULL;
DIR *pdir = NULL; // pointer to the directory
pdir = opendir("documents");
//////////////////////////////////////////////////
while(pent = readdir(pdir)){
// read in values line by line, then word by word
while(getline(cin,line)){
++currentLine;
strLine.clear();
strLine.str(line);
while(strLine >> word){
// insert the words into a table
}
} // end getline
//print the words in the table
closedir(pdir);
}
You should be using int main() and not void main().
You should be error checking the call to opendir().
You will need to open a file instead of using cin to read the contents of the file. And, of course, you will need to ensure that it is closed appropriately (which might be by doing nothing and letting a destructor do its stuff).
Note that the file name will be a combination of the directory name ("documents") and the file name returned by readdir().
Note too that you should probably check for directories (or, at least, for "." and "..", the current and parent directories).
The book "Ruminations on C++" by Andrew Koenig and Barbara Moo has a chapter that discusses how to wrap the opendir() family of functions in C++ to make them behave better for a C++ program.
Heather asks:
What do I put in getline() instead of cin?
The code at the moment reads from standard input, aka cin at the moment. That means that if you launch your program with ./a.out < program.cpp, it will read your program.cpp file, regardless of what it finds in the directory. So, you need to create a new input file stream based on the file you've found with readdir():
while (pent = readdir(pdir))
{
...create name from "documents" and pent->d_name
...check that name is not a directory
...open the file for reading (only) and check that it succeeded
...use a variable such as fin for the file stream
// read in values line by line, then word by word
while (getline(fin, line))
{
...processing of lines as before...
}
}
You probably can get away with just opening the directories since the first read operation (via getline()) will fail (but you should probably arrange to skip the . and .. directory entries based on their name). If fin is a local variable in the loop, then when the outer loop cycles around, fin will be destroyed, which should close the file.