get() function in c++ not working for files - c++

I wrote the following code in codeblocks and since I am new to programming I would like to know the problem in simple words. Does the open() constructor create a new file if it does not exists?
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
char str[80];
cout<<"Enter a string : ";cin>>str;
int len=strlen(str);
fstream file;
file.open("TEXT",ios::in|ios::out);
for(int i=0;i<len;i++)
file.put(str[i]);
file.seekg(0);
char ch;
cout<<"\nPrintitng Contents....\n";
int k=0;
while(file)
{
file.seekg(k);
file.get(ch);
cout<<ch;
k++;
}
return 0;
}

I think you don't have "TEXT". and fstream::open don't make file when if file that you want to read do not exist.
so you may try in different stream to read and write.
following code will help you.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
char str[80];
cout << "Enter a string : ";
cin >> str;
int len = strlen(str);
ofstream fout;
fout.open("TEXT.txt");
for (int i = 0; i<len; i++)
fout.put(str[i]);
fout.close();
ifstream fin;
fin.open("TEXT.txt");
char ch;
cout << "\nPrintitng Contents....\n";
while (!fin.eof())
{
fin.get(ch);
cout << ch;
ch = NULL;
}
fin.close();
return 0;
}
and you may improve your code like this
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str;
cout << "Enter a string : ";
cin >> str;
ofstream fout;
fout.open("TEXT.txt");
fout << str;
fout.close();
str.clear();
ifstream fin;
fin.open("TEXT.txt");
cout << "\nPrintitng Contents....\n";
fin >> str;
cout << str;
fin.close();
return 0;
}

Following code is more appropriate for C++, I think
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string str;
string newStr;
cout << "Enter a string : "; cin >> str;
int len = str.length();
fstream file;
file.open("TEXT", ios::out| ios::in );
if (!file.is_open())
return 0;
file << str;
file.seekg(0,file.beg);
char ch;
cout << "\nPrintitng Contents....\n";
file >> newStr;
cout << newStr;
file.close();
return 0;
}

Related

While loop wont loop with inFile

I'm currently writing a program that reads in a .txt file. The program is supposed to read a character, output an error, and continue reading the rest of the lines, and follow suit. I'm not sure where I'm going wrong.
#include <iostream>
#include <cmath>
#include <string>
#include <stream>
using namespace std;
int main(){
double runningTotal;
int c = 0;
string fileName;
ifstream infile;
cout << "Enter a file name\n";
cin >> fileName;
infile.open(fileName.c_str());
if (!infile.is_open()) {
cout<<"Error! invalid entry please retry!\n";
}
else {
string line;
int a, b;
while (!inflile.fail()) {
infile >> a >> b;
c = a + b;
runningTotal = c;
cout <<a<<" + "<<b<<" = " << runningTotal<<endl;
}
getline(infile, line, '\n');
if (infile.fail()){
getline(infile, line, '\n');
cout<<"stop being stupid\n";
}
}
}

Where did I go wrong in my code for word count C++

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void wordCount(ifstream& in_stream, ofstream& out_stream);
int main()
{
char inputFile[100];
ifstream fin;
ofstream fout;
cout << "Enter a File name: " << endl;
cin >> inputFile;
fin.open(inputFile);
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
wordCount(fin, fout);
fin.close();
fout.close();
return 0;
}
void wordCount(ifstream& in_stream, ofstream& out_stream)
{
int counter = 0,i;
char next,last[1];
in_stream.get(next);
while (!in_stream.eof())
{
if (next == ' ')
(next >> last[1]);
for(i = 0; last[i] != '\0'; ++i)
{
if (last[i] == ' ')
counter++;
}
in_stream.get(next);
}
}
I'm trying to get the word count of this and its not working
the chars being saved are fine, but whats not working if I input from notepad a file with something like:
I
am
working
it will show 0 words if I I type normally it will count the words why is that?
I edit your code, Do you mean something like this?
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int wordCount(ifstream& in_stream, ofstream& out_stream);
int main()
{
char inputFile[100];
ifstream fin;
ofstream fout;
cout << "Enter a File name: " << endl;
cin >> inputFile;
fin.open(inputFile);
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
int WordCount = wordCount(fin, fout);
fin.close();
fout.close();
return 0;
}
int wordCount(ifstream& in_stream, ofstream& out_stream)
{
int counter = 0;
char data[100];
in_stream >> data;
while (strlen(data)>0)
{
counter++;
in_stream >> data;
}
return counter;
}

C++ ifstream error using string as opening file path

