Here is the code I have so far.
What I need to do is read from two different text files, Matrix A and Matrix B.
I can do this however for each text file matrix I read it only comes up with
1 0 0
(so basically the first line) where the whole text file for Matrix A is in fact
1 0 0
2 0 0
3 0 0
so does anybody know how I can do this?
Thanks!
#include <iostream> //declaring variables
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string code(string& line);
int main()
{
ofstream outf;
ifstream myfile;
string infile;
string line;
string outfile;
cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
cin >> infile; //prompts user for input file
if (infile == "A.txt")
{ //read whats in it and write to screen
myfile.open("A.txt");
cout << endl;
getline (myfile, line);
cout << line << endl;
}
else
if (infile == "B.txt")
{
myfile.open("B.txt");
cout << endl;
getline (myfile, line);
cout << line << endl;
}
else
{
cout << "Unable to open file." << endl;
}
//{
//while("Choose next operation");
//}
return 0;
}
Well, getline obviously gets one line.
You should read line by line until the end of file, and you can achieve that with, for example:
while (getline(myfile, line))
out << line << endl;
This means: while there is a line to get from myfile, write that line to the output stream.
You are reading only once, so this is not a miracle. You will need to use a while or for loop for continous reading. You would be writing something like this:
while (getline (myfile, line))
cout << line << endl;
This would be the whole code to write:
#include <iostream> //declaring variables
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string code(string& line);
int main()
{
ofstream outf;
ifstream myfile;
string infile;
string line;
string outfile;
cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
cin >> infile; //prompts user for input file
if (infile == "A.txt")
{ //read whats in it and write to screen
myfile.open("A.txt");
cout << endl;
while (getline (myfile, line))
cout << line << endl;
}
else
if (infile == "B.txt")
{
myfile.open("B.txt");
cout << endl;
while (getline (myfile, line))
cout << line << endl;
}
else
{
cout << "Unable to open file." << endl;
}
//{
//while("Choose next operation");
//}
return 0;
}
Using getline is the easiest way:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void read_file_line_by_line(){
ifstream file;
string line;
file.open("path_to_file");
while (getline (file, line))
cout << line << endl;
}
int main(){
read_file_line_by_line();
return 0;
}
Related
I need to delete all the occurencies of a string in a file.
I receive the text as a string and erase every occurencies.
After I deleted all the occurencies i don't know how to save the string back to the file.
I've tried to close the file and wit ofstream to write in it but it didn't work.
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::string file_contents = "";
std::ifstream myfile ("text.txt");
char ch;
if (myfile.is_open())
{
// READ FILE CONTENTS AS STRING
while ( myfile >> std::noskipws >> ch)
{
file_contents += ch;
}
// DISPLAY STRING
std::cout << file_contents << '\n';
// GET WORD TO BE DELETED
std::string word;
std::cout << "Please enter word to be deleted: ";
std::cin >> word;
std::string::size_type found;
std::string new_text;
//DELETE WORD FROM STRING
bool ok=0;
do{
found = file_contents.find(word);
ok=1;
if (found!=std::string::npos)
{
std::cout << word << " found at: " << found << '\n';
file_contents.erase(found, word.length());
std::cout << file_contents << '\n';
}
else
ok==0;
new_text=file_contents;
}while(ok==1);
myfile.close();
}
else std::cout << "Unable to open file";
return 0;
}
Okay so you must close ifstream instance before proceeding to write to the file again.
After closing the file, modify the content and then open the same file for write using ofstream and simply write the content.
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string file_contents = "";
std::ifstream myfile("text.txt");
char ch;
if (myfile.is_open())
{
// READ FILE CONTENTS AS STRING
while (myfile >> std::noskipws >> ch)
{
file_contents += ch;
}
myfile.close();
}
else {
std::cout << "Unable to open file";
return -1; // no need to continue if can't read it
}
// DISPLAY STRING
std::cout << file_contents << '\n';
// GET WORD TO BE DELETED
std::string word;
std::cout << "Please enter word to be deleted: ";
std::cin >> word;
//DELETE WORD FROM STRING
size_t found;
while ((found = file_contents.find(word)) != file_contents.npos)
{
std::cout << word << " found at: " << found << '\n';
file_contents.erase(found, word.length());
std::cout << file_contents << '\n';
}
// this will open in text mode and will replace all existing content
std::ofstream out("text.txt");
if (out.is_open()) {
out << file_contents;
out.close();
}
else {
std::cout << "Unable to open file for writing.\n";
return -2; // for failure to open for write
}
return 0;
}
Note: the loop you had went infinitely when I tried to execute it, I had to change it to the code shown above. Also, new_text is completely unnecessary, why have it?
The simpliest way is first to open as input stream. When finish open as output stream to write. It is not the only way you can do that.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
std::string file_contents = "";
{
std::ifstream myfile ("text.txt");
if (! myfile.is_open())
{
else std::cout << "Unable to open file";
return 1;
}
// READ FILE CONTENTS AS STRING
char ch;
while ( myfile >> std::noskipws >> ch) file_contents += ch;
myfile.close();
}
{
std::string word;
std::cin >> word; // GET WORD TO BE DELETED
std::string::size_type found;
while((found = file_contents.find(word))!=std::string::npos)
file_contents.erase(found, word.length());
std::ofstream myfile ("text.txt");
myfile << file_contents<< std::flush;
myfile.close();
}
return 0;
}
I am doing an ATM exercise. In my database or ".txt" file I have the basic information.
0123456789 John Doe 0123 9000
The only thing I can find in the internet, reading a file in C++ is using getline();. It reads the file and stores it in a variable string. I have values like integer and float which I have to use.
How do I store the values in my database to a different data type not
just in one string?
Or is there a way of cutting the string and store the the different values in a float or integer?
Is there another way of reading? I am just new to C++ programming.
In C++,you can read file by "file stream":
#include <iostream>
#include <string>
using namespace std;
int main(){
string filename="test.txt";
ifstream fin(filename);
while (!fin.fail()){//read until end of file
int a,d,e;
string b,c;
fin>>a>>b>>c>>d>>e;//That's a line
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" \n";//show it on command line
}
fin.close();
return 0;
}
You can use the libary #include <fstream>. Here's a code of what it will look like in your case. In "Text.txt" I copied and pasted your inputfile.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int firstNumber, thirdNumber, fourthNumber;
string firstString, secondString;
//Reading from a file
ifstream file;
file.open("Text.txt", ios::in);
if (file.is_open()) {
while (!file.eof()) {
file >> firstNumber >> firstString >> secondString >> thirdNumber >> fourthNumber;
cout << firstNumber << " " << firstString << " " << secondString << " " << thirdNumber << " " << fourthNumber << endl;
}
file.close();
}
else {
cout << "File did not open";
}
//Outputing to a file
ofstream file2;
file2.open("SecondText.txt", ios::out); // ios::out instead of ios::in
if (file2.is_open()) {
file2 << firstNumber << " " << firstString << " " << secondString << " " << thirdNumber << " " << fourthNumber;
file2.close();
}
else {
cout << "Problem with SecondText.txt";
}
return 0;
}
This code basically states that while open, and not at the end of the file, retrieve an Integer, String, String, Integer, and Integer, in that order for every line in that file.
If you have any questions just ask!
I am currently trying to read a bunch of words from a .txt document and can only manage to read the characters and display them yet. I'd like to do the same but with whole words.
My code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile("banned.txt");
if (!infile)
{
cout << "ERROR: ";
cout << "Can't open input file\n";
}
infile >> noskipws;
while (!infile.eof())
{
char ch;
infile >> ch;
// Useful to check that the read isn't the end of file
// - this stops an extra character being output at the end of the loop
if (!infile.eof())
{
cout << ch << endl;
}
}
system("pause");
}
Change char ch; to std::string word; and infile >> ch; to infile >> word; and you're done. Or even better do the loop like this:
std::string word;
while (infile >> word)
{
cout << word << endl;
}
I recently learned how to read data from a text file, but I would like to continue expanding my knowledge on that matter. I would like to read files that contain numbers and characters. Can anyone give me some advice please?
The following is the code I wrote to read numbers:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
ifstream infile("input.txt", ios::in);
if (!infile) {
cout << "Inputxxxxx file could not be opened" << endl;
exit(1);
}
ofstream outfile ("outputt.txt", ios::out);
if (!outfile) {
cout << "inputssss file could not be opened " <<endl;
exit(1);
}
int number;
int answer[10];
for (int i = 0; i < 9 ; i++) {
infile >> number;
answer[i]=number;
cout << answer[i] << " ";
outfile <<answer[i]<< " ";
}
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Main Routine
void main() {
char in;
string s,m;
fstream f;
// Open file
cout << "Positive Filter Program\n"<< endl;
cout << "Input file name: ";
cin >> s;
cout << "Output file name: ";
cin >> m;
f.open(s.data(),ios::in);
f.open(m.data(),ios::out);
// Loop through file
if(f.is_open())
{
while(f.good())
{
f.get(in);
f<<in;
cout << "\nFinished!"<< endl;
}
}
else cout << "Could not open file";
// Close file
f.close();
}
I am not sure what I am doing wrong here. In this program I am trying to cin the file name that would input, and then would output onto what file name that you typed in.
The same fstream object is being reused:
f.open(s.data(),ios::in);
f.open(m.data(),ios::out);
it will never read the input file. Change to:
std::ifstream in(s.data());
std::ofstream out(m.data());
The while loop is incorrect, the result of a read attempt should be checked immediately after the read:
char ch;
while(in.get(ch))
{
out << ch;
}
cout << "\nFinished!"<< endl; // Moved this to outside the while