I'm studying C++ and I don't know how to read blank word(like " ")
my code just reading only "words"
please help me
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
ofstream output("data4.dat", ios::out | ios::binary);
string buffer = "A program that translates a high level languageto a machine language\n is called a compiler. A compiler is thus a somewhat peculiar sort\n of program and its output is yet another program. To avoid confusion,\n the input program is usually called the source program or\n source code, and the translated version produced by the compiler is called";
output << buffer;
output.close();
ifstream is;
ifstream input("data4.dat", ios::in | ios::binary);
string in_buffer;
if (! is.eof()) {
for ( int i = 0; i < sizeof(input); i++) {
input >> buffer[i];
}
}
input.close();
std::cout << buffer[2] << '\n';
return 0;
}
Related
Hello everyone I need help with one of the excercise I am doing. I have searched alot but didn't found what I was looking.
So, I want my program to read from one .txt file and then gives output in another .txt file.
Source File have this contents.
Name Salary
Aamir 12000
Amara 15000
Adnan 13000
Afzal 11500
Output File should have this
Name Salary
Aamir 14000
Amara 17000
Adnan 15000
Afzal 13500
The program should read the source file and add 2000 to the salary in the output file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c;
char inputFile[] = "my-file.txt";
char outputFile[] = "my-file-out.txt";
ifstream inFile;
ofstream outFile;
inFile.open(inputFile, ios::in);
if (!inFile)
{
cout << "The file cannnot be open please check" << endl;
exit(1);
}
outFile.open(outputFile, ios::out | ios::ate);
while ((c = inFile.get()) != EOF)
{
outFile.put(c);
}
inFile.close();
outFile.close();
}
I was able to copy the content from source file to output file after some struggle but now no matter what I do it won't give me the desirable solution. What should I add into the code that would give the correct output.
prefer using const std::string over const char*
extract the first line and write to output stream. std::getline can help you.
read data from source with operator>> if your source data constists of fixed types of datas.
raise salary and write to dest file with operator<<
Following code works but I omitted handling exceptions or erros for simplicity. You should add handling code when opening, reading, writing data and closing streams.
#include <fstream>
#include <iostream>
#include <string>
using std::string;
int main() {
const string name_input_file = "D:/my-file.txt";
std::ifstream ifs(name_input_file);
const string name_output_file = "D:/my-file-out.txt";
std::ofstream ofs(name_output_file);
string fist_line;
std::getline(ifs, fist_line);
ofs << fist_line << '\n';
string name;
int salary;
while (ifs && ofs) {
ifs >> name >> salary;
salary += 2'000;
ofs << name << ' ' << salary << '\n';
}
return 0;
}
Here's a dirty solution. Reads my-file.text, adds 2000 to each salaries and writes the new strings to my-file-out.txt. Note that my-file-out.txt will lose its previous data everytime you run the program.
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::ifstream ifile;
std::ofstream ofile;
ifile.open("my-file.txt", std::ifstream::in);
ofile.open("my-file-out.txt", std::ofstream::out | std::ofstream::trunc);
std::string line{};
bool flag{true};
while (std::getline(ifile, line))
{
if (flag){ flag = false; ofile << line << "\n"; continue; }
std::string salary{};
for (size_t i{line.length() - 1}; i >= 0; --i)
{
if (line[i] == ' ') break;
salary += line[i];
}
std::reverse(salary.begin(), salary.end());
line.replace(line.length() - salary.length(), line.length(), std::to_string(std::stoi(salary) + 2000));
ofile << line << "\n";
}
ifile.close();
ofile.close();
}
I am trying to run below code but it is neither showing any file on the path nor reading anything from it. Whatever I am writing into the file through "cin >>" it is not being written. Can anybody please let me know mistake in my code below:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char string[80];
cout << "Enter Input" << endl;
cin >> string;
int len = strlen(string);
fstream file;
file.open("TEXT", ios::in | ios::out);
for (int i = 0; i < len; i++)
file.put(string[i]);
file.close();
file.open("TEXT", ios::in | ios::out);
file.seekg(0);
cout << "Output" << endl;
while (file) {
char ch;
file.get(ch);
cout << ch;
}
file.close();
return 0;
}
You should add the fstream::app flag in your open call and this will do the trick !
Don't mess with file modes you don't need (or understand). Open the file for writing only first, and then open it for reading only next. Use different streams for reading and writing.
ofstream file_out;
file_out.open("TEXT");
for (int i = 0; i < len; i++)
file_out.put(string[i]);
file_out.close();
ifstream file_in;
file_in.open("TEXT");
cout << "Output" << endl;
while (file_in) {
char ch;
file_in.get(ch);
cout << ch;
}
Unless you actually understand the rules concerning ios::in and ios::out it's safer to just use ifstream when you want input and ofstream when you want output.
More reading if you do want to understand the rules.
I have an issue with some code I have been working on. I am trying to read the contents of a text file (input.txt) into a variable fileContents. The loop in the code enters, but the program produces no output. Some of the variables are not used, I know about this. What is wrong?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
ifstream input("input.txt");
ofstream output("output.txt"); //init output controller
// new lines will be skipped unless we stop it from happening:
//input.unsetf(std::ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
unsigned line_count = std::count(std::istream_iterator<char>(input),std::istream_iterator<char>(), '\n');
string fileContents = ""; //init message, that will be filled by input.txt
string str; //temp string
while (input >> fileContents)
{
//cout << "loop entered";
cout << fileContents << "\n";
}
//cout << "test" << "\n";
return 0;
}
I'm writing some simple code that's supposed to read every other character, as well as overwriting their adjacent characters with '?'s in a random text file.
eg.
test.txt contains "Hello World";
after running the program, it'd be "H?l?o?W?r?d"
My code below allows me to read every other character from the text file in the console window, but after the program ends and when I open up test.txt, nothing has been changed. Need help to figure out why...
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
while (!data.eof())
{
if (!data.eof())
{
char ch;
data.get(ch);
cout << "ch is now " << ch << endl;
}
if (!data.eof())
data.put('?');
}
data.close();
return 0;
}
You forgot to consider that you have 2 streams, istream and ostream.
You need to synchronize the location of these 2 streams to achieve what you want. I modified your code a bit to show what I mean.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
while (data.get(ch))
{
cout << "ch is now " << ch << endl;
data.seekg(data.tellp()); //set ostream to point to the new location that istream set
data.put('?');
data.seekp(data.tellg()); //set istream to point to the new location that ostream set
}
data.close(); // not required, as it's part of `fstream::~fstream()`
return 0; // not required, as 0 is returned by default
}
You are misusing eof(). Do it like this instead:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream data("test.txt", ios::in | ios::out); //here the test.txt can be any random text file
char ch;
while (data.get(ch))
{
cout << "ch is now " << ch << endl;
data.put('?');
}
data.close();
return 0;
}
I am trying to read a text file into an array of integers but my read doesn't affect anything and my array stays at its default value of 0, my code is as follows
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream myfile;
int a[1000] = {0};
myfile.open ("Euler7.txt", ios::out | ios::app | ios::binary);
for (int i=0;i<1000;i++)
{
myfile << a[i];
cout << a[i] << endl;
}
myfile.close();
int c = 0;
for (int b=0;b>995;b++)
{
if (a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4] > c)
c = a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4];
}
cout << c << a[0];
return 0;
}
i suspect i need an fin.ignore somewhere in there somewhere, but my skills with files go about as far as #include , the file i am trying to open goe something like
6717653133062491922511967442657474235534919493496983520312774506326239578318
No spaces or separation between numbers. i need each item of the array to hold an individual digit, there are 1000 numbers in the file.
Your file stream object is an ofstream, which is for writing out to a file. Instead, you want ifstream, which is for input. You'll also want to get rid of the ios::out and ios::app flags in the call to open. In addition, you're using operator << which is for output, rather than >> for input.
Seems like the fstream technique wasn't needed at all to solve this problem. Special thanks to Tacet for parts of this code:
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int pause;
string series="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
unsigned int max = 0; //if it can not be less than zero, use a unsigned type
const unsigned int end = series.size()-4;
for (int i=0;i<end;i++)
if ((series[i]-'0')*(series[i+1]-'0')*(series[i+2]-'0')*(series[i+3]-'0')*(series[i+4]-'0') > max)
max = (series[i]-'0')*(series[i+1]-'0')*(series[i+2]-'0')*(series[i+3]-'0')*(series[i+4]-'0');
cout << max << '\n';
cin >> pause;
return 0;
}