I get this error when I try running the program.What might be the problem as the code is correct as far as I can see.
Here is the error
std::basic_fstream::basic_fstream(std::string&, const openmode&)'
Here is the code
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string fileName;
int frequencyArray[26];
char character;
for (int i = 0; i < 26; i++)
frequencyArray[i] = 0;
cout << "Please enter the name of file: ";
getline(cin, fileName);
fstream inFile(fileName, fstream::in); // to read the file
if (inFile.is_open())
{
while (inFile >> noskipws >> character)
{
// if alphabet
if (isalpha(character))
{
frequencyArray[(int)toupper(character) - 65]++;
}
}
inFile.close();
cout << "Letter frequencies are as: " << endl;
for (int i = 0; i < 26; i++)
{
cout << (char)(i + 65) << " = " << frequencyArray[i] << endl;
}
}
else
{
cout << "Invalid File. Exiting...";
}
return 0;
}
You could change
fstream inFile(fileName, fstream::in);
to
fstream inFile(fileName.c_str(), fstream::in);
Although C++11 defines a std::fstream constructor that accepts a std::string as input, Microsoft's implementation of std::fstream apparently does not:
https://msdn.microsoft.com/en-us/library/a33ahe62.aspx#basic_fstream__basic_fstream
You will have to use the std::string::c_str() method to pass the filename:
fstream inFile(fileName.c_str(), fstream::in);
That being said, consider using std::ifstream instead:
ifstream inFile(fileName.c_str());

How to detect a new line and add it to the string. Fstream

Hello I created this program and the only problem is I don't know how to detect a new line and how to add \n to the string every time new line occurs. I'm using Visual Studio Community 2015 if it matters. Thank you!
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main() {
ofstream ofile;
ifstream ifile;
string filehold;
vector<string> data;
string line;
ofile.open("C:/Users/Nada/Desktop/data.txt");
ofile << "ph4n70m is awesome \n LOL im awesome"<< flush;
ifile.open("C:/Users/Nada/Desktop/data.txt");
if (ifile.fail()) {
cerr << "Error!" << endl;
system("pause");
exit(1);
}
while (!ifile.eof()) {
ifile >> filehold;
data.push_back(filehold);
}
cout << "data.txt: " << endl;
for (unsigned int i = 0; i < data.size(); i++) {
cout << data[i] + " ";
}
ifile.close();
system("pause");
return 0;
}
Change your reading loop to
while (std::getline(ifile,filehold)) {
filehold += '\n';
data.push_back(filehold);
}

Displaying content of a file in C++

I've made a program using which we can add records in a file is an
order, but the displaying of the file after adding the record is
not working.
If I add "|ios::app" in "ofstream fo" to open the file in append
mode, the displaying by "fin" is working.
Why is it so? I'm using mingw4.8.1
#include<fstream>
#include<iostream>
#include<stdio.h>
class c{
public:
int r;
char nm[20];
};
using namespace std;
int main()
{
c a,b;
ifstream fi("old.txt",ios::binary);
ofstream fo("new.txt",ios::binary); // if |ios::app is added here, the
// display by fin below is working fine
cout<<"Enter roll\t";
cin>>b.r;
cout<<"Enter name\t";
fflush(stdin);
gets(b.nm);
int w=0;
while(true)
{
fi.read((char *)&a,sizeof(a));
if(fi.eof()) break;
if(b.r<a.r&&w==0)
{
fo.write((char *)&b,sizeof(b));
w++;
}
else
fo.write((char *)&a,sizeof(a));
}
ifstream fin("new.txt",ios::binary); // this is not working of the //ios:: is not added in the ofstream fo
while(true)
{
fin.read((char *)&a,sizeof(a));
if(fin.eof()) break;
cout<<a.r<<endl;
puts(a.nm);
}
return 0;
}
This is the correct code closest to what you want
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct c {
int roll;
string name;
};
int main() {
c a, b;
ifstream fi("old.txt");
ofstream fo("new.txt");
cout << "Enter roll no. and name" << endl;
cin >> b.roll >> b.name;
while (fi >> a.roll >> a.name) {
if (b.roll < a.roll)
fo << b.roll << " " << b.name << endl;
else
fo << a.roll << " " << a.name << endl;
}
fi.close();
fo.close();
ifstream fin("new.txt");
while (fin >> a.roll >> a.name)
cout << a.roll << " " << a.name << endl;
fin.close();
return 0;
}
It is my understanding that if you use ios::app you must also indicate ios::out try ios::app | ios::out | ios::binary and see if that helps.